Page 1 of 1

XML parsing

Posted: September 1st, 2012, 12:11 pm
by byronnash
What are you all using to get data out of a XML file? I'm thinking about switching to XML to store data in instead of CSV style text files. I was hoping there was a good method in place already that will read a file and return an object with all the nodes of the XML in it.

Re: XML parsing

Posted: September 2nd, 2012, 11:19 pm
by Dan Ebberts
It's pretty straightforward once you have the file object:

myXmlFile.open("r");
var myXmlString = myXmlFile.read();
var myRoot = new XML (myXmlString);
myXmlFile.close();

There's a chapter on it in the JavaScript Tools Guide.

Dan

Re: XML parsing

Posted: September 3rd, 2012, 12:05 pm
by byronnash
Wow, I didn't realized it was so simple. In testing, it seems to accept some XML files and not others. Also, how would you access something like this?

Code: Select all

      <languages>
        <version>1</version>
        <language>en-US</language>
        <language>ja-JP</language>
      </languages>
For the "version" token I think I could read it by putting .languages.version after my XMLroot object. But how do you read the language token listed multiple times?

Re: XML parsing

Posted: September 3rd, 2012, 12:23 pm
by Dan Ebberts
I think it would be .languages.language[0] etc.

Also, sometimes you have to add .toString() to get things to work right.

Dan

Re: XML parsing

Posted: September 7th, 2012, 10:59 am
by byronnash
Thanks, I have the reading and writing working now.

Is there a way to write an XML from an Object? I can't find a way to dynamically access all the properties of an object. It would be awesome if you could convert the tree-like structure of an object straight into a XML object.