-1

Before I begin, I have read many posts/topics here/on the internet, but as far as I understand, I think none of the solutions I saw can deal with this JSON


I have a JSON file which I am looking to convert into an XML file - the catch is, when converting to XML normally, JSON like this -

{
    "data": {
        "key4":{
            "sample8": [
                {
                    "sample9":"val",
                    "sample10":"val"
                },
                {
                    "sample11":"val",
                    "sample12":"val"
                },
                {
                    "sample13":"val",
                    "sample14":"val"
                }
            ]
        }
    }
}

becomes -

<?xml version="1.0"?>
<data>
    <key4>
        <sample8>
            <sample9>val</sample9>
            <sample10>val</sample10>
        </sample8>
        <sample8>
            <sample11>val</sample11>
            <sample12>val</sample12>
        </sample8>
        <sample8>
            <sample13>val</sample13>
            <sample14>val</sample14>
        </sample8>
    </key4>
</data>

But, what I am looking to do is create a container element in the XML for every JSON array, with a specific array item element name (like "item"). Here's an example of the XML result I want -

<?xml version="1.0"?>
<data>
    <key4>
        <sample8>
            <item>
                <sample9>val</sample9>
                <sample10>val</sample10>
            </item>
            <item>
                <sample11>val</sample11>
                <sample12>val</sample12>
            </item>
            <item>
                <sample13>val</sample13>
                <sample14>val</sample14>
            </item>
        </sample8>
    </key4>
</data>

How do I do this? As I said, I've tried many different things but nothing seems to work out for me... Kindly guide :) Thanks!


P.S. If PHP doesn't support this kind of conversion, I'll be okay to use any other language which supports doing something like this. 👍

5
  • Chek it stackoverflow.com/questions/9544840/php-json-or-array-to-xml/… Commented Nov 23, 2021 at 18:30
  • What are you using to handle the json to xml conversion at the moment? Commented Nov 23, 2021 at 19:36
  • Hi @IGP - nothing currently, as none of the ideas I used didn't work out so I removed them... Commented Nov 23, 2021 at 19:37
  • I've managed to do it with simplexml. I'm posting it as an answer Commented Nov 23, 2021 at 19:40
  • Thanks a ton @IGP - That could help me a lot :) Commented Nov 23, 2021 at 19:40

1 Answer 1

0
$json = <<<JSON
{
  "data": {
    "key4":{
      "sample8": [
        {
          "sample9":"val",
          "sample10":"val"
        },
        {
          "sample11":"val",
          "sample12":"val"
        },
        {
          "sample13":"val",
          "sample14":"val"
        }
      ]
    }
  }
}
JSON;
function toXml($node, $array)
{
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            toXml($node->addChild(is_numeric($key) ? 'item' : $key), $value);
        } else {
            $node->addChild($key, $value);
        }
    }
}
// convert to array
$jsonArr = json_decode($json, true); 
/* 
$jsonArr = [
    "data" => [
        "key4" => [
            "sample8" => [
                [
                    "sample9" => "val",
                    "sample10" => "val"
                ],
                [
                    "sample11" => "val",
                    "sample12" => "val"
                ],
                [
                    "sample13" => "val",
                    "sample14" => "val"
                ]
            ]
        ]
    ]
]
*/
// initiate SimpleXMLElement with the root node and the xlmns:ajson attribute
$xml = new SimpleXMLElement('<data xmlns:ajson="http://www.altova.com/json"/>');
// use array_shift to skip first element (data)
toXml($xml, array_shift($jsonArr));
// your xml
echo $xml->asXML();

If you want to see it with white-spaces and line breaks, you can pass it to DomDocument.

$xmlDocument = new DOMDocument('1.0');
$xmlDocument->preserveWhiteSpace = false;
$xmlDocument->formatOutput = true;
$xmlDocument->loadXML($xml->asXML());

echo $xmlDocument->saveXML();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot @IGP for the solution! When I ran your code, all I see from echo is val val val val val val, which are my values. - How do I see the proper XML contents of the result? - kindly guide... Thanks!
Weird, I can xml as a string just fine. Try var_dump($xml->asXML()); or var_dump($xmlDocument->saveXML());
I have a working example at replit.com/@-IGP/json-to-xml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.