- Home /
How to access embedded nodes without system.xml
After getting a bit of an answer from here: http://answers.unity3d.com/questions/378690/converting-from-systemxml-to-monoxml.html I decided to try and use the suggested TinyXML class to read my data. However, i cannot get to information that is stored deep within tags. For example, i want to get the content from the second of three "string" tags that are all stored inside a "dict" tag which is stored inside another "dict" tag. If it cannot be done with TinyXML then i am happy to change to any other suggestion.
Out of curiosity: Why do you wish to avoid System.Xml in the first place? It's a very powerful X$$anonymous$$L library, and easily supports what you're trying to do.
@CHPedersen - but has lots of dependencies, so for simple reading of small xml files it's overkill.
As Graham pointed out its overkill. This particular job is for a 2D animation script, which i hope to be able to use on any platform and donate to others, so its worth getting it streamlined.
Answer by Graham-Dunnett · Jan 15, 2013 at 10:25 PM
Try this xmltest.cs
. This kinda shows how to use mono.xml
. I've not used this code in production, but it compiles, and dumps out the expected values to the console. You'll need to grab the mono.xml
zip file linked to from the Unity website, and copy the files into your project.
//c#
using UnityEngine;
using System.Collections;
using Mono.Xml;
using System.IO;
public class xmltest : MonoBehaviour {
class Handler : SmallXmlParser.IContentHandler {
public void OnStartParsing (SmallXmlParser parser)
{ // start of file
}
public void OnEndParsing (SmallXmlParser parser)
{ // end of file
}
public void OnStartElement (string name, SmallXmlParser.IAttrList attrs)
{ // opening tag, with attributes
Debug.Log ("Got <" + name + ">");
foreach (string s in attrs.Names)
Debug.Log ("Attribute name: " + s);
foreach (string t in attrs.Values)
Debug.Log ("Attribute value: " + t);
}
public void OnEndElement (string name)
{ // closing tag
}
public void OnChars (string s)
{ // string between open and closing tag
Debug.Log (s);
}
public void OnIgnorableWhitespace (string s)
{
}
public void OnProcessingInstruction (string name, string text)
{
}
}
void Start () {
SmallXmlParser x = new SmallXmlParser();
Handler h = new Handler();
TextReader textReader = new StreamReader(Application.dataPath + "/test.xml");
x.Parse(textReader, h);
}
}
I tested this code with the following in Assets/test.xml
.
<?xml version="1.0"?>
<dict>
<dict>
<string jim="sheila">fred</string>
<string>Douglas</string>
<string>Another string</string>
</dict>
</dict>
Note that there is no way to say "get me the second string two dicts down". You'll need to count the number of calls into OnEndElement()
when string name
is equal to dict, and then look for the second call to OnEndElement()
when string name is equal to string. If you know that your xml is clean, and the string you need is always the 4th tag, then just count 4 tags.
I am certain that you can achieve the same with TinyXML. I have never used that.
Thank you for the detailed help! That is a great example to get started with. I solved the problem of getting to the correct tags by using bool flags. I had a few other problems that i thought i would mention here for others. First, i was getting "This parser does not support document type." I manually modified my xml by taking out the top line which fixed it. This is only a temporary fix, but it meant i don't have to modify the mono document. However, my next issue came with an "Inconsistent accessibility error". Here i was forced to make one small change and swop "internal class SmallXmlParser" for "public class SmallXmlParser". If anyone wants to see my final solution then let me know. I will be posting the animation class up soon that uses it.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
XML parser, how to get to 1 of many elements with the same name. 1 Answer
XML parsing and create UI 0 Answers
XML SelectNodes 0 Answers
Serializing and Deserializing from/to XML inside Resources folder 1 Answer