当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > XML SimpleXML

PHP
PHP 安全邮件
MySQL 介绍
连接 MySQL
创建 MySQL
MySQL 插入记录
MySQL 选择记录
MySQL Where
MySQL Order By
MySQL 记录更新
MySQL 删除记录
PHP ODBC
XML Expat Parser
XML SimpleXML
PHP 数组参考
PHP Calendar
PHP Date
PHP Directory
PHP Filesystem
PHP FTP
PHP HTTP

PHP 中的 XML SimpleXML


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1073 ::
收藏到网摘: n/a

SimpleXML handles the most common XML tasks and leaves the rest for other extensions.
PHP SimpleXML是用来处理最为普通的XML任务的


What is SimpleXML?
什么是SimpleXML?

SimpleXML is new in PHP 5. It is an easy way of getting an element's attributes and text, if you know the XML document's layout.
SimpleXML是作为一个全新的函数在PHP5中出现的。如果你非常了解XML文档的布局的话,使用SimpleXML将可以很方便的获取一个元素的属性和文本内容。

Compared to DOM or the Expat parser, SimpleXML just takes a few lines of code to read text data from an element.
与DOM或Expat解析器相比较,SimpleXML只需简单的几行代码就可以实现阅读一个元素中数据的功能。

SimpleXML converts the XML document into an object, like this:
SimpleXML将XML文档转化为一个对象,如下所示:

  • Elements - Are converted to single attributes of the SimpleXMLElement object. When there's more than one element on one level, they're placed inside an array
    元素[Elements] —将被转换成SimpleXMLElement对象的单独属性;当在同一水平上不止一个元素时,它们将以数组的形式被保存下来
  • Attributes - Are accessed using associative arrays, where an index corresponds to the attribute name
    属性[Attributes] — 通过与之相关的数组进行访问;在这个数组中,每个索引指数[index]都与属性名称相对应
  • Element Data - Text data from elements are converted to strings. If an element has more than one text node, they will be arranged in the order they are found
    元素数据[Element Data] — 元素中的文本数据将被转换成字符串。如果一个元素包含不止一个的文本节点[text node],它们将按照顺序进行排列

SimpleXML is fast and easy to use when performing basic tasks like:
SimpleXML处理下面列举的工作将非常简单迅速:

  • Reading XML files
    阅读XML文件
  • Extracting data from XML strings
    从XML字符串中提取数据
  • Editing text nodes or attributes
    编辑一个文本节点或属性

However, when dealing with advanced XML, like namespaces, you are better off using the Expat parser or the XML DOM.
然而,在处理高级XML时,比如:名称空间[namespace],你最好不要使用Expat或者XML DOM。


Installation
安装

As of PHP 5.0, the SimpleXML functions are part of the PHP core. There is no installation needed to use these functions.
在PHP5.0中,XML Expat解析器函数是PHP核心的一部分,所以无需安装。


Using SimpleXML
使用SimpleXML

Below is an XML file:
下面是一个XML文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

We want to output the element names and data from the XML file above.
我们希望输出上述XML文件中的元素名称和数据。

Here's what to do:
下面列举运行步骤:

  1. Load the XML file
    加载XML文件
  2. Get the name of the first element
    获取第一个元素的名称
  3. Create a loop that will trigger on each child node, using the children() function
    使用children()函数创建一个可以激发每个子节点的循环。
  4. Output the element name and data for each child node
    为每个子节点输出元素的名称和数据

Example
案例

<?php
$xml = simplexml_load_file("test.xml");
echo $xml->getName() . "<br />";
foreach($xml->children() as $child) { echo $child->getName() . ": " . $child . "<br />"; }
?>

The output of the code above will be:
讲述代码将输出下面的结果:

note
to: Tove
from: Jani
heading: Reminder
body: Don't forget me this weekend!


More PHP SimpleXML
更多关于PHP SimpleXML的知识

For more information about the PHP SimpleXML functions, visit our PHP SimpleXML Reference.
如果你想学习更多关PHP SimpleXML的函数,请访问我们的PHP SimpleXML参数。

评论 (0) All

登陆 | 还没注册?