Unity XML 操作


首先注意头文件:

using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 
using System.Xml;  
using System.IO; 
using System.Text; 

1、生成XML:

public void createXml() 
{ 
    //xml保存的路径,这里放在Assets路径 注意路径。 
    string filepath = Application.dataPath + @"/my.xml"; 
     //继续判断当前路径下是否有该文件 
    if(!File.Exists (filepath)) 
    { 
        //创建XML文档实例 
         XmlDocument xmlDoc = new XmlDocument(); 
          //创建root节点,也就是最上一层节点 
         XmlElement root = xmlDoc.CreateElement("transforms"); 
          //继续创建下一层节点 
         XmlElement elmNew = xmlDoc.CreateElement("rotation"); 
         //设置节点的两个属性 ID 和 NAME 
         elmNew.SetAttribute("id","0"); 
         elmNew.SetAttribute("name","momo"); 
         //继续创建下一层节点 
           XmlElement rotation_X = xmlDoc.CreateElement("x"); 
         //设置节点中的数值 
         rotation_X.InnerText = "0"; 
         XmlElement rotation_Y = xmlDoc.CreateElement("y"); 
         rotation_Y.InnerText = "1"; 
          XmlElement rotation_Z = xmlDoc.CreateElement("z"); 
	  rotation_Z.InnerText = "2"; 
         //这里在添加一个节点属性,用来区分。。 
          rotation_Z.SetAttribute("id","1"); 
      
         //把节点一层一层的添加至XMLDoc中 ,请仔细看它们之间的先后顺序,这将是生成XML文件的顺序 
          elmNew.AppendChild(rotation_X); 
          elmNew.AppendChild(rotation_Y); 
          elmNew.AppendChild(rotation_Z); 
          root.AppendChild(elmNew); 
         xmlDoc.AppendChild(root); 
         //把XML文件保存至本地 
         xmlDoc.Save(filepath); 
         Debug.Log("createXml OK!"); 
    } 
} 


运行结果:

<transforms>  
  <rotation id="0" name="momo">  
    <x>0</x> 
    <y>1</y> 
    <z id="1">2</z> 
  </rotation>  
</transforms> 



2.更新XML文件:

以其中某个节点名称做条件,当查询到时更新该节点

public void UpdateXml() 
{ 
    string filepath = Application.dataPath + @"/my.xml"; 
    if(File.Exists (filepath)) 
    { 
         XmlDocument xmlDoc = new XmlDocument(); 
                     //根据路径将XML读取出来 
         xmlDoc.Load(filepath); 
                     //得到transforms下的所有子节点 
         XmlNodeList nodeList=xmlDoc.SelectSingleNode("transforms").ChildNodes; 
                     //遍历所有子节点 
         foreach(XmlElement xe in nodeList) 
         { 
            	//拿到节点中属性ID =0的节点 
            if(xe.GetAttribute("id")=="0") 
            { 
                //更新节点属性 
                xe.SetAttribute("id","1000"); 
                //继续遍历 
                foreach(XmlElement x1 in xe.ChildNodes) 
                { 
                    if(x1.Name=="z") 
                    { 
                    //这里是修改节点名称对应的数值,而上面的拿到节点连带的属性。。。 
                         x1.InnerText="update00000"; 
                    } 
                } 
                break; 
            } 
         } 
         xmlDoc.Save(filepath); 
         Debug.Log("UpdateXml OK!"); 
    } 
} 

运行结果:

<transforms> 
  <rotation id="1000" name="momo"> 
    <x>0</x> 
    <y>1</y> 
    <z id="1">update00000</z> 
  </rotation> 
</transforms> 



3.添加XML:

public void AddXml() 
{ 
    string filepath = Application.dataPath + @"/my.xml"; 
    if(File.Exists (filepath)) 
    { 
        XmlDocument xmlDoc = new XmlDocument(); 
        xmlDoc.Load(filepath); 
        XmlNode root = xmlDoc.SelectSingleNode("transforms"); 
        XmlElement elmNew = xmlDoc.CreateElement("rotation"); 
        elmNew.SetAttribute("id","1"); 
        elmNew.SetAttribute("name","yusong"); 
         XmlElement rotation_X = xmlDoc.CreateElement("x"); 
         rotation_X.InnerText = "0"; 
         rotation_X.SetAttribute("id","1"); 
         XmlElement rotation_Y = xmlDoc.CreateElement("y"); 
         rotation_Y.InnerText = "1"; 
         XmlElement rotation_Z = xmlDoc.CreateElement("z"); 
         rotation_Z.InnerText = "2"; 
         elmNew.AppendChild(rotation_X); 
         elmNew.AppendChild(rotation_Y); 
         elmNew.AppendChild(rotation_Z); 
         root.AppendChild(elmNew); 
         xmlDoc.AppendChild(root); 
         xmlDoc.Save(filepath); 
         Debug.Log("AddXml OK!"); 
    } 
} 


运行结果:

<transforms> 
  <rotation id="1000" name="momo"> 
    <x>0</x> 
    <y>1</y> 
    <z id="1">update00000</z> 
  </rotation> 
  <rotation id="1" name="yusong"> 
    <x id="1">0</x> 
    <y>1</y> 
    <z>2</z> 
  </rotation> 
</transforms> 



4.删除XML:

public void deleteXml() 
{ 
    string filepath = Application.dataPath + @"/my.xml"; 
    if(File.Exists (filepath)) 
    { 
         XmlDocument xmlDoc = new XmlDocument(); 
         xmlDoc.Load(filepath); 
         XmlNodeList nodeList=xmlDoc.SelectSingleNode("transforms").ChildNodes; 
         foreach(XmlElement xe in nodeList) 
         { 
            if(xe.GetAttribute("id")=="1") 
            { 
                xe.RemoveAttribute("id"); 
            } 
            foreach(XmlElement x1 in xe.ChildNodes) 
            { 
                if(x1.Name == "z") 
                { 
                    x1.RemoveAll(); 
                } 
            } 
         } 
         xmlDoc.Save(filepath); 
         Debug.Log("deleteXml OK!"); 
    }  
}


运行结果:

<transforms> 
  <rotation id="1000" name="momo"> 
    <x>0</x> 
    <y>1</y>  
    <z> 
    </z> 
  </rotation> 
  <rotation name="yusong"> 
    <x id="1">0</x> 
    <y>1</y>  
    <z> 
    </z> 
  </rotation> 
</transforms> 



5.解析与输出上面的XML:

public void showXml() 
{ 
    string filepath = Application.dataPath + @"/my.xml";  
    if(File.Exists (filepath)) 
    { 
         XmlDocument xmlDoc = new XmlDocument(); 
         xmlDoc.Load(filepath); 
         XmlNodeList nodeList=xmlDoc.SelectSingleNode("transforms").ChildNodes; 
        //遍历每一个节点,拿节点的属性以及节点的内容 
         foreach(XmlElement xe in nodeList) 
         { 
         Debug.Log("Attribute :" + xe.GetAttribute("name")); 
            Debug.Log("NAME :" + xe.Name); 
            foreach(XmlElement x1 in xe.ChildNodes) 
         { 
                if(x1.Name == "y") 
                { 
                   Debug.Log("VALUE :" + x1.InnerText);  
                } 
        }  
         } 
         Debug.Log("all = " + xmlDoc.OuterXml); 
    } 
}


原文:http://www.jb51.cc/article/p-oynnjezt-mo.html




版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇