0
我正在研究沒有PHP的simpleXML可用的服務器,並且需要將XML字符串轉換爲JSON,因此我使用xml_parse_into_struct()
來完成工作。然後,我試圖將它生成的兩個數組與array_combine()
合併,然後使用json_encode()
返回XML字符串的JSON表示。我遇到的問題是,兩個陣列xml_parse_into_struct()
創建的長度不相同,因此array_combine()
正在引發錯誤。我相信這可能是由於XML字符串有一堆具有相同名稱的元素造成的。如何將這個xml字符串轉換爲JSON並保留所有元素及其屬性而不使用simpleXML?使用xml_parse_into_struct將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));
請問服務器已經'的DomDocument()'來查詢XML到數組中? – Parfait
感謝您的回覆@Parfait - 'DOMDocument()'似乎不可用。 – exeleon
這是一個私人的本地服務器嗎?如果是這樣,請詢問IT部門或者您自己安裝那些默認啓用的[擴展](http://php.net/manual/en/simplexml.installation.php)。而且大多數商業網站主機都會啓用這些擴展。如果沒有核心xml類,你自己就會發展成PHP。 – Parfait