首页 > 代码库 > xml simpleXML_load_file(), simpleXML_load_string()
xml simpleXML_load_file(), simpleXML_load_string()
xml.xml文件
<?xml version=‘1.0‘?>
<man>
<att>
<name>lin3615</name>
<sex>M</sex>
<age>26</age>
</att>
<att>
<name>lin361500</name>
<sex>mmm</sex>
<age>20</age>
</att>
</man>
用simpleXML_load_file()实现
<?php
$ff = ‘http://localhost/test.xml‘;
$str = simpleXML_load_file($ff);
print_r($str);
foreach($str->att as $v) print_r($v);
function get_contents($url){
if (ini_get("allow_url_fopen") == "1") {
$response = file_get_contents($url);
}else{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
}
return $response;
}
用simpleXML_load_string()实现
<?php
$ff = get_contents("http://localhost/test/test.xml");
$str = simpleXML_load_string($ff);
print_r($str);
foreach($str->att as $v) print_r($v);
function get_contents($url){
if (ini_get("allow_url_fopen") == "1") {
$response = file_get_contents($url);
}else{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
}
return $response;
}
结果都为:
SimpleXMLElement Object
(
[att] => Array
(
[0] => SimpleXMLElement Object
(
[name] => lin3615
[sex] => M
[age] => 26
)
[1] => SimpleXMLElement Object
(
[name] => lin361500
[sex] => mmm
[age] => 20
)
)
)
SimpleXMLElement Object
(
[name] => lin3615
[sex] => M
[age] => 26
)
SimpleXMLElement Object
(
[name] => lin361500
[sex] => mmm
[age] => 20
)