使用Mono.xml解析xml表
来源:互联网 发布:lol英雄详细数据 编辑:程序博客网 时间:2024/04/26 11:27
本文章使用Mono.xml进行2次封装的xml解析工具类,可以解析常用的数据类型,比如int,string,float,double;数据结构,一维数组,2维数组,List等。
using UnityEngine;using System.Collections;using Mono.Xml;using System.Security;using System.Collections.Generic;public abstract class XmlUtil { private SecurityElement se = null; private XMLNode node = null; public abstract void Parse(); public void LoadXml(string filePath) { string path = "";//xml表保存的路径 string fileUrl = path + filePath; string data = Resources.Load(fileUrl).ToString(); Read(data); } /// <summary> /// 获取根节点 /// </summary> /// <returns></returns> public XMLNode getXMLNode() { return node; } public static string ParseToString(XMLNode node,string attrName) { string value = node.se.Attribute(attrName); if(!string.IsNullOrEmpty(value)) { return value; } else { Debug.Log("没有属性:" + attrName); return null; } } public static int ParseToInt(XMLNode node, string attrName) { string str = node.se.Attribute(attrName); int value = -1; if(!string.IsNullOrEmpty(str)) { if (int.TryParse(str, out value)) { return value; } else { Debug.Log("属性:" + attrName + ",不能强转int类型"); return -1; } } else { Debug.Log("没有属性:" + attrName); return -1; } } public static double ParseToDouble(XMLNode node, string attrName) { string str = node.se.Attribute(attrName); double value = -1.0; if (!string.IsNullOrEmpty(str)) { if (double.TryParse(str, out value)) { return value; } else { Debug.Log("属性:" + attrName + ",不能强转double类型"); return -1.0; } } else { Debug.Log("没有属性:" + attrName); return -1.0; } } public static float ParseToFloat(XMLNode node, string attrName) { string str = node.se.Attribute(attrName); float value = -1.0f; if (!string.IsNullOrEmpty(str)) { if (float.TryParse(str, out value)) { return value; } else { Debug.Log("属性:" + attrName + ",不能强转float类型"); return -1.0f; } } else { Debug.Log("没有属性:" + attrName); return -1.0f; } } public static List<int> ParseToIntList(XMLNode node, string attrName,char split = ',') { string str = node.se.Attribute(attrName); List<int> list = new List<int>(); int value = 0; if (!string.IsNullOrEmpty(str)) { string[] strArray = str.Split(split); for (int i = 0; i < strArray.Length; i++) { if(int.TryParse(strArray[i],out value)) { list.Add(value); } else { Debug.LogError("属性:" + attrName + "不能强转List<int>"); return null; } } return list; } else { Debug.Log("没有属性:" + attrName); return null; } } public static List<double> ParseToDoubleList(XMLNode node, string attrName, char split = ',') { string str = node.se.Attribute(attrName); List<double> list = new List<double>(); double value = 0; if (!string.IsNullOrEmpty(str)) { string[] strArray = str.Split(split); for (int i = 0; i < strArray.Length; i++) { if (double.TryParse(strArray[i], out value)) { list.Add(value); } else { Debug.LogError("属性:" + attrName + "不能强转List<int>"); return null; } } return list; } else { Debug.Log("没有属性:" + attrName); return null; } } public static List<float> ParseToFloatList(XMLNode node, string attrName, char split = ',') { string str = node.se.Attribute(attrName); List<float> list = new List<float>(); float value = 0; if (!string.IsNullOrEmpty(str)) { string[] strArray = str.Split(split); for (int i = 0; i < strArray.Length; i++) { if (float.TryParse(strArray[i], out value)) { list.Add(value); } else { Debug.LogError("属性:" + attrName + "不能强转List<float>"); return null; } } return list; } else { Debug.Log("没有属性:" + attrName); return null; } } public static int[] ParseToIntArray(XMLNode node, string attrName, char split = ',') { List<int> list = ParseToIntList(node, attrName, split); if(list != null) { int[] array = list.ToArray(); return array; } else { Debug.Log("属性:" + attrName + "不能强转为int[]类型"); return null; } } public static double[] ParseToDoubleArray(XMLNode node, string attrName, char split = ',') { List<double> list = ParseToDoubleList(node, attrName, split); if (list != null) { double[] array = list.ToArray(); return array; } else { Debug.Log("属性:" + attrName + "不能强转为double[]类型"); return null; } } public static float[] ParseToFloatArray(XMLNode node, string attrName, char split = ',') { List<float> list = ParseToFloatList(node, attrName, split); if (list != null) { float[] array = list.ToArray(); return array; } else { Debug.Log("属性:" + attrName + "不能强转为float[]类型"); return null; } } public static int[,] ParseToIntArray2D(XMLNode node, string attrName, char split1 = ';', char split2 = ',') { string str = node.se.Attribute(attrName); int[,] array2D = null; if (!string.IsNullOrEmpty(str)) { string[] str1 = str.Split(split1); int value = 0; for (int i = 0; i < str1.Length; i++) { string[] str2 = str1[i].Split(split2); if(array2D == null) { array2D = new int[str1.Length, str2.Length]; } for (int j = 0; j < str2.Length; j++) { if (int.TryParse(str2[j], out value)) { array2D[i, j] = value; } else { Debug.Log("属性:" + attrName + "不能强转为int[,]类型"); return null; } } } return array2D; } else { Debug.Log("没有属性:" + attrName); return null; } } public static double[,] ParseToDoubleArray2D(XMLNode node, string attrName, char split1 = ';', char split2 = ',') { string str = node.se.Attribute(attrName); double[,] array2D = null; if (!string.IsNullOrEmpty(str)) { string[] str1 = str.Split(split1); double value = 0; for (int i = 0; i < str1.Length; i++) { string[] str2 = str1[i].Split(split2); if (array2D == null) { array2D = new double[str1.Length, str2.Length]; } for (int j = 0; j < str2.Length; j++) { if (double.TryParse(str2[j], out value)) { array2D[i, j] = value; } else { Debug.Log("属性:" + attrName + "不能强转为double[,]类型"); return null; } } } return array2D; } else { Debug.Log("没有属性:" + attrName); return null; } } public static float[,] ParseToFloatArray2D(XMLNode node, string attrName, char split1 = ';', char split2 = ',') { string str = node.se.Attribute(attrName); float[,] array2D = null; if (!string.IsNullOrEmpty(str)) { string[] str1 = str.Split(split1); float value = 0; for (int i = 0; i < str1.Length; i++) { string[] str2 = str1[i].Split(split2); if (array2D == null) { array2D = new float[str1.Length, str2.Length]; } for (int j = 0; j < str2.Length; j++) { if (float.TryParse(str2[j], out value)) { array2D[i, j] = value; } else { Debug.Log("属性:" + attrName + "不能强转为float[,]类型"); return null; } } } return array2D; } else { Debug.Log("没有属性:" + attrName); return null; } } void Read(string data) { SecurityParser sp = new SecurityParser(); sp.LoadXml(data); SecurityElement se = sp.ToXml(); this.se = se; node = new XMLNode(); node.se = this.se; }}
下面的类用于保存xml的节点
using UnityEngine;using System.Collections;using Mono.Xml;using System.Security;using System.Collections.Generic;public class XMLNode { public SecurityElement se = null; /// <summary> /// 根据Tag获取xml元素 /// </summary> /// <param name="tagName">元素名</param> /// <returns></returns> public List<XMLNode> GetNodesByName(string tagName) { // XMLNode node = new XMLNode(); List<XMLNode> nodes = new List<XMLNode>(); foreach (SecurityElement item in se.Children) { if (item.Tag == tagName) { XMLNode node = new XMLNode(); node.se = item; nodes.Add(node); } } if (nodes.Count > 0) { return nodes; } else { Debug.Log("没有找到标签:" + tagName); return null; } }}Mono.xml下载:
下载地址: http://download.csdn.net/detail/cwqcwk1/7105071
1 0
- 使用Mono.xml解析xml表
- dotnet(C#/mono)轻量级XML解析库Mono.Xml源码
- unity3d使用Mono.Xml读取xml
- unity3d使用Mono.Xml读取xml
- unity3d使用Mono.Xml读取xml
- 使用Unity3d官方提供得精简版xml解析脚本Mono.xml
- Unity3D Mono.Xml和XmlParser的使用
- 在Unity3D中使用Mono.XML读取XML文件
- XML解析一 使用SAX解析XML
- XML解析二 使用DOM解析XML
- XML解析三 使用PULL解析XML
- 使用JSR172解析XML
- 使用Jdom解析XML
- 使用 dom4j 解析 XML
- 使用SAX解析XML
- 使用 dom4j 解析 XML
- 使用Libxml2解析xml
- 使用 dom4j 解析 XML
- 文章标题
- Redis之——主从配置详解
- GPLT L2-008. 最长对称子串
- Failure [INSTALL_FAILED_INSUFFICIENT_STORAGE]
- 漫步数理统计七——随机变量(上)
- 使用Mono.xml解析xml表
- HDUoj 4278 Faulty Odometer (数学
- 科锐课堂笔记:2017/3/13 宏与预处理指令
- Gson 解析Json数据
- 图像绕某点旋转的矩阵的确定(getRotationMatrix2D函数解析)
- MySQL5.6.11安装步骤
- 谷歌自动驾驶再创纪录!够跑300年!
- 你肯定不知道的设计心理学原则和定律
- 递归工作原理