Home > php教程 > php手册 > body text

php+xml编程之SimpleXML的应用实例,simplexml应用实例

WBOY
Release: 2016-06-13 09:16:35
Original
884 people have browsed it

php+xml编程之SimpleXML的应用实例,simplexml应用实例

本文实例讲述了php+xml编程之SimpleXML的应用。分享给大家供大家参考。具体如下:

SimpleXML的核心思想:以面向对象的方式来操作xml文件,它会将xml文件的所有元素都转成对象。

xml文档:words.xml

复制代码 代码如下:




 boy
 男孩


 girl
 女孩


 teacher
 老师


 beauty
 美女


simplexml使用实例:

复制代码 代码如下:

echo "

";<br>
$words = simplexml_load_file("words.xml");//返回数组对象,可以用print_r()或var_dump()查看<br>
var_dump($words);<br>
?>
<p>读取内容:<br>
</p><p class="codetitle"><span><u>复制代码</u></span> 代码如下:</p><p class="code" id="code60226">
echo "</p><pre class="brush:php;toolbar:false">";<br>
$words = simplexml_load_file("words.xml");//返回数组对象,可以用print_r()或var_dump()查看<br>
//echo $words->word[2];<br>
foreach($words->word as $row){//$row还是一个对象<br>
 print_r($row);<br>
 echo $row->ch."<hr>"; //其实,$row->ch还是一个对象,只不能它能echo出来<br>
}<br>
?><br>
第二段代码输出结果:<br>
<p class="codetitle"><span><u>复制代码</u></span> 代码如下:</p><p class="code" id="code99747">SimpleXMLElement Object<br>
(<br>
    [en] => boy<br>
    [ch] => 男孩<br>
)<br>
男孩<br>
SimpleXMLElement Object<br>
(<br>
    [en] => girl<br>
    [ch] => 女孩<br>
)<br>
女孩<br>
SimpleXMLElement Object<br>
(<br>
    [en] => teacher<br>
    [ch] => 老师<br>
)<br>
老师</p>
<p>希望本文所述对大家的php+xml程序设计有所帮助。</p>







Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template