John Davidson

Transform same recursive node xml into Json with php

0 comments
Message:


I'm trying to transform XML into Json with PHP. I have in input an XML file (file.xml) with a recursive structure and I would like to transpose it into Json (out.json). The graph contains a repeated recursive node:


xml file sample:


<Transfer>
<Name>Transfer</Name>
<Title>Transfer + Date</Title>
<Level>
<Name>Item 1</Name>
<Title>Title 1</Title>
<className>middle-level</className>
<Level>
<Name>Item 1.1</Name>
<Title>Title 1.1</Title>
<className>product-dept</className>
</Level>
<Level>
<Name>Item 1.2</Name>
<Title>Title 1.2</Title>
<className>product-dept</className>
<Level>
<Name>Item 1.2.1</Name>
<Title>Title 1.2.1</Title>
<className>pipeline1</className>
<Level>
<Name>Item 1.2.1.1</Name>
<Title>Title 1.2.1.1</Title>
<className>otherClass</className>
</Level>
</Level>
</Level>
</Level>
<Level>
<Name>Item 2</Name>
<Title>Title 2</Title>
<className>middle-level</className>
</Level>
</Transfer>

Json output desired :


{
'name': 'Transfer',
'title': 'transfer Date',
'children': [
{ 'name': 'Item 1', 'title': 'Title 1', 'className': 'middle-level',
'children': [
{ 'name': 'Item 1.1', 'title': 'Title 1.1', 'className': 'product-dept' },
{ 'name': 'Item 1.2', 'title': 'Title 1.2', 'className': 'product-dept',
'children': [
{ 'name': 'Item 1.2.1', 'title': 'Title 1.2.1', 'className': 'pipeline1' ,
'children': [
{ 'name': 'Item 1.2.1.1', 'title': 'Title 1.2.1.1', 'className': 'otherClass' },
]
]
}
]
},
{ 'name': 'Item 2', 'title': 'Title 2', 'className': 'middle-level'}
]
}

All this with php code :


$xml = simplexml_load_file("fileOK.xml");
$result = [];
$result['names'] = (string)$xml->Name;
$result['titles'] = (string)$xml->Title;

foreach ($xml->Level as $value)
{

$child = [];
$item = [];
$item['name'] = (string)$value->Name;
$item['title'] = (string)$value->Title;
$item['className'] = (string)$value->className;

foreach ($value->Level as $level)
{
$child2 = [];
foreach ($level->Level as $level) {
$child[] = array(
'name' => (string)$level->Name,
'title' => (string)$level->Title,
'className' => (string)$level->className,
);
$item['children'] = $child2;
}

}

$item['children'] = $child;
$result['childrens'][] = $item;
}
$json_string = json_encode($result);
print_r($json_string);

I'm just transforming a part of the graph but I don't understand how to do more.


Thanks for the help.



My Answer:

Parent Node


Child Node 1


Grandchild Node 1


Great Grandchild Node 1


Great Great Grandchild Node 1









Child Node 2




Desired output in out.json:
{
"name": "Parent Node",
"children": [
{
"name": "Child Node 1",
"children": [
{
"name": "Grandchild Node 1",
"children": [
{
"name": "Great Grandchild Node 1",
"children": [
{
"name": "Great Great Grandchild Node 1",
"children": []
}
]
}
]
}
]
},
{
"name": "Child Node 2",
"children": []
}
]
}
To achieve this transformation, you can use PHP's SimpleXMLElement class to parse the XML file and then convert it to an associative array using json_encode. Here's a sample code snippet to accomplish this:
php
$xml = simplexml_load_file('file.xml');
function xmlToArray($xml) {
$array = [];
foreach ($xml->children() as $child) {
$name = $child->name;
$children = $child->children;
$array[] = [
'name' => (string) $name,
'children' => $children ? xmlToArray($children) : []
];
}
return $array;
}
$array = xmlToArray($xml);
$json = json_encode($array, JSON_PRETTY_PRINT);
file_put_contents('out.json', $json);

This code will read the XML file, convert it to an associative array, and then encode it as JSON before writing it to the out.json file.

Rate this post

4 of 5 based on 2377 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us