1 |
32a96ee6
|
Andreas Kohlbecker
|
<?php
|
2 |
|
|
/*
|
3 |
|
|
============================================================================================
|
4 |
|
|
Filename:
|
5 |
|
|
---------
|
6 |
|
|
xml2json.php
|
7 |
|
|
|
8 |
|
|
Description:
|
9 |
|
|
------------
|
10 |
|
|
This PHP class converts XML-based data into JSON formatted data.
|
11 |
|
|
This program makes use of several open source PHP utility classes and functions.
|
12 |
|
|
|
13 |
|
|
License:
|
14 |
|
|
--------
|
15 |
|
|
This code is made available free of charge with the rights to use, copy, modify,
|
16 |
|
|
merge, publish and distribute. This Software shall be used for Good, not Evil.
|
17 |
|
|
|
18 |
|
|
First Created on:
|
19 |
|
|
-----------------
|
20 |
|
|
Oct/04/2006
|
21 |
|
|
|
22 |
|
|
Last Modified on:
|
23 |
|
|
-----------------
|
24 |
|
|
Oct/07/2006
|
25 |
|
|
============================================================================================
|
26 |
|
|
*/
|
27 |
|
|
require_once 'json/JSON.php';
|
28 |
|
|
|
29 |
|
|
// Internal program-specific Debug option.
|
30 |
|
|
define ("DEBUG", false);
|
31 |
|
|
// Maximum Recursion Depth that we can allow.
|
32 |
|
|
define ("MAX_RECURSION_DEPTH_ALLOWED", 25);
|
33 |
|
|
// An empty string
|
34 |
|
|
define ("EMPTY_STR", "");
|
35 |
|
|
// SimpleXMLElement object property name for attributes
|
36 |
|
|
define ("SIMPLE_XML_ELEMENT_OBJECT_PROPERTY_FOR_ATTRIBUTES", "@attributes");
|
37 |
|
|
// SimpleXMLElement object name.
|
38 |
|
|
define ("SIMPLE_XML_ELEMENT_PHP_CLASS", "SimpleXMLElement");
|
39 |
|
|
|
40 |
|
|
class xml2json {
|
41 |
|
|
|
42 |
|
|
/*
|
43 |
|
|
=============================================================================
|
44 |
|
|
Function name:
|
45 |
|
|
---------------
|
46 |
|
|
transformXmlStringToJson
|
47 |
|
|
|
48 |
|
|
Function Parameters:
|
49 |
|
|
---------------------
|
50 |
|
|
1) XML data string.
|
51 |
|
|
|
52 |
|
|
Description:
|
53 |
|
|
------------
|
54 |
|
|
This function transforms the XML based String data into JSON format. If the input XML
|
55 |
|
|
string is in table format, the resulting JSON output will also be in table format.
|
56 |
|
|
Conversely, if the input XML string is in tree format, the resulting JSON output will
|
57 |
|
|
also be in tree format.
|
58 |
|
|
|
59 |
|
|
Function Return Value:
|
60 |
|
|
----------------------
|
61 |
|
|
1) If everything is successful, it returns a string containing JSON table/tree formatted data.
|
62 |
|
|
Otherwise, it returns an empty string.
|
63 |
|
|
|
64 |
|
|
First Created on:
|
65 |
|
|
-----------------
|
66 |
|
|
Oct/04/2006
|
67 |
|
|
|
68 |
|
|
Last Modified on:
|
69 |
|
|
-----------------
|
70 |
|
|
Oct/07/2006
|
71 |
|
|
=============================================================================
|
72 |
|
|
*/
|
73 |
|
|
public static function transformXmlStringToJson($xmlStringContents) {
|
74 |
|
|
/*
|
75 |
|
|
Get the SimpleXMLElement representation of the function input
|
76 |
|
|
parameter that contains XML string. Convert the XML string
|
77 |
|
|
contents to SimpleXMLElement type. SimpleXMLElement type is
|
78 |
|
|
nothing but an object that can be processed with normal property
|
79 |
|
|
selectors and (associative) array iterators.
|
80 |
|
|
simplexml_load_string returns a SimpleXMLElement object which
|
81 |
|
|
contains an instance variable which itself is an associative array of
|
82 |
|
|
several SimpleXMLElement objects.
|
83 |
|
|
*/
|
84 |
97935565
|
Katja Luther
|
$simpleXmlElementObject = simplexml_load_string($xmlStringContents);
|
85 |
32a96ee6
|
Andreas Kohlbecker
|
|
86 |
|
|
if ($simpleXmlElementObject == null) {
|
87 |
|
|
return(EMPTY_STR);
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
$simpleXmlRootElementName = $simpleXmlElementObject->getName();
|
91 |
|
|
// Uncomment this line to see the inner details of the SimpleXMLElement object.
|
92 |
|
|
if (DEBUG) {
|
93 |
|
|
// var_dump($simpleXmlRootElementName);
|
94 |
|
|
// var_dump($simpleXmlElementObject);
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
$jsonOutput = EMPTY_STR;
|
98 |
|
|
// Let us convert the XML structure into PHP array structure.
|
99 |
|
|
$array1 = xml2json::convertSimpleXmlElementObjectIntoArray($simpleXmlElementObject);
|
100 |
|
|
|
101 |
|
|
if (($array1 != null) && (sizeof($array1) > 0)) {
|
102 |
|
|
//create a new instance of Services_JSON
|
103 |
|
|
$json = new Services_JSON();
|
104 |
|
|
$jsonOutput = $json->encode($array1);
|
105 |
|
|
|
106 |
|
|
if (DEBUG) {
|
107 |
|
|
// var_dump($array1);
|
108 |
|
|
// var_dump($jsonOutput);
|
109 |
|
|
}
|
110 |
|
|
} // End of if (($array1 != null) && (sizeof($array1) > 0))
|
111 |
|
|
|
112 |
|
|
return($jsonOutput);
|
113 |
|
|
} // End of function transformXmlStringToJson
|
114 |
|
|
|
115 |
|
|
/*
|
116 |
|
|
=============================================================================
|
117 |
|
|
Function name:
|
118 |
|
|
---------------
|
119 |
|
|
convertSimpleXmlElementObjectIntoArray
|
120 |
|
|
|
121 |
|
|
Function Parameters:
|
122 |
|
|
---------------------
|
123 |
|
|
1) Simple XML Element Object
|
124 |
|
|
|
125 |
|
|
(The following function argument needs to be passed only when this function is
|
126 |
|
|
called recursively. It can be omitted when this function is called from another
|
127 |
|
|
function.)
|
128 |
|
|
2) Recursion Depth
|
129 |
|
|
|
130 |
|
|
Description:
|
131 |
|
|
------------
|
132 |
|
|
This function accepts a SimpleXmlElementObject as a single argument.
|
133 |
|
|
This function converts the XML object into a PHP associative array.
|
134 |
|
|
If the input XML is in table format (i.e. non-nested), the resulting associative
|
135 |
|
|
array will also be in a table format. Conversely, if the input XML is in
|
136 |
|
|
tree (i.e. nested) format, this function will return an associative array
|
137 |
|
|
(tree/nested) representation of that XML.
|
138 |
|
|
|
139 |
|
|
There are so many ways to turn an XML document into a PHP array. Out of all
|
140 |
|
|
those options, the recursive logic here uses a method that is very nicely
|
141 |
|
|
documented by the PHP open source community in the SimpleXMLElement section of
|
142 |
|
|
the PHP manual available at www.php.net. Credit goes to all those kind
|
143 |
|
|
PHP (People Helping People!!!) souls.
|
144 |
|
|
|
145 |
|
|
Function Return Value:
|
146 |
|
|
----------------------
|
147 |
|
|
1) If everything is successful, it returns an associate array containing
|
148 |
|
|
the data collected from the XML format. Otherwise, it returns null.
|
149 |
|
|
|
150 |
|
|
Caution and Remarks:
|
151 |
|
|
---------------------
|
152 |
|
|
IT IS A RECURSIVE FUNCTION.
|
153 |
|
|
|
154 |
|
|
First Created on:
|
155 |
|
|
-----------------
|
156 |
|
|
Oct/04/2006
|
157 |
|
|
|
158 |
|
|
Last Modified on:
|
159 |
|
|
-----------------
|
160 |
|
|
June/01/2007
|
161 |
|
|
=============================================================================
|
162 |
|
|
*/
|
163 |
|
|
public static function convertSimpleXmlElementObjectIntoArray($simpleXmlElementObject, &$recursionDepth=0) {
|
164 |
|
|
// Keep an eye on how deeply we are involved in recursion.
|
165 |
|
|
if ($recursionDepth > MAX_RECURSION_DEPTH_ALLOWED) {
|
166 |
|
|
// Fatal error. Exit now.
|
167 |
|
|
return(null);
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
if ($recursionDepth == 0) {
|
171 |
|
|
if (get_class($simpleXmlElementObject) != SIMPLE_XML_ELEMENT_PHP_CLASS) {
|
172 |
|
|
// If the external caller doesn't call this function initially
|
173 |
|
|
// with a SimpleXMLElement object, return now.
|
174 |
|
|
return(null);
|
175 |
|
|
} else {
|
176 |
|
|
// Store the original SimpleXmlElementObject sent by the caller.
|
177 |
|
|
// We will need it at the very end when we return from here for good.
|
178 |
|
|
$callerProvidedSimpleXmlElementObject = $simpleXmlElementObject;
|
179 |
|
|
}
|
180 |
|
|
} // End of if ($recursionDepth == 0) {
|
181 |
|
|
|
182 |
|
|
if (get_class($simpleXmlElementObject) == SIMPLE_XML_ELEMENT_PHP_CLASS) {
|
183 |
|
|
// Get a copy of the simpleXmlElementObject
|
184 |
|
|
$copyOfsimpleXmlElementObject = $simpleXmlElementObject;
|
185 |
|
|
// Get the object variables in the SimpleXmlElement object for us to iterate.
|
186 |
|
|
$simpleXmlElementObject = get_object_vars($simpleXmlElementObject);
|
187 |
|
|
}
|
188 |
|
|
|
189 |
|
|
// It needs to be an array of object variables.
|
190 |
|
|
if (is_array($simpleXmlElementObject)) {
|
191 |
|
|
// Initialize the result array.
|
192 |
|
|
$resultArray = array();
|
193 |
|
|
// Is the input array size 0? Then, we reached the rare CDATA text if any.
|
194 |
|
|
if (count($simpleXmlElementObject) <= 0) {
|
195 |
|
|
// Let us return the lonely CDATA. It could even be whitespaces.
|
196 |
|
|
return (trim(strval($copyOfsimpleXmlElementObject)));
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
// Let us walk through the child elements now.
|
200 |
|
|
foreach($simpleXmlElementObject as $key=>$value) {
|
201 |
|
|
// When this block of code is commented, XML attributes will be
|
202 |
|
|
// added to the result array.
|
203 |
|
|
// Uncomment the following block of code if XML attributes are
|
204 |
|
|
// NOT required to be returned as part of the result array.
|
205 |
|
|
/*
|
206 |
|
|
if((is_string($key)) && ($key == SIMPLE_XML_ELEMENT_OBJECT_PROPERTY_FOR_ATTRIBUTES)) {
|
207 |
|
|
continue;
|
208 |
|
|
}
|
209 |
|
|
*/
|
210 |
|
|
// Let us recursively process the current element we just visited.
|
211 |
|
|
// Increase the recursion depth by one.
|
212 |
|
|
$recursionDepth++;
|
213 |
|
|
$resultArray[$key] = xml2json::convertSimpleXmlElementObjectIntoArray($value, $recursionDepth);
|
214 |
|
|
// Decrease the recursion depth by one.
|
215 |
|
|
$recursionDepth--;
|
216 |
|
|
} // End of foreach($simpleXmlElementObject as $key=>$value) {
|
217 |
|
|
|
218 |
|
|
if ($recursionDepth == 0) {
|
219 |
|
|
// That is it. We are heading to the exit now.
|
220 |
|
|
// Set the XML root element name as the root [top-level] key of
|
221 |
|
|
// the associative array that we are going to return to the caller of this
|
222 |
|
|
// recursive function.
|
223 |
|
|
$tempArray = $resultArray;
|
224 |
|
|
$resultArray = array();
|
225 |
|
|
$resultArray[$callerProvidedSimpleXmlElementObject->getName()] = $tempArray;
|
226 |
|
|
}
|
227 |
|
|
|
228 |
|
|
return ($resultArray);
|
229 |
|
|
} else {
|
230 |
|
|
// We are now looking at either the XML attribute text or
|
231 |
|
|
// the text between the XML tags.
|
232 |
|
|
return (trim(strval($simpleXmlElementObject)));
|
233 |
|
|
} // End of else
|
234 |
|
|
} // End of function convertSimpleXmlElementObjectIntoArray.
|
235 |
|
|
|
236 |
97935565
|
Katja Luther
|
} // End of class xml2json
|