当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > XML SimpleXML
SimpleXML handles the most common XML tasks and leaves the rest for other extensions.
PHP SimpleXML是用来处理最为普通的XML任务的
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文档转化为一个对象,如下所示:
SimpleXML is fast and easy to use when performing basic tasks like:
SimpleXML处理下面列举的工作将非常简单迅速:
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。
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核心的一部分,所以无需安装。
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:
下面列举运行步骤:
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! |
For more information about the PHP SimpleXML functions, visit our PHP SimpleXML Reference.
如果你想学习更多关PHP SimpleXML的函数,请访问我们的PHP SimpleXML参数。