XmlNode.class.php
7.35 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/**
* XML节点存储对象 用来存储DOM中节点数据,由此可以实现DOM持久化
*
* @name Util_Common_Xml_XmlNode
* @version version (2009-3-5 上午09:52:42)
* @package Q.Common.Xml
* @author peter.zyliu liuziyang@zadooo.com
* @since 1.0
*/
class Util_Common_Xml_XmlNode {
private $name;
private $value;
private $type;
private $parentNode;
private $attributeMap = array ();
private $children = array ();
/**
* 当前DOM节点
*
* @var DOMNode
*/
private $domNode;
/**
* Enter description here...
*
* @var Util_Common_Xml_Document
*/
private $document;
private $ns = array ();
/**
* 构造器
*
* @param string|DOMNode $xml XML字符串或DOM节点
* @param Util_Common_Xml_Document|null $document 所属文档
*/
public function __construct($xml = null, Util_Common_Xml_Document $document = null) {
if ($xml) {
if (is_string ( $xml )) {
$this->load ( $xml );
} elseif ($this->if_is_instance_of ( $xml, "DOMNode" )) {
$this->loadDOM ( $xml );
}
}
if ($document) {
$this->document = $document;
}
}
/**
* 添加新属性
*
* @param string $name 属性名
* @param string $value 属性值
*/
public function addAttr($name, $value) {
$this->attributeMap [$name] = $value;
}
/**
* 添加子节点
*
* @param Util_Common_Xml_XmlNode $childNode 新的子节点
*/
public function addChild(Util_Common_Xml_XmlNode $childNode) {
$this->children [] = $childNode;
}
/**
* 取得属性值
*
* @param string $name 属性名
* @return string|null
*/
public function attr($name) {
if (isset ( $this->attributeMap [$name] )) {
return $this->attributeMap [$name];
}
return null;
}
/**
* 取得所有属性
*
* @return array
* @see attrs
* @deprecated 该函数被attrs()所替代
*/
public function attrMap() {
return $this->attributeMap;
}
/**
* 取得所有属性的名值对
*
* @return array
*/
function attrs() {
return $this->attrMap ();
}
/**
* 使用名字取得对应的节点
*
* @param string $childName 子节点名
* @param boolean $caseSensitive 是否大小写敏感
* @return array
*/
public function namedChildren($childName, $caseSensitive = true) {
$children = array ();
foreach ( $this->children as $node ) {
$nodeName = $node->name ();
if (($caseSensitive && $nodeName === $childName) || (! $caseSensitive && strcasecmp ( $nodeName, $childName ) == 0)) {
$children [] = $node;
}
}
return $children;
}
/**
* 取得所有子节点
*
* @return array
*/
public function children() {
return $this->children;
}
/**
* 取得节点名
*
* @return string
*/
public function name() {
return $this->name;
}
/**
* 取得父节点
*
* @return Util_Common_Xml_XmlNode
*/
public function parent() {
return $this->parentNode;
}
/**
* 取得节点类型
*
* @return string
*/
public function type() {
return $this->type;
}
/**
* 取得节点值
*
* @return string
*/
public function value() {
return $this->value;
}
/**
* 设置节点名
*
* @param string $name 节点名
*/
public function setName($name) {
$this->name = $name;
}
/**
* 设置节点类型
*
* @param string $type 节点类型
*/
public function setType($type) {
$this->type = $type;
}
/**
* 节点值
*
* @param string $value 节点值
*/
public function setValue($value) {
$this->value = $value;
}
/**
* 设置父节点
*
* @param Util_Common_Xml_XmlNode $parentNode 父节点
*/
public function setParent(Util_Common_Xml_XmlNode $parentNode) {
$this->parentNode = $parentNode;
}
/**
* 判断一个对象/类是否是一个类的实例
*
* @param object|string $object 对象或类名
* @param string $class 类名
* @return boolean
*/
public function if_is_instance_of($object, $class) {
if (! is_object ( $object ) && ! is_string ( $object )) {
return false;
}
return ($object instanceof $class);
}
/**
* 从DOM节点中加载Util_Common_Xml_XmlNode节点树
*
* @param DOMNode $domNode DOM节点
*/
public function loadDOM(DOMNode $domNode) {
if (! $this->document && $this->if_is_instance_of ( $domNode, "Util_Common_Xml_Document" )) {
$this->setDocument ( new Util_Common_Xml_Document ( $domNode ) );
}
$this->domNode = $domNode;
$this->setType ( $domNode->nodeType );
$this->setName ( $domNode->nodeName );
$this->setValue ( $domNode->nodeValue );
if ($domNode->hasAttributes ()) {
$attributes = $domNode->attributes;
foreach ( $attributes as $attribute ) {
$this->addAttr ( $attribute->nodeName, $attribute->nodeValue );
}
}
if ($domNode->hasChildNodes ()) {
$childNodes = $domNode->childNodes;
foreach ( $childNodes as $childNode ) {
if ($childNode->nodeType == XML_ELEMENT_NODE) {
$child = new Util_Common_Xml_XmlNode ( $childNode, $this->document );
$child->setParent ( $this );
$this->addChild ( $child );
}
}
}
}
/**
* 从XML字符串中加载节点树
*
* @param string $xmlString XML字符串
*/
public function load($xmlString) {
$dom = new DOMDocument ( );
$dom->loadXML ( $xmlString );
$this->setDocument ( new Util_Common_Xml_Document ( $dom ) );
$this->loadDOM ( $dom );
}
/**
* 根据xpath表达式查找节点
*
* @param string|integer $expr xpath表达式或者子节点索引
* @return Util_Common_Xml_XmlNode
*/
function find($expr) {
if (is_int ( $expr )) {
return isset ( $this->children [$expr] ) ? $this->children [$expr] : null;
}
$nodes = $this->findAll ( $expr );
return empty ( $nodes ) ? null : $nodes [0];
}
/**
* 根据xpath表达式查找节点
*
* @param string $expr 表达式
* @return array
*/
function findAll($expr) {
$xpath = new DOMXPath ( $this->document->dom () );
foreach ( $this->document->ns () as $prefix => $uri ) {
$xpath->registerNamespace ( $prefix, $uri );
}
$nodeList = $xpath->query ( $expr, $this->domNode );
$ret = array ();
for($i = 0; $i < $nodeList->length; $i ++) {
$node = $nodeList->item ( $i );
$ret [] = new Util_Common_Xml_XmlNode ( $node, $this->document );
}
return $ret;
}
/**
* 查找和当前节点相邻的下一个节点
*
* @return Util_Common_Xml_XmlNode
*/
function next() {
$current = $this->domNode;
while ( $next = $current->nextSibling ) {
if ($next->nodeType == XML_ELEMENT_NODE) {
return new Util_Common_Xml_XmlNode ( $next, $this->document );
}
$current = $next;
}
return null;
}
/**
* 查找和当前节点相邻的上一个节点
*
* @return Util_Common_Xml_XmlNode
*/
function prev() {
$current = $this->domNode;
while ( $prev = $current->previousSibling ) {
if ($prev->nodeType == XML_ELEMENT_NODE) {
return new Util_Common_Xml_XmlNode ( $prev, $this->document );
}
$current = $prev;
}
return null;
}
/**
* 将当前节点转化为XML字符串
*
* @return string
*/
function asXml() {
if ($this->document) {
return $this->document->dom ()->saveXML ( $this->domNode );
}
return null;
}
/**
* 设置当前节点所在文档
*
* @param Util_Common_Xml_Document $document 文档
*/
function setDocument(Util_Common_Xml_Document $document) {
$this->document = $document;
}
/**
* 获取当前节点所在文档
*
* @return IXmlDocument 文档
* @since 1.0.1
*/
function document() {
return $this->document;
}
}
?>