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

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

Jun 06, 2016 pm 08:12 PM
php simplexml xml application programming

这篇文章主要介绍了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>
?&gt;
<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-&gt;word[2];<br>
foreach($words-&gt;word as $row){//$row还是一个对象<br>
 print_r($row);<br>
 echo $row-&gt;ch."<hr>"; //其实,$row-&gt;ch还是一个对象,只不能它能echo出来<br>
}<br>
?&gt;<br>
第二段代码输出结果:<br>
<p><span>复制代码</span> 代码如下:</p><p>SimpleXMLElement Object<br>
(<br>
    [en] =&gt; boy<br>
    [ch] =&gt; 男孩<br>
)<br>
男孩<br>
SimpleXMLElement Object<br>
(<br>
    [en] =&gt; girl<br>
    [ch] =&gt; 女孩<br>
)<br>
女孩<br>
SimpleXMLElement Object<br>
(<br>
    [en] =&gt; teacher<br>
    [ch] =&gt; 老师<br>
)<br>
老师</p>
<p>希望本文所述对大家的php+xml程序设计有所帮助。</p>



Copy after login
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

How do you parse and process HTML/XML in PHP?

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

PHP Program to Count Vowels in a String

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

The Key to Coding: Unlocking the Power of Python for Beginners

Java Made Simple: A Beginner's Guide to Programming Power Java Made Simple: A Beginner's Guide to Programming Power Oct 11, 2024 pm 06:30 PM

Java Made Simple: A Beginner's Guide to Programming Power

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Create the Future: Java Programming for Absolute Beginners

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder

See all articles