I want to convert and XML file to JSON with PHP. The XML file looks like this:
<content>
<!-- other elements... -->
<box id="1">
<a>...</a>
<b>...</b>
<c>...</c>
</box>
<box id="2">
<a>...</a>
<b>...</b>
<c>...</c>
</box>
<box id="3">
<a>...</a>
<b>...</b>
<c>...</c>
</box>
<!-- more <box> elements... -->
<!-- other elements... -->
</content>
I am using this simple PHP script:
// Open XML file with SimpleXML.
$xml = simplexml_load_file('file.xml');
// Convert XML content to JSON.
$json = json_encode($xml);
// Output JSON.
echo $json;
I get the full content of the XML file as JSON output, however I need to modify the script to:
- Get only JSON for the
<box>elements, not the complete file. - Get JSON without element attributes.
This is an example of what I want to get as output:
[{"a":"...","b":"...","c":"..."},
{"a":"...","b":"...","c":"..."},
{"a":"...","b":"...","c":"..."}]
Please help me, how can I do this? what is the best practice?
Thanks in advance.