日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

xml simpleXML_load_file(), simpleXML_load_string()

發布時間:2025/3/15 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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
)

轉載于:https://www.cnblogs.com/lin3615/p/3876669.html

總結

以上是生活随笔為你收集整理的xml simpleXML_load_file(), simpleXML_load_string()的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。