[Unity基础]xml在unity中的使用

前端之家收集整理的这篇文章主要介绍了[Unity基础]xml在unity中的使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

原文链接:http://www.xuanyusong.com/archives/1901


XmlDocument:CreateElement(创建节点)、AppendChild(添加节点,使被添加的节点成为子节点)、Save(保存xml文件)、Load(读取xml文件)、

SelectSingleNode(拿到特定名称的节点)


XmlElement:SetAttribute(设置节点属性)、InnerText(设置节点内容)、AppendChild(添加节点,使被添加的节点成为子节点)、

GetAttribute(拿到节点属性)、RemoveAttribute、RemoveAll


通用:ChildNodes(拿到所有子节点,但不包含子节点里的子节点)


0.引用头文件

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Xml;
  5. using System.IO;
  6. using System.Text;
  7. using LitJson;

1.生成xml
  1. public void createXml()
  2. {
  3. //xml保存的路径,这里放在Assets路径 注意路径。
  4. string filepath = Application.dataPath + @"/my.xml";
  5. //继续判断当前路径下是否有该文件
  6. if(!File.Exists (filepath))
  7. {
  8. //创建XML文档实例
  9. XmlDocument xmlDoc = new XmlDocument();
  10. //创建root节点,也就是最上一层节点
  11. XmlElement root = xmlDoc.CreateElement("transforms");
  12. //继续创建下一层节点
  13. XmlElement elmNew = xmlDoc.CreateElement("rotation");
  14. //设置节点的两个属性 ID 和 NAME
  15. elmNew.SetAttribute("id","0");
  16. elmNew.SetAttribute("name","momo");
  17. //继续创建下一层节点
  18. XmlElement rotation_X = xmlDoc.CreateElement("x");
  19. //设置节点中的数值
  20. rotation_X.InnerText = "0";
  21. XmlElement rotation_Y = xmlDoc.CreateElement("y");
  22. rotation_Y.InnerText = "1";
  23. XmlElement rotation_Z = xmlDoc.CreateElement("z");
  24. rotation_Z.InnerText = "2";
  25. //这里在添加一个节点属性,用来区分。。
  26. rotation_Z.SetAttribute("id","1");
  27. //把节点一层一层的添加至XMLDoc中 ,请仔细看它们之间的先后顺序,这将是生成XML文件的顺序
  28. elmNew.AppendChild(rotation_X);
  29. elmNew.AppendChild(rotation_Y);
  30. elmNew.AppendChild(rotation_Z);
  31. root.AppendChild(elmNew);
  32. xmlDoc.AppendChild(root);
  33. //把XML文件保存至本地
  34. xmlDoc.Save(filepath);
  35. Debug.Log("createXml OK!");
  36. }
  37. }


运行结果
  1. <transforms>
  2. <rotation id="0" name="momo">
  3. <x>0</x>
  4. <y>1</y>
  5. <z id="1">2</z>
  6. </rotation>
  7. </transforms>

2.更新xml文件
  1. public void UpdateXml()
  2. {
  3. string filepath = Application.dataPath + @"/my.xml";
  4. if(File.Exists (filepath))
  5. {
  6. XmlDocument xmlDoc = new XmlDocument();
  7. //根据路径将XML读取出来
  8. xmlDoc.Load(filepath);
  9. //得到transforms下的所有子节点
  10. XmlNodeList nodeList=xmlDoc.SelectSingleNode("transforms").ChildNodes;
  11. //遍历所有子节点
  12. foreach(XmlElement xe in nodeList)
  13. {
  14. //拿到节点中属性ID =0的节点
  15. if(xe.GetAttribute("id")=="0")
  16. {
  17. //更新节点属性
  18. xe.SetAttribute("id","1000");
  19. //继续遍历
  20. foreach(XmlElement x1 in xe.ChildNodes)
  21. {
  22. if(x1.Name=="z")
  23. {
  24. //这里是修改节点名称对应的数值,而上面的拿到节点连带的属性。。。
  25. x1.InnerText="update00000";
  26. }
  27. }
  28. break;
  29. }
  30. }
  31. xmlDoc.Save(filepath);
  32. Debug.Log("UpdateXml OK!");
  33. }
  34. }


运行结果
  1. <transforms>
  2. <rotation id="1000" name="momo">
  3. <x>0</x>
  4. <y>1</y>
  5. <z id="1">update00000</z>
  6. </rotation>
  7. </transforms>

3.添加xml
  1. public void AddXml()
  2. {
  3. string filepath = Application.dataPath + @"/my.xml";
  4. if(File.Exists (filepath))
  5. {
  6. XmlDocument xmlDoc = new XmlDocument();
  7. xmlDoc.Load(filepath);
  8. XmlNode root = xmlDoc.SelectSingleNode("transforms");
  9. XmlElement elmNew = xmlDoc.CreateElement("rotation");
  10. elmNew.SetAttribute("id","1");
  11. elmNew.SetAttribute("name","yusong");
  12. XmlElement rotation_X = xmlDoc.CreateElement("x");
  13. rotation_X.InnerText = "0";
  14. rotation_X.SetAttribute("id","1");
  15. XmlElement rotation_Y = xmlDoc.CreateElement("y");
  16. rotation_Y.InnerText = "1";
  17. XmlElement rotation_Z = xmlDoc.CreateElement("z");
  18. rotation_Z.InnerText = "2";
  19. elmNew.AppendChild(rotation_X);
  20. elmNew.AppendChild(rotation_Y);
  21. elmNew.AppendChild(rotation_Z);
  22. root.AppendChild(elmNew);
  23. xmlDoc.AppendChild(root);
  24. xmlDoc.Save(filepath);
  25. Debug.Log("AddXml OK!");
  26. }
  27. }

运行结果
  1. <transforms>
  2. <rotation id="1000" name="momo">
  3. <x>0</x>
  4. <y>1</y>
  5. <z id="1">update00000</z>
  6. </rotation>
  7. <rotation id="1" name="yusong">
  8. <x id="1">0</x>
  9. <y>1</y>
  10. <z>2</z>
  11. </rotation>
  12. </transforms>

4.删除xml
  1. public void deleteXml()
  2. {
  3. string filepath = Application.dataPath + @"/my.xml";
  4. if(File.Exists (filepath))
  5. {
  6. XmlDocument xmlDoc = new XmlDocument();
  7. xmlDoc.Load(filepath);
  8. XmlNodeList nodeList=xmlDoc.SelectSingleNode("transforms").ChildNodes;
  9. foreach(XmlElement xe in nodeList)
  10. {
  11. if(xe.GetAttribute("id")=="1")
  12. {
  13. xe.RemoveAttribute("id");
  14. }
  15. foreach(XmlElement x1 in xe.ChildNodes)
  16. {
  17. if(x1.Name == "z")
  18. {
  19. x1.RemoveAll();
  20. }
  21. }
  22. }
  23. xmlDoc.Save(filepath);
  24. Debug.Log("deleteXml OK!");
  25. }
  26. }

运行结果
  1. <transforms>
  2. <rotation id="1000" name="momo">
  3. <x>0</x>
  4. <y>1</y>
  5. <z>
  6. </z>
  7. </rotation>
  8. <rotation name="yusong">
  9. <x id="1">0</x>
  10. <y>1</y>
  11. <z>
  12. </z>
  13. </rotation>
  14. </transforms>

猜你在找的XML相关文章