Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
XML.inc
1 <?php
18 class XML
19 {
20 
21 
30  public static function nodeListToArray(DomNodeList $nodeList)
31  {
32  $retval = array();
33  foreach ($nodeList as $item) {
34  $retval[] = $item;
35  }
36 
37  return $retval;
38 
39  }//end nodeListToArray()
40 
41 
51  public static function getElementsByTagNames(array $tagNames, DomNode $elem)
52  {
53  $retval = array();
54 
55  foreach ($tagNames as $tagName) {
56  $elements = $elem->getElementsByTagName($tagName);
57  foreach ($elements as $element) {
58  $retval[] = $element;
59  }
60  }
61 
62  return $retval;
63 
64  }//end getElementsByTagNames()
65 
66 
81  public static function getElementsByAttributeValue(DomDocument $doc, DomNode $element, $attr, $value, $recursive=TRUE)
82  {
83  $nodes = array();
84  if ($attr === '') {
85  return $nodes;
86  }
87 
88  $xp = new DOMXPath($doc);
89  $query = ($recursive === FALSE) ? '*' : '//*';
90  $query = $query.'[@'.$attr.' = "'.$value.'"]';
91  $nodes = $xp->query($query, $element);
92 
93  return $nodes;
94 
95  }//end getElementsByAttributeValue()
96 
97 
109  public static function hasChildElements(DomNode $node)
110  {
111  if ($node->hasChildNodes() === TRUE) {
112  foreach ($node->childNodes as $child) {
113  if ($child->nodeType === XML_ELEMENT_NODE) {
114  return TRUE;
115  }
116  }
117  }
118 
119  return FALSE;
120 
121  }//end hasChildElements()
122 
123 
135  public static function getFirstChildElement(DomNode $node)
136  {
137  foreach ($node->childNodes as $child) {
138  if ($child->nodeType === XML_ELEMENT_NODE) {
139  return $child;
140  }
141  }
142 
143  return NULL;
144 
145  }//end getFirstChildElement()
146 
147 
157  public static function getChildAt(DomNode $node, $position)
158  {
159  $pos = 0;
160  foreach ($node->childNodes as $child) {
161  if ($child->nodeType === XML_ELEMENT_NODE) {
162  if ($pos === $position) {
163  return $child;
164  }
165 
166  $pos++;
167  }
168  }
169 
170  return NULL;
171 
172  }//end getChildAt()
173 
174 
183  public static function childCount(DomNode $node)
184  {
185  $count = 0;
186  foreach ($node->childNodes as $child) {
187  if ($child->nodeType === XML_ELEMENT_NODE) {
188  $count++;
189  }
190  }
191 
192  return $count;
193 
194  }//end childCount()
195 
196 
208  public static function traverseDom(DomElement $element)
209  {
210  $nodeInfo = array();
211  $nodeInfo['tagName'] = $element->tagName;
212 
213  if ($element->hasAttributes() === TRUE) {
214  foreach ($element->attributes as $attribute) {
215  $nodeInfo['attrs'][$attribute->name] = $attribute->value;
216  }
217  }
218 
219  if ($element->hasChildNodes() === TRUE) {
220  foreach ($element->childNodes as $childNode) {
221  if ($childNode->nodeType === XML_TEXT_NODE) {
222  $nodeInfo['value'] = trim($childNode->nodeValue);
223  } else if ($childNode->tagName !== NULL) {
224  $nodeInfo['children'][] = self::traverseDOM($childNode);
225  }
226  }
227  }
228 
229  return $nodeInfo;
230 
231  }//end traverseDom()
232 
233 
247  public static function setAttributes(DomElement $element, array $attributes)
248  {
249  foreach ($attributes as $attrName => $attrValue) {
250  $element->setAttribute($attrName, $attrValue);
251  }
252 
253  return $element;
254 
255  }//end setAttributes()
256 
257 
258 }//end class
259 
260 
261 ?>