phpjsonxmlxml-to-json

Root tag missing after converting XML to JSON and saving to disk


I'm attempting create a simple XML to JSON converter, reading from one folder and outputting to another. This process seems to be working okay, but I just cannot seem to get this thing to include the root tag of the XML. Please find samples below:

<?php

$inputFolder = new FilesystemIterator("C:/folder/XML to JSON/Input");
$outputFolder = ("C:/XML to JSON/Output/");
//for new filename
$extensionToRemove = array(".xml");

foreach ($inputFolder as $xmlFile) {

    //purely file name related
    $theFileNameAndExtension = $xmlFile->getFilename(); 
    $theFileName = str_replace($extensionToRemove, "", $theFileNameAndExtension);

    //load xml file from disk
    $xmlFileLoaded = simplexml_load_file($xmlFile); 
    //json encode the xml
    $jsonResult = json_encode($xmlFileLoaded, JSON_FORCE_OBJECT | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION | JSON_PRETTY_PRINT);     

    //save new json file to disk
    file_put_contents($outputFolder.$theFileName.".json", $jsonResult);

    //print out each file name as they are completed
    echo $theFileName."<br>";

}

echo 'Done with all files.';

Example XML input read from disk:

<?xml version= "1.0" encoding="UTF-8"?>
<RootArea>
 <TransactionNum>
  <TranNum>20180501_11355088774_001</TranNum>
  <TranDate>20180501</TranDate>
 </TransactionNum>
 <SectionA>
  <Type>Type A</Type>
  <Name>Example Name</Name>
  <Num>04419883333</Num>
  <Code>MP0614343</Code>
  <AnotherCode>0140000422053</AnotherCode>
 </SectionA> 
 <SectionB>
  <TotTarItems>3333</TotTarItems>
  <TotModItems>3333</TotModItems>
  <TotTarAmount>55555</TotTarAmount>
  <TotModAmount>222</TotModAmount>
 </SectionB>
</RootArea>

Example JSON output being successfully saved to disk:

{
    "TransactionNum": {
        "TranNum": "20180501_11355088774_001",
        "TranDate": "20180501"
    },
    "SectionA": {
        "Type": "Type A",
        "Name": "Example Name",
        "Num": "04419883333",
        "Code": "MP0614343",
        "AnotherCode": "0140000422053"
    },
    "SectionB": {
        "TotTarItems": "3333",
        "TotModItems": "3333",
        "TotTarAmount": "55555",
        "TotModAmount": "222"
    }
}

I would very much like this process to simply include my example <RootArea></RootArea> tags within the output, but nothing I have attempted has worked.


Solution

  • The simple_load_xml() method assumes an implicit root level, so it won't be added to your json unless you explicitly add it yourself. There are a few ways you can go about that, but here's one ugly way that doesn't mess with your xml, and keeps your original json_encode method intact :

    //load xml file from disk
    $xmlFileLoaded = simplexml_load_file($xmlFile); 
    $root = $xmlFileLoaded->getName();  // this grabs the root of your xml
    
    //json encode the xml
    $jsonResult = json_encode($xmlFileLoaded, JSON_FORCE_OBJECT | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION | JSON_PRETTY_PRINT);     
    
    // wrap your json with the root element.  Ugly, yes, but it gets the job done
    $finaloutput = json_encode( array($root => json_decode($jsonResult) ) )
    
    //save new json file to disk
    file_put_contents( $outputFolder.$theFileName.".json", $finaloutput );