码迷,mamicode.com
首页 > Web开发 > 详细

PHP解析xml

时间:2015-02-03 19:21:37      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

<?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

标签:

原文地址:http://www.cnblogs.com/sunxun/p/4270733.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!