• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

XML - append child

Contributor ,
Jul 26, 2019 Jul 26, 2019

Copy link to clipboard

Copied

Hi,

     I am generating new xml from existing xml by reading as xml. Reading elements (specific elements) and appending it into new xml file by using appendChild of xml method. The element which I appending have nested elements so that final xml have the nested elements as new line without space before. But i am appending with space and the elements.  pls. refer the below screenshots.

    Indesign.png

Code:

xmlProcess();

function xmlProcess() {    

      var root = new XML("<root></root>");

      var child = new XML("<child>Child 1</child>");    

      root.appendChild(child);

      var child2 = new XML("<child>Child 2 <emph>test contents</emph></child>");          //

      root.appendChild(child2);

      var file = new File("~/Desktop/test.xml");

      var xml = root.toXMLString();

      file.open("W");

      file.write(xml);

      file.close();

        

      var doc = app.activeDocument;     

      doc.importXML(file);    

    };

    

     How to append the element in a single line as it is given in a string..

TOPICS
Scripting

Views

1.7K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Jul 31, 2019 Jul 31, 2019

I told you earlier that playing with xml using javascript will get you in little bit trouble with spacing issue :

spaceIssue.png

space getting deleted: this problem has a pattern, that spaces just before a child starts or child ends or a sibling starts or end.

That means near by tag opening and closing spaces you have to look around. you can tackle them with non-breaking space entity.

Here's how you can fight with space deletion near closing or opening tag bracket problems:

xmlProcess(); 

function xmlProcess() {  

...

Votes

Translate

Translate
People's Champ ,
Jul 26, 2019 Jul 26, 2019

Copy link to clipboard

Copied

Hello,

If I am not mistaken, your problem isn't a code one but a indesign one. XML in InDesign is mainly all about a text flow and every indentations made of tabs or spaces will be unless you asked otherwise in teh XML import options be considered as text characters inside InDesign.

So if you want your child two not to be "indented" in the layour, either ask to "Do Not Import Contents Of Whitespace-Only Elements" in the regular import options or set those in the app.xmlImportPreferences…

…Or set prettyIndent to false prior to output your XML String and deal with encoded carriage returns on your own.

FWIW

Loic

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 26, 2019 Jul 26, 2019

Copy link to clipboard

Copied

Hi,

     I am inserting the elements (child2) with emph tag as single.  but in generated xml, its in next line.

     please check the xml structure generated in desktop.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jul 26, 2019 Jul 26, 2019

Copy link to clipboard

Copied

As far as I can see that you are trying to transform xml with JavaScript just like E4X.

Well try this code:

xmlProcess();

function xmlProcess() {   

    var root = new XML("<root></root>");

    var child = new XML("<child>Child 1</child>");   

    root.appendChild(child);

    var child2 = new XML("<child>Child 2<emph>test contents</emph></child>");          //

    root.appendChild(child2);

    var file = new File("~/Desktop/test.xml");

    var xml = root.toXMLString();

    xml = xml.replace(/\n[\s]+/g,""); // replace all enter marks or line breaks with nothing

    xml = xml.replace(/(<\/child>)/g,"$1\n"); // put enter mark wherever you need to put into it

    //// transforming xml with javascript will get you in a little bit more trouble about space getting deleted

    //==== That you have to manage n play along with it==

    file.open("W");

    file.encoding = 'UTF-8';

    file.write(xml);

    file.close();

    var doc = app.activeDocument;    

    doc.importXML(file);

    }

My first advice to you for transforming xml, is to go with XSLT.

Still if you want in this way only, hit it.

This link also will get you some thing more Ecmascript4Xml

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#XML.html

Best

Sunil

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 31, 2019 Jul 31, 2019

Copy link to clipboard

Copied

Hi,

     Thank you for your reply... I will check the link which you given...

     Its also gives the same output.

In.png

     Out.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jul 31, 2019 Jul 31, 2019

Copy link to clipboard

Copied

I told you earlier that playing with xml using javascript will get you in little bit trouble with spacing issue :

spaceIssue.png

space getting deleted: this problem has a pattern, that spaces just before a child starts or child ends or a sibling starts or end.

That means near by tag opening and closing spaces you have to look around. you can tackle them with non-breaking space entity.

Here's how you can fight with space deletion near closing or opening tag bracket problems:

xmlProcess(); 

function xmlProcess() {     

    var root = new XML("<root></root>"); 

    var child = new XML("<child>Child 1</child>");     

    root.appendChild(child); 

    var child2 = new XML("<child>Child 2&#160;<emph>test contents</emph></child>");          // 

    root.appendChild(child2); 

    var file = new File("~/Desktop/test.xml"); 

    var xml = root.toXMLString(); 

    xml = xml.replace(/\n[\s]+/g,""); // replace all enter marks or line breaks with nothing 

    xml = xml.replace(/(<\/child>)/g,"$1\n"); // put enter mark wherever you need to put into it 

    //// transforming xml with javascript will get you in a little bit more trouble about space getting deleted 

    //==== That you have to manage n play along with it== 

    file.open("W"); 

    file.encoding = 'UTF-8'; 

    file.write(xml); 

    file.close(); 

    var doc = app.activeDocument;      

    doc.importXML(file); 

    }

Best

Sunil

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 01, 2019 Aug 01, 2019

Copy link to clipboard

Copied

Hi,

     You have used entity for space to retain the space right.

     When appending child we need to replace the space to entity and need to proceed? Is it right method to proceed with an existing xml file?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 01, 2019 Aug 01, 2019

Copy link to clipboard

Copied

Space near opening bracket of child tag if there is any space you want to retain at that place you can use the non-breaking space entity &#160;

Apart from that other spaces will be retained anyway.

Best

Sunil

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 09, 2019 Aug 09, 2019

Copy link to clipboard

Copied

Hi,

     Thanks for your reply.

     I have done it for space with < symbol in string and added. But i could not see the entity &#60; for space but space is retain.

     But for the "tab" is not retain. Can you pls. clarify for the below points.

     1.  Other space will be retain - Em space retained but tab is not retained.

     2. will it affect symbols like < and >

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 09, 2019 Aug 09, 2019

Copy link to clipboard

Copied

Hi @https://forums.adobe.com/people/Sudha%20K​,

For your points:

     1. -> To keep your tabs use this entity: &#9;

     2. -> No. it wont affect < and >.

Best

Sunil

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 09, 2019 Aug 09, 2019

Copy link to clipboard

Copied

LATEST

Hi,

Thank you...

I have checked for space and tab and noticed the below points.

1. Replace space and tab in xml string and convert to xml object.  when generating new xml file, entities are not there and also tab is converted single space like tat.

2. space is missing in before and after the tag "<" & ">". Tab is missing over all place when using string to xml(str). For tab need to replace all occurences of document? and also what are the characters need to check and handle?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines