方法1
function parseXML($xmlSrc){
if(empty($xmlSrc)){
return false;
}
$array = array();
$xml = simplexml_load_string($xmlSrc);
$encode = Utils::getXmlEncode($xmlSrc);
if($xml && $xml->children()) {
foreach ($xml->children() as $node){
if($node->children()) {
$k = $node->getName();
$nodeXml = $node->asXML();
$v = substr($nodeXml, strlen($k)+2, strlen($nodeXml)-2*strlen($k)-5);
} else {
$k = $node->getName();
$v = (string)$node;
}
if($encode!="" && $encode != "UTF-8") {
$k = iconv("UTF-8", $encode, $k);
$v = iconv("UTF-8", $encode, $v);
}
$array[$k] = $v;
}
}
return $array;
}
function getXmlEncode($xml) {
$ret = preg_match ("/<?xml[^>]* encoding=\"(.*)\"[^>]* ?>/i", $xml, $arr);
if($ret) {
return strtoupper ( $arr[1] );
} else {
return "";
}
}
xml转array
function xmlToArray($xml){
libxml_disable_entity_loader(true);
$values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $values;
}
方法2
<?xml version = "1.0" encoding = "UTF-8"?>
<find>
<set_number>002740</set_number>
<no_records>000000088</no_records>
<no_entries>000000088</no_entries>
<session-id>HMUSRLFCHC7FY2RDGYDQPARXSDSEDGEBL3VTSVTRQBKAR5DTEA</session-id>
</find>
$xml =simplexml_load_string($xmls);
$xmljson= json_encode($xml);
$xml=json_decode($xmljson,true);
var_dump($xml);
array(4) {
["set_number"]=>
string(6) "002742"
["no_records"]=>
string(9) "000000088"
["no_entries"]=>
string(9) "000000088"
["session-id"]=>
string(50) "KV47UCJGAVCGANM2286ENMNXPLGPH9JPX7R9S751TGKDX6TSGR"
}