由于自己项目中使用 XML 作为配置文件,为了解析起来方便,其实更加方便的是解析 XML 的 .cs 文件都通过程序去生成,读者可以自己去实现,下面是 XML 解析器,仅供大家参考!
先来看看最终使用例子的代码(在我们的项目中,我们需要借助上一篇的资源加载管理器来预先加载我们的配置文件):
using UnityEngine;
using System.Collections;
public class LoaderXml : MonoBehaviour
{
void Awake()
{
// 可以使用 WwwLoaderManager,加载 XML,然后使用 WwwDataManager 获取到 XML 文件内容
string xmlText = "";
// 初始化加载解析器
LoaderDocumentAnalysis xmlDocumentAnalysis = new LoaderDocumentAnalysis (xmlText);
// 获取解析数据
LoaderModel loaderModel = (LoaderModel)xmlDocumentAnalysis.Analysis ("loader", "loaderID", "UiLoader");
// 输出解析结果
Debug.Log (loaderModel.loaderPath);
}
}
LoaderModel.cs
using UnityEngine;
using System.Collections.Generic;
public class LoaderModel
{
public string loaderID;
public string loaderPath;
public IList resourceList;
}
LoaderResourceModel.cs
using UnityEngine;
using System.Collections;
public class LoaderResourceModel
{
public string resourceID;
public string resourcePath;
}
LoaderAnalysis.cs
using UnityEngine;
using System.Collections.Generic;
using System.Xml;
public class LoaderAnalysis : XmlAnalysis
{
public override object Analysis (System.Xml.XmlNode xmlNode, string xmlPath)
{
LoaderModel loaderModel = new LoaderModel ();
loaderModel.loaderID = xmlNode.Attributes ["loaderID"].Value;
loaderModel.loaderPath = xmlNode.Attributes ["loaderPath"].Value;
loaderModel.resourceList = new List ();
string pathFormat = string.Format (xmlPath + "/resources/resource");
XmlNodeList nodeList = xmlNode.SelectNodes(pathFormat);
foreach (XmlNode itemNode in nodeList)
{
LoaderResourceModel itemModel = new LoaderResourceModel();
itemModel.resourceID = itemNode.Attributes["resourceID"].Value;
itemModel.resourcePath = itemNode.Attributes["resourcePath"].Value;
loaderModel.resourceList.Add(itemModel);
}
return loaderModel;
}
}
LoaderDocumentAnalysis.cs
using UnityEngine;
using System.Collections;
public class LoaderDocumentAnalysis : XmlDocumentAnalysis
{
public LoaderDocumentAnalysis(string xmlText):base(xmlText) {}
public override XmlAnalysis GetXmlAnalysis ()
{
return new LoaderAnalysis();
}
}
下面是我们的 XML 数据解析器的类库,很简单,只有两个类,实现基本功能来让子类重写解析各自的模块。
XmlAnalysis.cs
using UnityEngine;
using System.Xml;
public class XmlAnalysis
{
public virtual object Analysis(XmlNode xmlNode, string xmlPath)
{
return null;
}
}
XmlDocumentAnalysis.cs
using UnityEngine;
using System.Collections.Generic;
using System.Xml;
public class XmlDocumentAnalysis
{
protected string xmlText;
protected XmlDocument xmlDocument;
protected XmlElement xmlElement;
///
/// 加载 XML
///
/// Xml text.
public XmlDocumentAnalysis(string xmlText)
{
this.xmlText = xmlText;
if (this.xmlDocument == null && !string.IsNullOrEmpty(xmlText))
{
this.xmlDocument = new XmlDocument();
this.xmlDocument.LoadXml(this.xmlText);
this.xmlElement = this.xmlDocument.DocumentElement;
this.xmlText = "";
}
}
///
/// 解析类
///
/// The xml analysis.
public virtual XmlAnalysis GetXmlAnalysis()
{
return null;
}
///
/// 获取数据,
///
/// Xml element.
/// Xml key.
/// Xml value.
public virtual object Analysis(string xmlElement, string xmlKey, string xmlValue)
{
if (this.xmlElement == null) return null;
XmlAnalysis xmlAnalysis = this.GetXmlAnalysis ();
if (xmlAnalysis == null) return null;
string pathFormat = string.Format("//{0}[@{1}=\"{2}\"]", xmlElement, xmlKey, xmlValue);
XmlNode xmlNode = this.xmlElement.SelectSingleNode (pathFormat);
return xmlAnalysis.Analysis(xmlNode, pathFormat);
}
///
/// 随机获取一条数据
///
/// The analysis.
/// Xml element.
public virtual object RangeAnalysis(string xmlElement)
{
if (this.xmlElement == null) return null;
XmlAnalysis xmlAnalysis = this.GetXmlAnalysis ();
if (xmlAnalysis == null) return null;
string pathFormat = string.Format("//{0}", xmlElement);
XmlNodeList xmlNodeList = this.xmlElement.SelectNodes (pathFormat);
int randomIndex = Random.Range (0, xmlNodeList.Count - 1);
return xmlAnalysis.Analysis (xmlNodeList[randomIndex], pathFormat);
}
///
/// 读取所有数据
///
/// The analysis.
/// Xml element.
/// Xml key.
public virtual IList
下载地址: 链接: http://pan.baidu.com/s/1ntsw9W9 密码: 7gi5