XML parsing

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

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.
Dan Ebberts
Posts: 320
Joined: June 26th, 2004, 10:01 am
Location: Folsom, CA
Contact:

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
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

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?
Dan Ebberts
Posts: 320
Joined: June 26th, 2004, 10:01 am
Location: Folsom, CA
Contact:

I think it would be .languages.language[0] etc.

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

Dan
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

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.
Post Reply