forked from stwa/google_addressbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_utils.php
68 lines (61 loc) · 1.67 KB
/
xml_utils.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
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* XML utils class used to turn an XML string into an array.
* This code is based on the code from "gaarf" and was slightly modified to fit my needs.
* See https://github.com/gaarf/XML-string-to-PHP-array
*
* @version 1.0
* @author Stefan L. Wagner
*/
class xml_utils
{
static function xmlstr_to_array($xmlstr) {
$doc = new DOMDocument();
$doc->loadXML($xmlstr);
return xml_utils::domnode_to_array($doc->documentElement);
}
static function domnode_to_array($node) {
$output = array();
switch ($node->nodeType) {
case XML_CDATA_SECTION_NODE:
case XML_TEXT_NODE:
case XML_ELEMENT_NODE:
if(!$node->childNodes) {
return;
}
for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
$child = $node->childNodes->item($i);
$v = xml_utils::domnode_to_array($child);
if(isset($child->tagName)) {
$t = $child->tagName;
if(!isset($output[$t])) {
$output[$t] = array();
}
$output[$t][] = $v;
} elseif($v) {
$output = $v;
}
}
if(is_array($output)) {
if($node->attributes->length) {
$a = array();
foreach($node->attributes as $attrName => $attrNode) {
$a[$attrName] = (string) $attrNode->value;
}
$output['@attributes'] = $a;
}
if($node->nodeType == XML_ELEMENT_NODE) {
$output['@text'] = trim($node->textContent);
}
foreach ($output as $t => $v) {
if(is_array($v) && count($v)==1 && $t!='@attributes') {
//$output[$t] = $v[0];
}
}
}
break;
}
return $output;
}
}
?>