search
Homephp教程php手册php+xml编程之SimpleXML的应用实例

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

Jun 06, 2016 pm 08:12 PM
phpsimplexmlxmlapplicationprogramming

这篇文章主要介绍了php+xml编程之SimpleXML的应用,实例分析了SimpleXML函数操作XML文件的方法,需要的朋友可以参考下

本文实例讲述了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><span>复制代码</span> 代码如下:</p><p>
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><span>复制代码</span> 代码如下:</p><p>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>



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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools