一、简介及安装
SimpleXML 扩展提供了一个非常简单和易于使用的工具集,能将 XML 转换成一个带有一般属性选择器和数组迭代器的对象。
此扩展需要 libxml PHP 扩展。这表示需要使用 --enable-libxml ,尽管这将隐式完成因为 libxml 是缺省开启的。
需要PHP5及以上版本。
此扩展默认为启用,编译时可通过下列选项禁用: --disable-simplexml
二、使用示例
基本使用
Example #1 Include file example.php with XML string<?php
$xmlstr = <<PHP: Behind the Parser
Ms. Coder
Onlivia Actora
Mr. Coder
El ActÓr
So, this language. It's like, a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.
PHP solves all my web problems
7
5
XML;
?>
The simplicity of SimpleXML appears most clearly when one extracts a string or number from a basic XML document.
Example #2 Getting <?phpinclude 'example.php';
$movies = new SimpleXMLElement($xmlstr);
echo $movies->movie[0]->plot;?>
以上例程会输出:So, this language. It's like, a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.
Accessing elements within an XML document that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.
Example #3 Getting <?phpinclude 'example.php';
$movies = new SimpleXMLElement($xmlstr);
echo $movies->movie->{'great-lines'}->line;?>
以上例程会输出:PHP solves all my web problems
Example #4 Accessing non-unique elements in SimpleXML
When multiple instances of an element exist as children of a single parent element, normal iteration techniques apply.<?phpinclude 'example.php';
$