My Api (laravel) use Json code. Now I also want to support xml. The Xml message must be converted to Json so it fits into the existing structure. But I can not match the XML to the desired Json format.
Example XML and Convert to Json:
$xml = "<?xml version='1.0'?>
<catalog>
<book>
<author>Jan</author>
<title>Xml to Json Array</title>
<pages>
<page>
<nr>1</nr>
<title>Welcome to my book</title>
<pagereviews>
<pagereview>
<name>maikel</name>
<comment>very good</comment>
</pagereview>
<pagereview>
<name>John</name>
<comment>i like this page</comment>
</pagereview>
</pagereviews>
</page>
<page>
<nr>2</nr>
<title>more info</title>
</page>
<page>
<nr>3</nr>
<title>lots of fun</title>
</page>
</pages>
</book>
</catalog>";
$xmlObject = simplexml_load_string($xml);
$jsonString = json_encode($xmlObject);
return $jsonString;
In Json this results in:
{
"book": {
"author": "Jan",
"title": "Xml to Json Array",
"pages": {
"page": [
{
"nr": "1",
"title": "Welcome to my book",
"pagereviews": {
"pagereview": [
{
"name": "maikel",
"comment": "very good"
},
{
"name": "John",
"comment": "i like this page"
}
]
}
},
{
"nr": "2",
"title": "more info"
},
{
"nr": "3",
"title": "lots of fun"
}
]
}
}
}
I need it without tags Page and Pagereview. Like this.
{
"book": {
"author": "Jan",
"title": "Xml to Json Array",
"pages": [
{
"nr": "1",
"title": "Welcome to my book",
"pagereviews": [
{
"name": "maikel",
"comment": "very good"
},
{
"name": "John",
"comment": "i like this page"
}
]
},
{
"nr": "2",
"title": "more info"
},
{
"nr": "3",
"title": "lots of fun"
}
]
}
}