使用xml_parse_into_struct将XML转换为JSON

时间:2016-02-22 16:12:26

标签: php json xml

我正在开发一个没有PHP的simpleXML的服务器,需要将XML字符串转换为JSON,所以我使用xml_parse_into_struct()来完成工作。然后我尝试将它生成的两个数组与array_combine()组合,然后使用json_encode()返回XML字符串的JSON表示。我遇到的问题是两个数组xml_parse_into_struct()正在创建的长度不相同,因此array_combine()会抛出错误。我相信这可能是由具有一堆同名元素的XML字符串引起的。如何在不使用simpleXML的情况下将此xml字符串转换为JSON并保留所有元素及其属性?

代码:

$string =
'<?xml version="1.0" encoding="UTF-8"?>
<session-data xmlns="http://oracle.com/determinations/engine/sessiondata/10.2">
   <entity id="global">
      <instance id="global">
         <attribute id="employer" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="legal" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="foodtype" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="app" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="org" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="tel" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="jfu" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="trans" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="serv" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="cit" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="street" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="zip" type="boolean" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="ddt" type="boolean" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="prov" type="boolean" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="prov2" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="teh" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="dis" type="boolean" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="num" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="ssn" type="text" inferred="false">
            <text-val>social</text-val>
         </attribute>
         <attribute id="eop" type="boolean" inferred="false">
            <boolean-val>false</boolean-val>
         </attribute>
         <attribute id="inst" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="cig" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="nips" type="boolean" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="pay" type="number" inferred="true">
            <number-val>200.0</number-val>
         </attribute>
         <attribute id="data" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="ent" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="ent2" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="person" type="boolean" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="activity" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="tob" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="start" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="tate" type="boolean" inferred="false">
            <boolean-val>false</boolean-val>
         </attribute>
         <attribute id="procs" type="text" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="officers" type="text" inferred="false">
            <text-val>3 or more Officers</text-val>
         </attribute>
         <attribute id="time" type="boolean" inferred="false">
            <boolean-val>false</boolean-val>
         </attribute>
         <attribute id="year" type="boolean" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="box" type="boolean" inferred="false">
            <unknown-val />
         </attribute>
         <attribute id="digi" type="boolean" inferred="false">
            <boolean-val>false</boolean-val>
         </attribute>
         <attribute id="store" type="boolean" inferred="false">
            <boolean-val>true</boolean-val>
         </attribute>
         <attribute id="rent" type="boolean" inferred="false">
            <boolean-val>false</boolean-val>
         </attribute>
         <attribute id="tain" type="boolean" inferred="false">
            <boolean-val>false</boolean-val>
         </attribute>
         <attribute id="goo" type="boolean" inferred="false">
            <boolean-val>false</boolean-val>
         </attribute>
         <attribute id="building" type="boolean" inferred="false">
            <boolean-val>false</boolean-val>
         </attribute>
         <attribute id="guard" type="boolean" inferred="false">
            <boolean-val>false</boolean-val>
         </attribute>
         <attribute id="hard" type="boolean" inferred="false">
            <boolean-val>true</boolean-val>
         </attribute>
            <unknown-val />
         </attribute>
         <entity id="regulated" complete="false" inferred="false" />
      </instance>
   </entity>
</session-data>';


$p = xml_parser_create();
xml_parse_into_struct($p, $string, $vals, $index);
xml_parser_free($p);



$jsonArray = array();
foreach (array_combine( $index, $vals ) as $name => $value) {
    $jsonArray[] = array('name' => $name, 'value' => $value);
}

echo "Encoded JSON:<br>";
print_r($json = json_encode($jsonArray));

1 个答案:

答案 0 :(得分:1)

考虑进一步操纵$vals数组以考虑嵌套树结构(兄弟姐妹,孩子等)。按原样,xml_parse_struct()返回所有相同级别的对象,只有属性作为嵌套数组。下面使用这个原始输出数组,并通过xml树的设计,定义其他数组,其中包含嵌套结构的相应值。

PHP 脚本

// EXTRACT XML CONTENT
$p = xml_parser_create();
xml_parse_into_struct($p, $string, $vals, $index);
xml_parser_free($p);

// INITIALIZE PARENT/CHILD ARRAYS
$session = []; $entity = []; $instance = [];
$attribute = []; $boolean = []; $unknown = [];

// POPULATE "OPEN" ELEMENTS WITH ATTRIBUTES
foreach($vals as $v){    
    if($v['tag'] == "SESSION-DATA" and $v['type']=="open"){
        $session['session-data'] = $v['attributes'];
    }
    if($v['tag'] == "ENTITY" and $v['type']=="open"){
        $entity = $v['attributes'];
    }
    if($v['tag'] == "INSTANCE" and $v['type']=="open"){
        $instance = $v['attributes'];
    }    
}

// NEST BOOLEAN-VAL/UNKNOWN-VAL UNDER ATTRIBUTE
$j=0;
for($i=0; $i<sizeof($vals); $i++){
    if($vals[$i]['tag'] == "ATTRIBUTE" and $vals[$i]['type']=="open"){
        $attribute[$j] = $vals[$i]['attributes'];
        $j++;
    }
    if($vals[$i]['tag'] == "BOOLEAN-VAL" and $vals[$i]['type']=="complete"){
        $boolean['value'] = $vals[$i]['value'];
        $attribute[$j-1]['BOOLEAN-VAL'] = $boolean;
    }    
    if($vals[$i]['tag'] == "UNKNOWN-VAL" and $vals[$i]['type']=="complete"){
        $unknown['value'] = $vals[$i]['type']=="complete";
        $attribute[$j-1]['UNKNOWN-VAL'] = $unknown;
    }
}

// ADD CHILD ARRAYS TO PARENTS
$instance['attributes'] = $attribute;
$entity['instance'] = $instance;
$session['session-data']['entity'] = $entity;

echo "Encoded JSON:<br>";
print_r($json = json_encode($session));

// PRETTY PRINT OUTPUT TO FILE
$json = json_encode($session, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
file_put_contents("Output.json", $json);

Pretty Print JSON 输出(根据online sources,是一个有效的json)

{
    "session-data": {
        "XMLNS": "http://oracle.com/determinations/engine/sessiondata/10.2",
        "entity": {
            "ID": "global",
            "instance": {
                "ID": "global",
                "attributes": [
                    {
                        "ID": "employer",
                        "TYPE": "text",
                        "INFERRED": "false",
                        "UNKNOWN-VAL": {
                            "value": true
                        }
                    },
                    {
                        "ID": "legal",
                        "TYPE": "text",
                        "INFERRED": "false",
                        "UNKNOWN-VAL": {
                            "value": true
                        }
                    },
                    {
                        "ID": "foodtype",
                        "TYPE": "text",
                        "INFERRED": "false",
                        "UNKNOWN-VAL": {
                            "value": true
                        }
                    },
...