首页 > 代码库 > PHP解析xml

PHP解析xml

<?xml version="1.0" encoding="UTF-8"?><ZIP_result><result name="ZipSearchXML" /><result version="1.01" /><result request_url="http%3A%2F%2Fzip.cgis.biz%2Fxml%2Fzip.php%3Fzn%3D5300001" /><result request_zip_num="5300001" /><result request_zip_version="none" /><result result_code="1" /><result result_zip_num="5300001" /><result result_zip_version="0" /><result result_values_count="1" />	<ADDRESS_value>	<value state_kana="?????" />	<value city_kana="????????" />	<value address_kana="????" />	<value company_kana="none" />	<value state="大阪府" />	<value city="大阪市北区" />	<value address="梅田" />	<value company="none" />	</ADDRESS_value></ZIP_result>

 文件名:test.xml

方法一:读取文件到一个字符串,通过解析字符串:

$url  = "http://localhost/test/test.xml";$xmlString=file_get_contents($url);$xml_array=simplexml_load_string($xmlString); var_dump($xml_array);	$state = $xml_array->ADDRESS_value->value[4][‘state‘]; 	$city = $xml_array->ADDRESS_value->value[5][‘city‘];	$address =$xml_array->ADDRESS_value->value[6][‘address‘]; 	$state = iconv("UTF-8","EUC-JP",$state);	$city = iconv("UTF-8","EUC-JP",$city);	$address =iconv("UTF-8","EUC-JP",$address);	echo ‘$state=‘.$state.‘<br>‘;	echo ‘$city=‘.$city.‘<br>‘;	echo ‘$address=‘.$address.‘<br>‘;

 方法二:直接解析xml文件

$xml = simplexml_load_file("http://localhost/test/test.xml")

 print_r($xml);

PHP解析xml