- Home /
Easy XML Parsing
I am battling to work / understand XML. Where can I find good clear help or tutorials on the topic? I am working in JavaScript.
Thanks
Answer by Bunny83 · May 10, 2011 at 03:06 PM
Do you have problems in understanding XML itself or how to read/write it? XML is a really simple but powerful language. Actually there are multiple ways of storing data in it. Some put their data values inside an element as text-node others (like me) prefer to put them as attributes into an element.
Here some example elements:
// a simple element without a body
//<ElementName/>
//
// a normal element with body
//<ElementName> body </ElementName>
//
// another empty element without a body but an attribute
//<ElementName AttributeName="AttributeValue" />
There are some rules you should keep in mind:
- An xml file can only have one root element. The only exception is the declaration node at the top
- An xml file should contain a declaration-node at top to specify the character encoding
- Element or attribute names mustn't contain spaces
- Attributes are seperated with spaces
- In most cases the attribute value should be placed in parenthesis.
- Every element that have been opened have to be closed. Single/empty elements don't need to be closed since they don't have a body section.
- The body of an element can contain other elements or just text (which counts as textelement)
- if you want to store text with sensitive characters like "<" "\" ">" they have to be stored inside a CDATA element.
- A CDATA element looks like:
<![CDATA[data goes here]]>
To read or write an xml file there are multiple possibilities. The most common way is to use a DOM-node-tree to represent the xml in memory. Another way (which might be a bit more advanced) is to use a SAX-parser which invokes callback routines for every element it parses.
SAX is great if the file is huge and you don't want to have the whole file as DOM in memory. But in most cases DOM is the easiest way.
In .NET or mono there are some special classes that represents a whole xml file as DOM tree. XmlDocument
and XmlElement
is what you need.
In JS (in C# as well) you need to include the xml namespace in order to use those classes.
// JS import System.Xml;
// C# using System.Xml;
If you have specific problems just ask ;)
Thanks for your help. I am trying to read the 3 values of three nodes in an X$$anonymous$$L file that I receive through loading a URL. One of the three nodes contains a URL for an image I need to load. It is an ad stream from inner-active.com
Will System.Xml work on every architecture? I mean, in iOS, Android, etc.
@$$anonymous$$arzoa: Yes it does, but it adds quite a bit to your filesize. I don't remember the actual size but i think it was around 500$$anonymous$$B.
I used it myself on Android and iOS for my menu system. There are alternatives to System.Xml but some of them lack of some basic features.
Depending on the data you want to store / read you can also use JSON. I've just published my JSON framework. It's very easy to use.
Answer by CHPedersen · May 10, 2011 at 01:46 PM
I used the XmlReader class to read an XML-file and load the tree into an object I could work with. Is your XML in a file? If so, take a look at this tutorial:
http://msdn.microsoft.com/en-us/library/aa720470%28v=vs.71%29.aspx
If your XML is from a Stream, there's an explanation here:
http://support.microsoft.com/kb/301228
I apologize that these articles are in C# and not JavaScript. The same principles should apply, though.
Answer by loihb · Aug 01, 2012 at 11:51 PM
Save - load script settings from xml
Download :
https://docs.google.com/open?id=0B0aMXRZ2PWXZQjhKMXdDLVJLZDA
Your answer
Follow this Question
Related Questions
Help a noob parse an xml file 1 Answer
Blimp (airship) Script 0 Answers
How should I begin to program an rpg? 1 Answer
Get google account on webgl 0 Answers
C# XML Question 1 Answer