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

Read the length of a XML

Advocate ,
Jan 11, 2016 Jan 11, 2016

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.

TOPICS
Actions and scripting

Views

720

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

Advisor , Jan 11, 2016 Jan 11, 2016

xmlObj.children().length()

I always make this mistake...

Votes

Translate

Translate
Adobe
Advisor ,
Jan 11, 2016 Jan 11, 2016

Copy link to clipboard

Copied

Try xmlObj.length()

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 ,
Jan 11, 2016 Jan 11, 2016

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

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 ,
Jan 11, 2016 Jan 11, 2016

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


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
Advisor ,
Jan 11, 2016 Jan 11, 2016

Copy link to clipboard

Copied

xmlObj.children().length()

I always make this mistake...

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 ,
Jan 12, 2016 Jan 12, 2016

Copy link to clipboard

Copied

Perfect xbytor2

Thank you very much. Easier than my function! haha

Gustavo

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
Advisor ,
Jan 13, 2016 Jan 13, 2016

Copy link to clipboard

Copied

LATEST

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.

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