-
Notifications
You must be signed in to change notification settings - Fork 3
/
XmlParser.php
55 lines (46 loc) · 1.57 KB
/
XmlParser.php
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
<?php
namespace src\utils;
class XmlParser
{
public static function domParser($xml)
{
$doc = new \DOMDocument();
$doc->loadXML($xml);
return $doc;
}
// Returns the first DOMNode associated with the given tag name in the xml
public static function getNodeByTagName($dom, $elementName)
{
return $dom->getElementsByTagName($elementName)->item(0);
}
// Returns the DOMNodeList associated with the given tag name in the xml
public static function getNodeListByTagName($dom, $elementName)
{
return $dom->getElementsByTagName($elementName);
}
// Returns the value associated with the given tag name in the xml
public static function getValueByTagName($dom, $elementName)
{
return $dom->getElementsByTagName($elementName)->item(0)->nodeValue;
}
// Returns the array of values associated with the given tag name in the xml
public static function getValueListByTagName($dom, $elementName)
{
$nodeList = $dom->getElementsByTagName($elementName);
$nodes = array();
foreach($nodeList as $node) {
$nodes[] = $node->nodeValue;
}
return $nodes;
}
// Returns the attribute by name, of the given tag name in the xml
public static function getAttribute($dom, $elementName, $attributeName)
{
$attributes = $dom->getElementsByTagName($elementName)->item(0);
return $attributes->getAttribute($attributeName);
}
public static function getDomDocumentAsString($dom)
{
return $dom->saveXML($dom);
}
}