XML数据解析传送

这篇博客介绍如何在Unity中解析XML文件并显示数据。通过使用XMLParser类解析XML文本,然后利用得到的数据实例化游戏对象,并设置其子组件的文本和图片属性,展示了如何将XML数据应用到游戏场景中。
摘要由CSDN通过智能技术生成

 xml简单的方法数据解析

Script1

using System.Collections;  
  
public class XMLNode: Hashtable  
{  
    public XMLNodeList GetNodeList(string path)  
    {  
        return GetObject(path) as XMLNodeList;  
    }  
      
    public XMLNode GetNode(string path)  
    {  
        return GetObject(path) as XMLNode;  
    }  
      
    public string GetValue(string path)  
    {  
        return GetObject(path) as string;  
    }  
      
    private object GetObject(string path)  
    {  
        string[] bits = path.Split('>');  
        XMLNode currentNode = this;  
        XMLNodeList currentNodeList = null;  
        bool listMode = false;  
        object ob;  
          
        for (int i = 0; i < bits.Length; i++)  
        {  
             if (listMode)  
             {  
                currentNode = (XMLNode)currentNodeList[int.Parse(bits[i])];  
                ob = currentNode;  
                listMode = false;  
             }  
             else  
             {  
                ob = currentNode[bits[i]];  
                  
                if (ob is ArrayList)  
                {  
                    currentNodeList = (XMLNodeList)(ob as ArrayList);  
                    listMode = true;  
                }  
                else  
                {  
                    // reached a leaf node/attribute  
                    if (i != (bits.Length - 1))  
                    {  
                        // unexpected leaf node  
                        string actualPath = "";  
                        for (int j = 0; j <= i; j++)  
                        {  
                            actualPath = actualPath + ">" + bits[j];  
                        }  
                          
                        //Debug.Log("xml path search truncated. Wanted: " + path + " got: " + actualPath);  
                    }  
                      
                    return ob;  
                }  
             }  
        }  
          
        if (listMode)   
            return currentNodeList;  
        else   
            return currentNode;  
    }  
}



Script2

using System.Collections;  
  
public class XMLNodeList: ArrayList   
{  
    public XMLNode Pop()  
    {  
        XMLNode item = null;  
      
        item = (XMLNode)this[this.Count - 1];  
        this.Remove(item);  
          
        return item;  
    }  
      
    public int Push(XMLNode item)  
    {  
        Add(item);  
          
        return this.Count;  
    }  
}



Script3

/* 
 * UnityScript Lightweight XML Parser 
 * by Fraser McCormick (unityscripts@roguishness.com) 
 * http://twitter.com/flimgoblin 
 * http://www.roguishness.com/unity/ 
 * 
 * You may use this script under the terms of either the MIT License  
 * or the Gnu Lesser General Public License (LGPL) Version 3.  
 * See: 
 * http://www.roguishness.com/unity/lgpl-3.0-standalone.html 
 * http://www.roguishness.com/unity/gpl-3.0-standalone.html 
 * or 
 * http://www.roguishness.com/unity/MIT-license.txt 
 */  
    
/* Usage: 
 * parser=new XMLParser(); 
 * var node=parser.Parse("<example><value type=\"String\">Foobar</value><value type=\"Int\">3</value></example>"); 
 *  
 * Nodes are Boo.Lang.Hash values with te

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值