我在文件中有以下XML:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<conjunctiva>
<od>
<conjnctivalchlais examname="Conjunctival Chalasis">
<negative elem_name="elem_ConjnctivalOd_neg">ASDFGH</negative>
<positive elem_name="elem_ConjnctivalOd_Pos"/>
<pos_4 elem_name="elem_ConjnctivalOd_pos4"/>
<pos_3 elem_name="elem_ConjnctivalOd_pos3"/>
<pos_2 elem_name="elem_ConjnctivalOd_pos2"/>
<pos_1 elem_name="elem_ConjnctivalOd_pos1"/>
<t elem_name="elem_ConjnctivalOd_T"/>
</conjnctivalchlais>
</od>
</conjunctiva>
我正在使用以下代码来解析xml:
$csvFileName = "con2.xml";
$conjunctiva = simplexml_load_file($csvFileName);
foreach($conjunctiva->od as $od){
if(gettype($od) == "object"){
$keys = get_object_vars($od);
$keys2 = array_keys($keys);
for($i = 0;$i < sizeof($keys2);$i++){
if(gettype($keys[$keys2[$i]]) == "object"){
$keys3 = get_object_vars($keys[$keys2[$i]]);
$keys4 = array_keys($keys3);
for($j = 0;$j < sizeof($keys4);$j++){
if(gettype($keys3[$keys4[$j]]) == "object"){
}else if(gettype($keys3[$keys4[$j]]) == "string"){
}else if(gettype($keys3[$keys4[$j]]) == "array"){
}
}
}
}
}
}
我想将XML放在数组中,其中属性值为键,节点值为value。但它没有用。
答案 0 :(得分:0)
$xml = simplexml_load_file('con2.xml');
$xmlArray = json_decode(json_encode((array)$xml), TRUE);
//this should give you the proper array with key=>value pair
print_r($xmlArray);
如果要从SImpleXml对象访问属性,请使用$xml->node
如果您的节点包含node-name
之类的hipns,那么请使用它$node->{node-name}
,
修改强>
现在$xmlArray
应该为您提供正确的数组