XML属性移动删除

来源:互联网 发布:java工程师工作内容 编辑:程序博客网 时间:2024/06/05 20:35
/************************
 ************需求内容**********
 *******把所有clips标签上的type,text属性移动到一个
 *****新建的标签——UI上,并把原来clip上的type,text属性删除
 * ********编辑器小工具***************
 *************************/
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Xml;


public class XmlChange {

    /// <summary>
    /// xml文件的绝对路径
    /// </summary>
    static string dataPath = Application.streamingAssetsPath + "/StoryTest.xml";
    
    [MenuItem("SkyWing/XML_ChangeDate")]
    private static void XMLchange()
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(dataPath);
        Debug.Log("开始修改");
        XmlElement list = xmlDoc.DocumentElement;
        Debug .Log (list.ChildNodes.Count);

            for (int i = 0, k = list.ChildNodes.Count; i < k; i++)
            {
                XmlNodeList nodeList = list.GetElementsByTagName("Clip");
                foreach (XmlElement item in nodeList)
                {
                    //不存在type属性 或者 text属性 则不替换
                    if (!item.HasAttribute("type") || !item.HasAttribute("text")) continue;
                    
                    //新生成一个UI元素 添加属性type和text 并且移除原来元素的type和text属性
                    XmlElement UIelement = xmlDoc.CreateElement("UI");
                    string type = item.GetAttribute("type");
                    string text = item.GetAttribute("text");
                    Debug.Log("type:" + type + ".......text:" + text);
                    UIelement.SetAttribute("type", type);
                    UIelement.SetAttribute("text", text);
                    item.AppendChild(UIelement);

                    item.RemoveAttribute("type");
                    item.RemoveAttribute("text");
                }
            }
            xmlDoc.Save(dataPath);
            Debug.Log("clip上的type,text已移动到新的标签UIelement上,并把原有的删除了");
    }

      
}
0 0
原创粉丝点击