easy!
$xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode(
array = json_decode($json,TRUE);
Answer from user1398287 on Stack OverflowVideos
How to use the
Convert XML to PHP Array Online for free?
How to use the
Convert XML to JSON Array Online for free?
What is PHP Array format?
easy!
$xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode(
array = json_decode($json,TRUE);
Another option is the SimpleXML extension (I believe it comes standard with most php installs.)
http://php.net/manual/en/book.simplexml.php
The syntax looks something like this for your example
$xml = new SimpleXMLElement($xmlString);
echo $xml->bbb->cccc->dddd['Id'];
echo $xml->bbb->cccc->eeee['name'];
// or...........
foreach ($xml->bbb->cccc as $element) {
foreach($element as
val) {
echo "{$key}: {$val}";
}
}
Easiest way to do this is to use the built in functions, then convert to an array.
<?php
$obj = simplexml_load_string($xml); // Parse XML
$array = json_decode(json_encode($obj), true); // Convert to array
?>
This piece of XML seems to break it.
<BackupJob ID="2011-11-09-05-00-00" StartTime="2011-11-09 04:56:51" EndTime="2011-11-09 05:02:01" BackupJobStatus="BS_STOP_SUCCESS" NumOfWarnEntries="0" NumOfErrorEntries="0" NumOfNewFiles="0" TotalNewFilesSize="0" NumOfUpdatedFiles="1" TotalUpdatedFilesSize="8709755" NumOfDeletedFiles="0" TotalDeletedFilesSize="0" NumOfMovedFiles="0" TotalMovedFilesSize="0" NumOfUpdatedPermissionFiles="0" TotalUpdatedPermissionFilesSize="0"></BackupJob>
You can json_encode() then json_decode() as array and use simplexml_load_string()
Steps:
1) First convert your XML into readable string object using simplexml_load_string().
2) Then json_encode() it.
3) json_decode() it, with second parameter TRUE, which will return array instead of object.
4) Now, your XML is converted into an array.
5) Take a blank array, loop over array from above code and append elements to it.
To get desired output:
<?php
$xml = '<?xml version = "1.0" encoding = "utf-8"?>
<tutorialspoint>
<course category = "JAVA">
<title lang = "en">Java</title>
<tutor>Gopal</tutor>
<duration>3</duration>
<price>$30</price>
</course>
<course category = "HADOOP">
<title lang = "en">Hadoop</title>.
<tutor>Satish</tutor>
<duration>3</duration>
<price>$50</price>
</course>
<course category = "HTML">
<title lang = "en">html</title>
<tutor>raju</tutor>
<duration>5</duration>
<price>$50</price>
</course>
<course category = "WEB">
<title lang = "en">Web Technologies</title>
<tutor>Javed</tutor>
<duration>10</duration>
<price>$60</price>
</course>
</tutorialspoint>';
$arr = [];
$array = json_decode(json_encode(simplexml_load_string($xml)),true);
if ( ! empty($array)) {
$i=0;
foreach ($array['course'] as $elem) {
$arr[$i]['title'] = $elem['title'];
$arr[$i]['tutor'] = $elem['tutor'];
$arr[$i]['duration'] = $elem['duration'];
$arr[$i]['price'] = $elem['price'];
++$i;
}
}
echo '<pre>';print_r($arr);echo '</pre>';
Output:
Array
(
[0] => Array
(
[title] => Java
[tutor] => Gopal
[duration] => 3
[price] => $30
)
[1] => Array
(
[title] => Hadoop
[tutor] => Satish
[duration] => 3
[price] => $50
)
[2] => Array
(
[title] => html
[tutor] => raju
[duration] => 5
[price] => $50
)
[3] => Array
(
[title] => Web Technologies
[tutor] => Javed
[duration] => 10
[price] => $60
)
)
Working Code:
This code snippet will convert your XML to array
$array = json_decode(json_encode((array)simplexml_load_string($xml)),true);
echo '<pre>';
print_r($array);