php xml拓展,PHP扩展之XML操作(一)——SimpleXML

一、简介及安装

SimpleXML 扩展提供了一个非常简单和易于使用的工具集,能将 XML 转换成一个带有一般属性选择器和数组迭代器的对象。

此扩展需要 libxml PHP 扩展。这表示需要使用 --enable-libxml ,尽管这将隐式完成因为 libxml 是缺省开启的。

需要PHP5及以上版本。

此扩展默认为启用,编译时可通过下列选项禁用: --disable-simplexml

二、使用示例

基本使用

Example #1 Include file example.php with XML string

$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 

include '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 

include '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.

include '

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值