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

parse XML file

Community Beginner ,
Sep 19, 2012 Sep 19, 2012

i'm trying parse a user selected XML file and am completely stuck. i've read through the Javascript Tools Guide CS6 - XML Object Reference section several times, but i must be missing something. here is my XML:

<xml>

          <comp>

                    <name>My Comp</name>

                    <width>1280</width>

                    <height>720</height>

                    <fps>24</fps>

          </comp>

          <layers>

                    <layer value="Layer Name 1">1</layer>

                    <layer value="Layer Name 2">2</layer>

                    <layer value="Layer Name 3">3</layer>

                    <layer value="Layer Name 4">4</layer>

          </layers>

</xml>

and here is my script attempting to parse it:

var config = File.openDialog("Choose A Config File","XML:*.xml");

if (config)

{

          var xmlString = config.toString();

          var myXML = new XML(xmlString);

          alert(myXML.length);

} else {

          alert("Could not open file.");

}

i get a blank alert box. future thanks for the help.

TOPICS
Scripting
8.5K
Translate
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 ,
Sep 20, 2012 Sep 20, 2012

Hi,

As for all the properties in XML, you need to put a pair of parenthesis at the end, like that myXML.length(). eg.name(), localName(), parent(), etc...

By calling myXML.length, you are actually asking for the child called length, like this :

<xml>

     <length/>

</xml>

As there is no child called length in your xml, you get no answer. Also, if you need to alert an XML, use myXML.toXMLString().

So whachout, it is an error that will happen quite a few times in your life. It is easy to lose a day of work trying to fing the bug when it is actually a forgotten pair of parenthesis (happenned to me last month).

Translate
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
Community Beginner ,
Sep 20, 2012 Sep 20, 2012

@chauffeurdevan thanks for pointing that out; i didn't see that "()" was expected when reading all XML properties.

however, i'm still unclear about how to traverse and read through the XML nodes. for instance, how would i find and then alert the "width" node from my XML?

<xml>

    <comp>

        <width>1280</width>

    </comp>

</xml>

Translate
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 ,
Sep 21, 2012 Sep 21, 2012

There is a lot of way, depending on how much you know the xml you are parsing.

Here is some example, and you can mix between them.

Let's call the xml you just wrote myXML

alert(myXML.comp.width)

for each(var child in myXML){
     for each(var subchild in child){
          alert(subchild)
     }
}

alert(myXML..width)

alert(myXML.comp.width)

alert(myXML.children()[0].children()[0])

I really suggest you give a look at this page, it is written for ActionScript, but it is really similar to javascript/extendscript http://www.senocular.com/flash/tutorials/as3withflashcs3/?page=4

Translate
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
Community Beginner ,
Sep 21, 2012 Sep 21, 2012

interesting, that's what i've been testing, but to no avail. i still get a blank alert box. i'm not sure if this makes any difference but i'm testing my code in After Effects, not through Extendscript.

also, whenever i alert() for any value using toXMLString() i get the path to the XML file (e.g. ~/Desktop/Test.xml). here's my *.jsx code:

var config = File.openDialog("Choose Character Config File","XML:*.xml");

          if (config)

          {

               var xmlString = config.toString();

                var myXML = new XML(xmlString);

                alert(myXML.toXMLString());

                alert(myXML.comp.width);

          } else {

                    alert("could not open file");

          }

and my sample XML:

<xml>

      <comp>

               <name>My Comp</name>

               <width>1280</width>

               <height>720</height>

               <fps>24</fps>

          </comp>

</xml>

when i run this, i get an alert box that shows "~/Desktop/Test.xml", followed by another alert box that blank.

Translate
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
Community Expert ,
Sep 21, 2012 Sep 21, 2012

I think what you'e missing is actually reading and closing the XML file:

var xmlString = config.read();

var myXML = new XML (xmlString);

config.close();

Dan

Translate
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
Community Beginner ,
Sep 22, 2012 Sep 22, 2012

@Dan thanks, you got me 99% there. i had to add config.open("r"); to make it finally work.

for future strugglers reference, here's the final code:

var config = File.openDialog("Choose Config File","XML:*.xml");

          if (config)

          {

               config.open("r");

               var xmlString = config.read();

                var myXML = new XML(xmlString);

               alert(myXML.toXMLString());

 

               config.close();

          } else {

               alert("Could not open file");

          }

Translate
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
Community Beginner ,
Nov 26, 2013 Nov 26, 2013

Still little result even with that. The xml variable contains the original file's text, but it won't parse ...

The code :

    f = File("test.xml");

    f.open("r");

    if(f) {

        f.readln();

        var xml = new XML(f.read());

        f.close();

        alert(xml.toString());

        alert(xml.PPTSlideShowSession);

    }

The file :

<?xml version="1.0" encoding="utf-8"?>

<PPTSlideShowSession xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" PPTSlideShowStartDateTime="2013-11-08T14:27:28.6445308+01:00" PPTSlideShowEndDateTime="2013-11-08T14:33:40.8945308+01:00" PowerpointVersion="12.0" xmlns="http://tempuri.org/Schema.xsd">

  <ShowSlideInfo SlideId="271" SlideIndex="1" SlideNumber="1" BeginTime="5008" />

  <ShowSlideInfo SlideId="287" SlideIndex="2" SlideNumber="2" BeginTime="10003" />

  <ShowSlideInfo SlideId="288" SlideIndex="3" SlideNumber="3" BeginTime="15007" />

</PPTSlideShowSession>

The output :

First alerts show the raw file content

Second alert : nothing (blank)

Translate
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
Community Beginner ,
Nov 26, 2013 Nov 26, 2013
LATEST

It seems the colons in the PPTSlideShowSession attributes is messing it all up.

Translate
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