将 XML 转换为 JSON 并保存到磁盘后根标记丢失

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

我正在尝试创建一个简单的 XML 到 JSON 转换器,从一个文件夹读取并输出到另一个文件夹。这个过程似乎工作正常,但我似乎无法让这个东西包含 XML 的根标签。请在下面找到示例:

<?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.';

示例 XML 从磁盘读取的输入:

<?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>

示例 JSON 输出成功保存到磁盘:

{
    "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"
    }
}

我非常希望这个过程在输出中简单地包含我的示例 <RootArea></RootArea> 标记,但我尝试的任何事情都没有奏效。

您可以通过在当前根节点之上添加另一个虚拟根 xml 节点来解决这个问题,如下所示

$xmlstr = str_replace('<?xml version= "1.0" encoding="UTF-8"?>','<?xml version= "1.0" encoding="UTF-8"?><newroot>',file_get_contents($xmlFile))."</newroot>";
$xmlFileLoaded = simplexml_load_string($xmlstr); 

simple_load_xml() 方法采用隐式根级别,因此它不会添加到您的 json 中,除非您自己明确添加它。有几种方法可以解决这个问题,但这里有一种丑陋的方法,它不会扰乱您的 xml,并保持原始 json_encode 方法完好无损:

//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 );