1

我正在尝试创建一个简单的 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>标签包含在输出中,但我尝试过的任何尝试都没有奏效。

4

2 回答 2

1

您可以通过在当前根节点之上添加另一个虚拟根 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); 
于 2018-08-23T17:16:46.247 回答
1

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 );
于 2018-08-23T17:24:22.457 回答