2016-02-22 141 views
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)); 
+0

請問服務器已經'的DomDocument()'來查詢XML到數組中? – Parfait

+0

感謝您的回覆@Parfait - 'DOMDocument()'似乎不可用。 – exeleon

+0

這是一個私人的本地服務器嗎?如果是這樣,請詢問IT部門或者您自己安裝那些默認啓用的[擴展](http://php.net/manual/en/simplexml.installation.php)。而且大多數商業網站主機都會啓用這些擴展。如果沒有核心xml類,你自己就會發展成PHP。 – Parfait

回答

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

漂亮的打印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 
         } 
        }, 
...