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

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: ``

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 ;)

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.

Unity / XML

http://unitynoobs.blogspot.com/

  • Read
  • Write
  • Encrypting

Save - load script settings from xml

Download :

https://docs.google.com/open?id=0B0aMXRZ2PWXZQjhKMXdDLVJLZDA