XML parsing
Moderator: Paul Tuersley
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.
-
- 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
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
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?
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?
Code: Select all
<languages>
<version>1</version>
<language>en-US</language>
<language>ja-JP</language>
</languages>
-
- 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
Also, sometimes you have to add .toString() to get things to work right.
Dan
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.
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.