Home  >  Article  >  Backend Development  >  php+xml编程之SimpleXML的应用实例_php技巧

php+xml编程之SimpleXML的应用实例_php技巧

WBOY
WBOYOriginal
2016-05-16 20:25:011012browse

本文实例讲述了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>
?>

读取内容:

复制代码 代码如下:
echo "
";<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>
?>

第二段代码输出结果:
复制代码 代码如下:
SimpleXMLElement Object
(
    [en] => boy
    [ch] => 男孩
)
男孩
SimpleXMLElement Object
(
    [en] => girl
    [ch] => 女孩
)
女孩
SimpleXMLElement Object
(
    [en] => teacher
    [ch] => 老师
)
老师

希望本文所述对大家的php+xml程序设计有所帮助。

Statement:
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