UnityでXMLを扱う
前回のjsonに引き続きXMLの読み込みのテスト。
この辺はFlashと同じような扱いでできた。
XMLをDOMとして扱うのであればこれでいいんだけど、SAXとして扱う場合はXmlReader.Create()を使うと思う…けどよく分からなかったけどこの辺が参考になるのかしら。
DOMのサンプル。
<?xml version="1.0" encoding="UTF-8"?> <persons> <person id="1"> <name><![CDATA[John]]></name> <age>23</age> <married>true</married> </person> <person id="2"> <name><![CDATA[Bob]]></name> <age>32</age> <married>true</married> </person> </persons>
using UnityEngine;
using System.Collections;
using System.Xml;
public class XMLTest : MonoBehaviour {
public void Start() {
TextAsset xmlTextAsset = Instantiate(Resources.Load("persons")) as TextAsset;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlTextAsset.text);
XmlNodeList nodes = xmlDoc.GetElementsByTagName("person");
foreach (XmlNode node in nodes) {
Debug.Log("id : " + node.Attributes.GetNamedItem("id").Value);
XmlNode childNode = node.FirstChild;
int count = 0;
do {
if (++count > 10) break;
Debug.Log(childNode.Name + " : " + childNode.FirstChild.Value);
} while ((childNode = childNode.NextSibling) != null);
}
}
}
[…] naochang.me naochang | […]