Highlighted

How to parse xml files to extract tags, elements, attributes and node value from xml file

Explorer ,
May 05, 2020

Copy link to clipboard

Copied

Hi All,

 

I am working in a application in which I need to extract tags, elements, attributes and node value to generate In-design document. The xml generated for my application is dynamic and based on the tags I need to identify element, attributes and nodes and use it with the in-design scripts to generate the in-design document. 

 

Kindly share any good tutorial or link with me which help to parse xml file to get all the required values from it

 

I have attached one sample xml file for reference

TOPICS
Import and export, Scripting, SDK

Views

1.3K

Likes

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

How to parse xml files to extract tags, elements, attributes and node value from xml file

Explorer ,
May 05, 2020

Copy link to clipboard

Copied

Hi All,

 

I am working in a application in which I need to extract tags, elements, attributes and node value to generate In-design document. The xml generated for my application is dynamic and based on the tags I need to identify element, attributes and nodes and use it with the in-design scripts to generate the in-design document. 

 

Kindly share any good tutorial or link with me which help to parse xml file to get all the required values from it

 

I have attached one sample xml file for reference

TOPICS
Import and export, Scripting, SDK

Views

1.3K

Likes

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
May 05, 2020 0
Adobe Community Professional ,
May 05, 2020

Copy link to clipboard

Copied

Look into the following discussion for a sample to load an XML file, read attributes and also executing an xpath

https://community.adobe.com/t5/indesign/read-xml/m-p/10914241?page=1

 

For more details go through the XML object documentation at the following link

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

 

-Manan

Likes

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
Reply
Loading...
May 05, 2020 1
Explorer ,
May 05, 2020

Copy link to clipboard

Copied

Thanks Mahan for the detail In-Design Object Documentation but when I am tring to call any of the functions specified in the link it is not working, not sure why. Can you please help me to know the reason 

for ex. If I am using the below script it is giving me error in count function. same as for any other function specified in the link https://www.indesignjs.de/extendscriptAPI/indesign-latest/#XML.html, please help

var myDocument = app.documents.add();

myDocument.importXML(File("/d/IV-3-a.en.xml"));

var myXMLElement = myDocument.xmlElements.item("EIF");
var i= myXMLElement.count();

 

Likes

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
Reply
Loading...
May 05, 2020 0
Adobe Community Professional ,
May 05, 2020

Copy link to clipboard

Copied

I misunderstood your question, the example i gave was a way to load an XML and parse it without anything to do with InDesign, it was pure ExtendScript. What you are trying to do is load the XML in an InDesign document and then traverse/parse it. For that you need to look at the following documentation

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

 

Some examples to get you started are mentioned below

https://community.adobe.com/t5/indesign/get-coordinates-from-xml-tag/m-p/11096670?page=1

https://community.adobe.com/t5/indesign/getting-xml-attribute-value/m-p/9823639?page=1

https://community.adobe.com/t5/indesign/xml-tage-name/m-p/9478137?page=1

 

-Manan

Likes

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
Reply
Loading...
May 05, 2020 1
Explorer ,
May 05, 2020

Copy link to clipboard

Copied

Hi Mahan,

Thanks again for your help but don't why still the functionality is not working..For ex. I am using the length property defined in the link for xml element collection but it is not working for me. Any Idea what can be the reason

 

ashishktrig_1-1588687270559.png

 

 

Likes

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
Reply
Loading...
May 05, 2020 0
Participant ,
May 05, 2020

Copy link to clipboard

Copied

Likes

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
Reply
Loading...
May 05, 2020 0
Adobe Community Professional ,
May 05, 2020

Copy link to clipboard

Copied

I suppose there is some confusion, the code is referencing an XMLElements object and not an XML object. In XMLElements length is a property and not a method, so the code is right in this respect. See the following

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

 

-Manan

Likes

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
Reply
Loading...
May 05, 2020 1
Adobe Community Professional ,
May 06, 2020

Copy link to clipboard

Copied

Hi,

If you are trying to just find the nodes and do something to it inside InDesign you can do that in any of the two methods like this:

/////////// First Method to find all nodes using Xpath ////////////////////////////////////////////////////////
var allNodes = app.documents[0].xmlElements[0].evaluateXPathExpression("//EIF");
alert(allNodes.length);
/////////// Second Method to find all nodes using Xpath //////////////////////////////////////////////////
var allNodes = [];
var root = app.documents[0].xmlElements[0];
var proc = app.xmlRuleProcessors.add(["//EIF"]);
var match = proc.startProcessingRuleSet(root);
var myElement = null;
while(match != undefined){
    myElement = match.element;
    if(myElement != undefined && myElement != null){
        allNodes.push(myElement);
        }
    match = proc.findNextMatch();
    }
alert(allNodes.length);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

Best

Sunil

Likes

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
Reply
Loading...
May 06, 2020 1
Adobe Community Professional ,
May 05, 2020

Copy link to clipboard

Copied

Did you look at the samples i gave? The code you have written is not right. When you access xmlElements on the document object you get the root element i.e. Document as per your XML. It does not have any node with the name EIF and hence the code fails.

 

This means that you can't just sit on a node and use the item method to access a node anywhere in the hierarchy downwards from it. For precisely this thing you will need to use the method evaluateXPathExpression method. XPaths are sort of query language for XML, using this you can query like get all the nodes named "EIF" which are present anywhere in the descendent hierearchy of the current node. Search internet you will get lots of examples of how to write xpaths and you should be good

 

-Manan

Likes

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
Reply
Loading...
May 05, 2020 2
Explorer ,
May 14, 2020

Copy link to clipboard

Copied

Hi Mahan,

 

When I use following scripts in indesign it generate the content with tag like <i></i> for Italics and <b></b> for bold but the content inside it still doesn't get shown in either Italics or Bold.

 

TextFrameChapterDetails.contents =labelText.italics();
TextFrameChapterDetails.contents =labelText.bold();

 

Added another question for it in support formum but didn't get the answer yet, so kindly help

 

Likes

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
Reply
Loading...
May 14, 2020 0