【原创】Unity3D 游戏项目实例教程之 Xml 数据解析器

由于自己项目中使用 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<LoaderResourceModel> 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<LoaderResourceModel> ();
		
		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;

	/// <summary>
	/// 加载 XML
	/// </summary>
	/// <param name="xmlText">Xml text.</param>
	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 = "";
		}
	}

	/// <summary>
	/// 解析类
	/// </summary>
	/// <returns>The xml analysis.</returns>
	public virtual XmlAnalysis GetXmlAnalysis()
	{
		return null;
	}

	/// <summary>
	/// 获取数据,
	/// </summary>
	/// <param name="xmlElement">Xml element.</param>
	/// <param name="xmlKey">Xml key.</param>
	/// <param name="xmlValue">Xml value.</param>
	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);
	}

	/// <summary>
	/// 随机获取一条数据
	/// </summary>
	/// <returns>The analysis.</returns>
	/// <param name="xmlElement">Xml element.</param>
	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);
	}

	/// <summary>
	/// 读取所有数据
	/// </summary>
	/// <returns>The analysis.</returns>
	/// <param name="xmlElement">Xml element.</param>
	/// <param name="xmlKey">Xml key.</param>
	public virtual IList<object> AllAnalysis(string xmlElement, string xmlKey)
	{
		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);

		IList<object> resultList = new List<object> ();

		foreach (XmlNode xmlNode in xmlNodeList)
		{
			resultList.Add(xmlAnalysis.Analysis(xmlNode, string.Format("//{0}[@{1}=\"{2}\"]", xmlElement, xmlKey, xmlNode.Attributes[xmlKey].Value)));
		}

		return resultList;
	}
}

下载地址: 链接: http://pan.baidu.com/s/1ntsw9W9 密码: 7gi5

转载于:https://my.oschina.net/wangjiajun/blog/484146

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值