搜尋
首頁php教程php手册php中simplexml_load_string使用实例

在php中simplexml_load_string() 函数把 XML 字符串载入对象中,下面我来给大家介绍几个简单实例的同时也介绍在使用simplexml_load_string的一些需要注意的事项.

先用一段代码重现一下问题,乍一看,结果很让人费解,代码如下:

<?php  
$string = <<<EOF  
<data>  
<foo><bar>hello</bar></foo>  
<foo><bar>world</bar></foo>  
</data>  
EOF; 
 
$data = simplexml_load_string($string); 
 
print_r($data);  
print_r($data->foo);  
 
?>
SimpleXMLElement Object  
(  
[foo] => Array  
(  
[0] => SimpleXMLElement Object  
(  
[bar] => hello  
)  
[1] => SimpleXMLElement Object  
(  
[bar] => world  
)  
)
)  
SimpleXMLElement Object  
(  
[bar] => hello  
)

明明print_r显示foo是一个有两个bar元素的数组,但是最后却仅仅显示了一个bar元素,原因其实很简单,在如上所示simplexml_load_string的结果里,foo并不是数组,而是一个迭代对象.

可以这样确认,代码如下:

foreach ($data->foo as $v) print_r($v); 
foreach ($data->children() as $v) print_r($v);

看来,print_r或者var_dump之类的表象并不完全可信,假如我们获取的XML数据如下,可以使用curl、fsockopen等方式获取,代码如下:

<?xml version="1.0" encoding="UTF-8" 
<dict num="219" id="219" name="219"> 
 <key>你好</key> 
 <pos></pos> 
 <acceptation>Array;Array;Array;</acceptation> 
 <sent> 
  <orig>Haven&#39;t seen you for a long time. How are you?</orig> 
  <trans>多日不见了,你好吗?</trans> 
 </sent> 
 <sent> 
  <orig>Hello! How are you?</orig> 
  <trans>嘿,你好?</trans> 
 </sent> 
 <sent> 
  <orig>Hello, Brooks!How are you?</orig> 
  <trans>喂,布鲁克斯!你好吗?</trans> 
 </sent> 
 <sent> 
  <orig>Hi, Barbara, how are you?</orig> 
  <trans>嘿,芭芭拉,你好吗?</trans> 
 </sent> 
 <sent> 
  <orig>How are you? -Quite well, thank you.</orig> 
  <trans>你好吗?-很好,谢谢你。</trans> 
 </sent> 
</dict>

经过simplexml_load_string得到如下代码:

SimpleXMLElement Object 
( 
[@attributes] => Array 
( 
[num] => 219 
[id] => 219 
[name] => 219 
) 
 
[key] => 你好www.phprm.com 
[pos] => SimpleXMLElement Object 
( 
) 
 
[acceptation] => Array;Array;Array; 
[sent] => Array 
( 
[0] => SimpleXMLElement Object 
( 
[orig] => Haven&#39;t seen you for a long time. How are you? 
[trans] => 多日不见了,你好吗? 
) 
 
[1] => SimpleXMLElement Object 
( 
[orig] => Hello! How are you? 
[trans] => 嘿,你好? 
) 
 
[2] => SimpleXMLElement Object 
( 
[orig] => Hello, Brooks!How are you? 
[trans] => 喂,布鲁克斯!你好吗? 
) 
 
[3] => SimpleXMLElement Object 
( 
[orig] => Hi, Barbara, how are you? 
[trans] => 嘿,芭芭拉,你好吗? 
) 
 
[4] => SimpleXMLElement Object 
( 
[orig] => How are you? -Quite well, thank you. 
[trans] => 你好吗?-很好,谢谢你。 
) 
 
) 
 
)

我们在PHP语言中可以用以下方法取得我们想要的值,代码如下:

".trim($xmldata->sent[0]->orig); //Haven't seen you for a long time. How are you? 
echo "
".trim($xmldata->key); //你好

   


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版