How to parse xml files to extract tags, elements, attributes and node value from xml file
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
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
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();
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
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
Copy link to clipboard
Copied
hi,
write correctly:
xml.length()
read documentation:
http://estk.aenhancers.com/9%20-%20Integrating%20XML%20into%20JavaScript/xml-object-reference.html
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
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
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
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

