Copy link to clipboard
Copied
Hi
I have a XML file with the following structure:
<rootEl>
<entry category = "00">
<sub_entry_1>Content</sub_entry_1>
<sub_entry_2>Content</sub_entry_2>
</entry>
<entry category = "01">
<sub_entry_1>Content</sub_entry_1>
<sub_entry_2>Content</sub_entry_2>
</entry>
<entry category = "02">
<sub_entry_1>Content</sub_entry_1>
<sub_entry_2>Content</sub_entry_2>
</entry>
</rootEl>
As you can see it has 3 elements, or children (do not know the right therm). But it has 3 "entry".
I want a script to read this as a XML and tell me the length of the XML elements.
So, suppose this XML file is inside Desktop:
var xmlFile = new File ("~/desktop/myFile.xml");
xmlFile.open ("r");
var xmlContent = xmlFile.read();
xmlFile.close();
var xmlObj = new XML(xmlContent);
It's perfect. At this point I have a XML object in my scripting code, called xmlObj.
The problem is now: I need to know the length of this xmlObj. It shoud tell I have 3 entries. But, if I set "xmlObj.length", like it it was an array object, then it returns nothing.
Looked at JavaScript Tools Guide, but perhaps I missed anything. I did not find a method to read the length of the XML.
Can anyone help me?
Thank you very much
Gustavo.
xmlObj.children().length()
I always make this mistake...
Copy link to clipboard
Copied
Try xmlObj.length()
Copy link to clipboard
Copied
Hi xbytor2
I already tried it, but this method always returns 1 as the value.
I pressed F1 in ExtendedScript Toolkit to read about this method. It says this method only works with XML Lists. If the object is not a XML list, the result will be always 1.
Do you know how could I convert my XML object into a list?
Thank you very much
Gustavo
Copy link to clipboard
Copied
Hi
An alternative way I found.... It's working. I created a function to calculate the length. Not so elegant, but it's working.
Suppose your XML variable is called xmlObj (like the example above). And you want to discover the number of elements in the structure (the elements in the example are named entry):
function calculateXML(obj){
var g = 0;
var test;
do{
test = obj.entry[giro];
if (test != undefined){
g = g + 1;
}
} while (test != undefined);
return g;
}
Gustavo
Copy link to clipboard
Copied
xmlObj.children().length()
I always make this mistake...
Copy link to clipboard
Copied
Perfect xbytor2
Thank you very much. Easier than my function! haha
Gustavo
Copy link to clipboard
Copied
The JS/XML API takes a bit of getting used to. If I'm not using it on a monthly basis, I'll forget stuff. The ESTK docs are relatively good, you just have to switch your brain into "xml-mode" when use this stuff.