使用Mono.xml解析xml表

本文章使用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下载:



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值