加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

XML的使用,增删改查(Unity中的)

发布时间:2020-12-16 05:58:30 所属栏目:百科 来源:网络整理
导读:创建之后的XML root_CharacterTmp CharacterTmp id="1000" name="xml" JobID2/JobID JobModenone/JobMode InitForce2.2/InitForce /CharacterTmp CharacterTmp name="name1" JobID /JobID JobModenone/JobMode InitForce2.2/InitForce /CharacterTmp/root_Ch

创建之后的XML

<root_CharacterTmp>
  <CharacterTmp id="1000" name="xml">
    <JobID>2</JobID>
    <JobMode>none</JobMode>
    <InitForce>2.2</InitForce>
  </CharacterTmp>
  <CharacterTmp name="name1">
    <JobID>
    </JobID>
    <JobMode>none</JobMode>
    <InitForce>2.2</InitForce>
  </CharacterTmp>
</root_CharacterTmp>
创建代码:


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

public class XmlTest : MonoBehaviour {

	string filePath;
	int id;
	int jobID;
	string jobMode;
	float initForce;

	void Start () {
		filePath = Application.dataPath + "/Test.xml";
	}

	void OnGUI()
	{
		if (GUI.Button (new Rect (10,10,200,30),"CREATE XML"))
			CreateXMl ();
		if (GUI.Button (new Rect (10,50,"UpDate XML"))
			UpDateXml ();
		if (GUI.Button (new Rect (10,90,"Add XML"))
			AddXml ();
		if (GUI.Button (new Rect (10,130,"Delete XML"))
			DeleteXml ();
		if (GUI.Button (new Rect (10,170,"Delete XML"))
			ShowXml ();

		GUILayout.Label ("id:" + id);

	}

	//创建XML
	public void CreateXMl()
	{
		//检测xml是否存在
		if(!File.Exists(filePath))
		{
			//新建XML实例
			XmlDocument xmlDoc = new XmlDocument();
			//创建根节点
			XmlElement root = 
				xmlDoc.CreateElement("root_CharacterTmp");
			//创建下一层节点
			XmlElement elmNew = 
				xmlDoc.CreateElement("CharacterTmp");
			//设置属性
			elmNew.SetAttribute("id","0");
			elmNew.SetAttribute("name","xml");
			//继续创建下一层节点
			XmlElement jobid = 
				xmlDoc.CreateElement("JobID");
			//设置节点的值
			jobid.InnerText = "1";
			XmlElement jobMode = 
				xmlDoc.CreateElement("JobMode");
			jobMode.InnerText = "none";
			XmlElement initForce = 
				xmlDoc.CreateElement("InitForce");
			initForce.InnerText = "0";
			//吧节点一层一层的添加
			elmNew.AppendChild(jobid);
			elmNew.AppendChild(jobMode);
			elmNew.AppendChild(initForce);
			root.AppendChild(elmNew);
			xmlDoc.AppendChild(root);
			xmlDoc.Save(filePath);
			Debug.Log("createXml ok!");
		}
	}

	//更新XML
	public void UpDateXml()
	{
		//检测xml是否存在
		if(File.Exists(filePath))
		{
			//新建实例
			XmlDocument xmlDoc = new XmlDocument();
			//根据路径将xml读取出来
			xmlDoc.Load(filePath);
			//得到根节点
			XmlNodeList nodeList = 
				xmlDoc.SelectSingleNode
					("root_CharacterTmp").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 == "JobID")
						{
							//更新值
							x1.InnerText = "2";
						}
					}

				}
			}
			xmlDoc.Save(filePath);
			Debug.Log("UpDateXML OK!");
		}
	}

	//添加xml
	public void AddXml()
	{
		if(File.Exists(filePath))
		{
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(filePath);
			//添加根节点
			XmlNode root = 
				xmlDoc.SelectSingleNode
					("root_CharacterTmp");
			//添加
			XmlElement elmNew = 
				xmlDoc.CreateElement("CharacterTmp");
			elmNew.SetAttribute("id","1");
			elmNew.SetAttribute("name","name1");
			XmlElement jobid = 
				xmlDoc.CreateElement("JobID");
			jobid.InnerText = "1";
			elmNew.AppendChild(jobid);
			XmlElement jobMode = 
				xmlDoc.CreateElement("JobMode");
			jobMode.InnerText = "none";
			elmNew.AppendChild(jobMode);
			XmlElement initForce = 
				xmlDoc.CreateElement("InitForce");
			initForce.InnerText = "2.2";
			elmNew.AppendChild(initForce);
			root.AppendChild(elmNew);
			xmlDoc.AppendChild(root);
			xmlDoc.Save(filePath);
			Debug.Log("AddXml OK!");

		}
	}

	//删除XML
	public void DeleteXml()
	{
		if(File.Exists(filePath))
		{
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(filePath);
			XmlNodeList nodeList = 
				xmlDoc.SelectSingleNode
					("root_CharacterTmp").ChildNodes;
			foreach(XmlElement xe in nodeList)
			{
				if(xe.GetAttribute("id") == "1")
				{
					xe.RemoveAttribute("id");
					foreach(XmlElement x1 in xe.ChildNodes)
					{
						if(x1.Name == "JobID")
						{
							x1.RemoveAll();
						}
					}
				}
			}
			xmlDoc.Save(filePath);
			Debug.Log("deleteXml OK!");
		}
	}

	//解析xml
	public void ShowXml()
	{
		if(File.Exists(filePath))
		{
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(filePath);
			XmlNodeList nodeList = 
				xmlDoc.SelectSingleNode
					("root_CharacterTmp").ChildNodes;
			foreach(XmlElement xe in nodeList)
			{
				if(xe.GetAttribute("id") == "1000")
				{

					id = int.Parse(xe.GetAttribute("id"));
					Debug.Log("id:" + id);
					foreach(XmlElement x1 in xe.ChildNodes)
					{
						switch(x1.Name)
						{
						case "JobID": 
							jobID = int.Parse(x1.InnerText);
							Debug.Log("jobID:" + jobID);
							break;
						case "JobMode" : 
							jobMode = x1.InnerText;
							Debug.Log("jobMode:" + jobMode);
							break;
						case "InitForce": 
							initForce = float.Parse(x1.InnerText);
							Debug.Log("initForce:" + initForce);
							break;
						default:
							break;
						}
					}

				}
			}
		}
	}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读