-
Notifications
You must be signed in to change notification settings - Fork 0
/
XMLNode.cs
executable file
·129 lines (114 loc) · 2.99 KB
/
XMLNode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*----------------------------------------------------------------
// Copyright (C) 2015 广州,Lucky Game
//
// 模块名:Xml 解析工具
// 创建者:D.S.Qiu
// 修改者列表:
// 创建日期:February 29 2016
// 模块描述:
//----------------------------------------------------------------*/
#if WP_BUILD
using Hashtable = MarkerMetro.Unity.WinLegacy.Plugin.Collections.Hashtable;//MakerMetro.Unity.WinLegacy.Plugin.Collections.Hashtable;
#else
using System.Collections;
#endif
public class XMLNode : Hashtable
{
public XMLNodeList GetNodeList(string path)
{
return this.GetObject(path) as XMLNodeList;
}
public XMLNodeList GetDeepNodeList(string name)
{
XMLNodeList nodeList = new XMLNodeList ();
IDictionaryEnumerator iter = this.GetEnumerator();
while (iter.MoveNext())
{
if (iter.Value is XMLNodeList)
{
XMLNodeList children = iter.Value as XMLNodeList;
//UnityEngine.Debug.Log ("children Count:" + children.Count + " name: " + name + " key:" + iter.Key + " " + name );
//it‘s can't use "iter.Key == name",because iter.Key is object.
if (iter.Key.Equals(name))
{
nodeList.AddRange (children);
}
else
{
foreach(XMLNode child in children)
nodeList.AddRange (child.GetDeepNodeList (name));
}
}
}
return nodeList;
}
public XMLNode GetNode(string path)
{
return this.GetObject(path) as XMLNode;
}
public string GetValue(string path)
{
return this.GetObject(path) as string;
}
private object GetObject(string path)
{
XMLNode currentNode = this;
XMLNodeList currentNodeList = null;
bool listMode = false;
string[] array = path.Split(new char[]
{
'>'
});
object result;
for (int i = 0; i < array.Length; i++)
{
if (listMode)
{
currentNode = (currentNodeList[int.Parse(array[i])] as XMLNode);
listMode = false;
}
else
{
object obj = currentNode[array[i]];
if (!(obj is XMLNodeList))
{
// reached a leaf node/attribute
if (i != array.Length - 1)
{
string actualPath = "";
for (int j = 0; j <= i; j++)
{
actualPath = actualPath + ">" + array[j];
}
UnityEngine.Debug.Log("xml path search truncated. Wanted: " + path + " got: " + actualPath);
}
result = obj;
return result;
}
currentNodeList = (obj as XMLNodeList);
listMode = true;
}
}
if (listMode)
{
result = currentNodeList;
return result;
}
result = currentNode;
return result;
}
public override string ToString ()
{
IDictionaryEnumerator iter = this.GetEnumerator();
string toText = string.Empty;
while (iter.MoveNext())
{
if (iter.Value is XMLNodeList)
toText += "\n";
else
toText += iter.Key + ", ";
toText += iter.Value + "\n";
}
return toText;
}
}