-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxml2array.class.php
More file actions
125 lines (117 loc) · 4.21 KB
/
xml2array.class.php
File metadata and controls
125 lines (117 loc) · 4.21 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
<?php
/**
* Class to translate PHP Array element into XML and vice versa.
*
* @author Marco Vito Moscaritolo
* @copyright GPL 3
* @tutorial http://mavimo.org/varie/array_xml_php
* @example index.php
* @version 0.8
*/
class ArrayToXML {
/**
* @staticvar string - String to use as key for node attributes into array
* @todo Convert this into a value settable from user
*/
const attr_arr_string = 'attributes';
/**
* The main function for converting to an XML document.
* Pass in a multi dimensional array and this recrusively loops through and builds up an XML document.
*
* @static
* @param array $data
* @param string $rootNodeName - what you want the root node to be - defaultsto data.
* @param SimpleXMLElement $xml - should only be used recursively
* @return string XML
*/
public static function toXml($data, $rootNodeName = 'data', &$xml = NULL) {
if (is_null($xml)) {
$xml = new SimpleXMLElement('<' . $rootNodeName . '/>');
}
// loop through the data passed in.
foreach($data as $key => $value) {
// if numeric key, assume array of rootNodeName elements
if (is_numeric($key)) {
$key = $rootNodeName;
}
// Check if is attribute
if($key == ArrayToXML::attr_arr_string) {
// Add attributes to node
foreach($value as $attr_name => $attr_value) {
$xml->addAttribute($attr_name, $attr_value);
}
} else {
// delete any char not allowed in XML element names
$key = preg_replace('/[^a-z0-9\-\_\.\:]/i', '', $key);
// if there is another array found recrusively call this function
if (is_array($value)) {
// create a new node unless this is an array of elements
$node = ArrayToXML::isAssoc($value) ? $xml->addChild($key) : $xml;
// recrusive call - pass $key as the new rootNodeName
ArrayToXML::toXml($value, $key, $node);
} else {
// add single node.
$value = htmlentities($value);
$xml->addChild($key,$value);
}
}
}
// pass back as string. or simple xml object if you want!
return $xml->asXML();
}
/**
* The main function for converting to an array.
* Pass in a XML document and this recrusively loops through and builds up an array.
*
* @static
* @param string $obj - XML document string (at start point)
* @param array $arr - Array to generate
* @return array - Array generated
*/
public static function toArray( $obj, &$arr = NULL ) {
if ( is_null( $arr ) ) $arr = array();
if ( is_string( $obj ) ) $obj = new SimpleXMLElement( $obj );
// Get attributes for current node and add to current array element
$attributes = $obj->attributes();
foreach ($attributes as $attrib => $value) {
$arr[ArrayToXML::attr_arr_string][$attrib] = (string)$value;
}
$children = $obj->children();
$executed = FALSE;
// Check all children of node
foreach ($children as $elementName => $node) {
// Check if there are multiple node with the same key and generate a multiarray
if($arr[$elementName] != NULL) {
if($arr[$elementName][0] !== NULL) {
$i = count($arr[$elementName]);
ArrayToXML::toArray($node, $arr[$elementName][$i]);
} else {
$tmp = $arr[$elementName];
$arr[$elementName] = array();
$arr[$elementName][0] = $tmp;
$i = count($arr[$elementName]);
ArrayToXML::toArray($node, $arr[$elementName][$i]);
}
} else {
$arr[$elementName] = array();
ArrayToXML::toArray($node, $arr[$elementName]);
}
$executed = TRUE;
}
// Check if is already processed and if already contains attributes
if(!$executed && $children->getName() == "" && !isset ($arr[ArrayToXML::attr_arr_string])) {
$arr = (String)$obj;
}
return $arr;
}
/**
* Determine if a variable is an associative array
*
* @static
* @param array $obj - variable to analyze
* @return boolean - info about variable is associative array or not
*/
private static function isAssoc( $array ) {
return (is_array($array) && 0 !== count(array_diff_key($array, array_keys(array_keys($array)))));
}
}