http://blog.csdn.net/guoguo1980/archive/2008/05/12/2436318.aspx<wbr><div><br></div> <div> <span style="line-height:21px; font-family:verdana,sans-serif"></span> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> SimpleXML</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 介绍</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> SimpleXML提供了一种简单,直观的方法来处理XML。它只有一个单一类型的类,三个函数和六个方法。</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 使用SimpleXML</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> SimpleXMLElement 类是这个扩展中所有操作的核心类。可以用new关键字直接创建这种类,或是使用simplexml_load_file()或 simplexml_load_string()函数返回这种类。本文将使用清单7-1的XML文档来说明如何使用SimpleXML,将此文档命名为 sml.xml。<br><span id="more-17" style="line-height:21px"></span><br> 清单7-1 sml.xml</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><?</span><span style="color:green">xml</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">version</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">1.0</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">encoding</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">UTF-8</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">?></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:gray">!</span><span style="color:rgb(0,0,139)">DOCTYPE</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">book</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">PUBLIC</span><span style="color:gray"></span><span style="color:rgb(139,0,0)">"</span><span style="color:red">-//OASIS//DTD DocBook XML V4.1.2//EN</span><span style="color:rgb(139,0,0)">"</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(139,0,0)">"</span><span style="color:red">http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">book</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">lang</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">en</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">bookinfo</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">title</span><span style="color:olive">></span><span style="color:gray">SimpleXML in PHP 5</span><span style="color:olive"></</span><span style="color:green">title</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">author</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">firstname</span><span style="color:olive">></span><span style="color:gray">Rob</span><span style="color:olive"></</span><span style="color:green">firstname</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">surname</span><span style="color:olive">></span><span style="color:gray">Richards</span><span style="color:olive"></</span><span style="color:green">surname</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"></</span><span style="color:green">author</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">copyright</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">year</span><span style="color:olive">></span><span style="color:gray">2005</span><span style="color:olive"></</span><span style="color:green">year</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">holder</span><span style="color:olive">></span><span style="color:gray">Rob Richards</span><span style="color:olive"></</span><span style="color:green">holder</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"></</span><span style="color:green">copyright</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"></</span><span style="color:green">bookinfo</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">preface</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">title</span><span style="color:olive">></span><span style="color:gray">Using SimpleXML</span><span style="color:olive"></</span><span style="color:green">title</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">para</span><span style="color:olive">></span><span style="color:gray">An example DOM Tree using DocBook.</span><span style="color:olive"></</span><span style="color:green">para</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"></</span><span style="color:green">preface</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">chapter</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">id</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">navigation</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">title</span><span style="color:olive">></span><span style="color:gray">Accessing Elements</span><span style="color:olive"></</span><span style="color:green">title</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">para</span><span style="color:olive">></span><span style="color:gray">Elements are accessed as properties</span><span style="color:olive"></</span><span style="color:green">para</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">para</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:rgb(255,165,0)"><![CDATA[</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)"> <?php</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)"> $data = '<?xml version="1.0"?></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)"> <root>content</root>';</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)"> $sxe = simplexml_load_string($data);</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)"> var_dump($sxe);</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)"> ?></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)"> ]]></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"></</span><span style="color:green">para</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"></</span><span style="color:green">chapter</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"></</span><span style="color:green">book</span><span style="color:olive">></span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 创建一个SimpleXMLElement对象</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 使用new关键字创建</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$xml</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red"><root><node1>Content</node1></root></span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$sxe</span><span style="color:gray">=</span><span style="color:green">new</span><span style="color:gray"></span><span style="color:blue">SimpleXMLElement</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$xml</span><span style="color:olive">)</span><span style="color:gray">;</span><span style="color:rgb(255,165,0)">//SimpleXMLElement Object ( [node1] => Content )</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 使用simplexml_load_string()创建</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$xml</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red"><root><node1>Content</node1></root></span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$sxe</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_string</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$xml</span><span style="color:olive">)</span><span style="color:gray">;</span><span style="color:rgb(255,165,0)">//SimpleXMLElement Object ( [node1] => Content )</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 如何选择这两种创建SimpleXMLElement的方法呢?simplexml_load_string()提供了更多的函数,比如控制解析选项的能力。如果不需要这些额外的函数的话就可以凭个人爱好选择一种方法。</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 使用simplexml_load_file()从一个URI创建</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"><li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$sxe</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">filename.xml</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">)</span><span style="color:gray">;</span> </li></ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> simplexml_load_string()和simplexml_load_file()都有一个必需的参数和可选的参数。从PHP5.1开始simplexml_load_file()多了一个用来控制解析行为的第三个参数。</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)">/* Prototype for PHP 5.0 */</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:blue">string</span><span style="color:gray"></span><span style="color:blue">data</span><span style="color:gray"></span><span style="color:olive">[</span><span style="color:gray">,</span><span style="color:blue">string</span><span style="color:gray"></span><span style="color:blue">class_name</span><span style="color:olive">])</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)">/* Prototype for PHP 5.1 */</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:blue">string</span><span style="color:gray"></span><span style="color:blue">data</span><span style="color:gray"></span><span style="color:olive">[</span><span style="color:gray">,</span><span style="color:blue">string</span><span style="color:gray"></span><span style="color:blue">class_name</span><span style="color:gray"></span><span style="color:olive">[</span><span style="color:gray">,</span><span style="color:blue">int</span><span style="color:gray"></span><span style="color:blue">options</span><span style="color:olive">]])</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 保存XML数据</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 与DOM扩展一样,SimpleXML也提供了一个用来输出XML内容的方法asXML()。可以用这个方法以字符串或文件形式输出这个文档或文档中的某个节点。</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$xml</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red"><root><node1>content</node1></root></span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$sxe</span><span style="color:gray">=</span><span style="color:green">new</span><span style="color:gray"></span><span style="color:blue">SimpleXMLElement</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$xml</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$sxe</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$sxe</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">test.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 输出:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><?</span><span style="color:green">xml</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">version</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">1.0</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">?></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">root</span><span style="color:olive">><</span><span style="color:green">node1</span><span style="color:olive">></span><span style="color:gray">content</span><span style="color:olive"></</span><span style="color:green">node1</span><span style="color:olive">></</span><span style="color:green">root</span><span style="color:olive">></span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 访问元素节点</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 在SimpleXML中,可以直接通过元素的名称来访问特定的元素。</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 访问元素</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 当一个文档被载入SimpleXML时,文档被看成是一个SimpleXML对象,文档中的所有元素都被看成是该对象的属性。</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue"><?php</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)">/* Access the bookinfo child element of the book element */</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$bookinfo</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)">/* Access the title child element from the bookinfo element */</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$title</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$bookinfo</span><span style="color:gray">-></span><span style="color:blue">title</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">?></span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 如果使用DOM来访问title,代码如下</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$dom</span><span style="color:gray">=</span><span style="color:green">new</span><span style="color:gray"></span><span style="color:blue">DOMDocument</span><span style="color:olive">()</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$dom</span><span style="color:gray">-></span><span style="color:blue">load</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$dom</span><span style="color:gray">-></span><span style="color:blue">documentElement</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:green">foreach</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">childNodes</span><span style="color:gray"></span><span style="color:green">as</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$node</span><span style="color:olive">)</span><span style="color:gray"></span><span style="color:olive">{</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:green">if</span><span style="color:gray"></span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$node</span><span style="color:gray">-></span><span style="color:blue">nodeName</span><span style="color:gray">==</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">bookinfo</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">)</span><span style="color:gray"></span><span style="color:olive">{</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:green">foreach</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$node</span><span style="color:gray">-></span><span style="color:blue">childNodes</span><span style="color:gray"></span><span style="color:green">as</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$child</span><span style="color:olive">)</span><span style="color:gray"></span><span style="color:olive">{</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:green">if</span><span style="color:gray"></span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$child</span><span style="color:gray">-></span><span style="color:blue">nodeName</span><span style="color:gray">==</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">title</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">)</span><span style="color:gray"></span><span style="color:olive">{</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:rgb(0,0,139)">$node</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$child</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:green">break</span><span style="color:gray"></span><span style="color:maroon">2</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive">}</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive">}</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive">}</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive">}</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:green">if</span><span style="color:gray"></span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$node</span><span style="color:olive">)</span><span style="color:gray"></span><span style="color:olive">{</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:rgb(0,0,139)">$title</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$node</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive">}</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 显然SimpleXML对的起它的名字。</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 访问内容</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue"><?php</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$bookinfo</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$title</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$bookinfo</span><span style="color:gray">-></span><span style="color:blue">title</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)">/* Object examined with var_dump */</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">var_dump</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$title</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)">/* Using print with element containing text-only content */</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(139,0,0)">"</span><span style="color:red">Title:</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">.</span><span style="color:rgb(0,0,139)">$title</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$author</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$bookinfo</span><span style="color:gray">-></span><span style="color:blue">author</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)">/* Object examined with var_dump */</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">var_dump</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$author</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)">/* Using print with element containing child elements */</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(139,0,0)">"</span><span style="color:red">Author:</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">.</span><span style="color:rgb(0,0,139)">$author</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">?></span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 这段代码检查了两个SimpleXMLElement对象,$author和$title。两者的区别是$author元素有包含子元素而$title元素只包含一个文本节点。<br> 输出如下:<br> object(SimpleXMLElement)#4 (1) {<br> [0]=><br> string(18) “SimpleXML in PHP 5″<br> }<br> Title: SimpleXML in PHP 5<br> object(SimpleXMLElement)#6 (2) {<br> [”firstname”]=><br> string(3) “Rob”<br> [”surname”]=><br> string(8) “Richards”<br> }<br> Author:</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 检查输出结果可以发现,$title是一个包含有文本内容的SimpleXMLElement对象,索引0表示元素的文本内容,当打印$title时,文本内容将以字符串形式返回。</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> $author元素有两个子元素,从输出结果可以看出,这些子元素被看成SimpleXMLElement对象的属性,这些属性的值是它们对应的节点包含的内容。用print输出$author时,输出结果是空格和换行符。</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 如果一个元素无子元素,只包含文本内容,那么可以将此元素所对应的SimpleXMLElement对象视为一个字符串来使用,有些情况下,为了获得以字符串形式返回文本内容,必须执行类型转换操作:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"><li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$titlecontent</span><span style="color:gray">=</span><span style="color:olive">(</span><span style="color:blue">string</span><span style="color:olive">)</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$title</span><span style="color:gray">;</span> </li></ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 有 子元素的元素所对应的SimpleXMLElement对象将返回该对象直属的文本节点,而不是任一子元素的内容。如果用print输出$author, 将得到一个27字符长度,包含空格和换行符的字符。 Neither of the child elements,firstname or surname, nor their content is returned in the string.</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 为了理解最后一点,可以运行一些下面这段代码:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$doc</span><span style="color:gray">=</span><span style="color:green">new</span><span style="color:gray"></span><span style="color:blue">SimpleXMLElement</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red"><root>some<child1>subtext</child1>thing</root></span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$doc</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 使用迭代对象</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> SimpleXMLElement 对象在大多数情况下是可迭代的,可以用这个特性来访问文档中多个元素名一样的节点,如清单7-1中的para元素。在使用元素名作为属性来访问一个元素 时,SimpleXMLElement对象不是一个单一节点的存取器,它实际上是作为属性访问的元素名节点的集合。直接利用元素名作为属性访问实际上访问 访问这个集合中的第一个元素。</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 看下这段代码</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$para</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">para</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$para</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:green">foreach</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$para</span><span style="color:gray"></span><span style="color:green">AS</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$node</span><span style="color:olive">)</span><span style="color:gray"></span><span style="color:olive">{</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$node</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive">}</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 这 段代码中,$para变量就是para元素的集合,实际上包含了两个元素。如果直接访问$para的话实际上是访问第一个para元素,利用迭代可以看出 $para所包含的内容是两个元素。其中CDATA节点被看做是纯文本的内容,其中包含的空格和换行符都会被如实输出。</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 用迭代的方式来访问所有的元素显然不太实际,有时候我们希望访问结果集中的某个特定的元素,这时可以使用从0开始的索引来访问这个结果集,例如:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$para</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">para</span><span style="color:olive">[</span><span style="color:maroon">1</span><span style="color:olive">]</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(139,0,0)">"</span><span style="color:red">Content:</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">.</span><span style="color:rgb(0,0,139)">$para</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:green">foreach</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$para</span><span style="color:gray"></span><span style="color:green">AS</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$node</span><span style="color:olive">)</span><span style="color:gray"></span><span style="color:olive">{</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(139,0,0)">"</span><span style="color:red">Iter Content:</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">.</span><span style="color:rgb(0,0,139)">$node</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive">}</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 运行这段代码会发现foreach循环失效了,这是因为SimpleXML知道你只是在寻找结果集中某个特定的元素,这种情况下对象是不可迭代的。</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> Caution:使用索引方式访问一个SimpleXMLElement对象会返回一个不可以迭代的对象,因为它是一个单一的元素而不是一个元素集。</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 访问未知元素</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 在不知道XML文档的结构的情况下可以利用SimpleXML中的children()方法来返回一个可以迭代方式访问的某个元素的所有子元素的SimpleXML对象。如:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$author</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:gray">-></span><span style="color:blue">author</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$children</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$author</span><span style="color:gray">-></span><span style="color:blue">children</span><span style="color:olive">()</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:green">foreach</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$children</span><span style="color:gray"></span><span style="color:green">AS</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$child</span><span style="color:olive">)</span><span style="color:gray"></span><span style="color:olive">{</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$child</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive">}</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 上述代码用children()方法返回了author节点下的所有子元素,然后用foreach循环输出。也可以使用索引方式访问返回的子元素,如echo $children[1];。</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 理解PHP对象函数</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> SimpleXMLElement对象的属性是动态的,因为这些属性是由对象实例决定的,而不是由类本身决定的。在PHP中,可以利用get_object_vars()函数来返回某个对象的所有属性,返回的结果是一个包含属性和值的数组,如:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$props</span><span style="color:gray">=</span><span style="color:blue">get_object_vars</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$author</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:green">foreach</span><span style="color:gray"></span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$props</span><span style="color:gray"></span><span style="color:green">AS</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$name</span><span style="color:gray">=></span><span style="color:rgb(0,0,139)">$value</span><span style="color:olive">)</span><span style="color:gray"></span><span style="color:olive">{</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$name</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">:</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">.</span><span style="color:rgb(0,0,139)">$value</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive">}</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 输出:<br> firstname: Rob<br> surname: Richards<br> 这段代码访问的子元素都只包含文本内容,所以返回的数组只包含属性名和值,对于一个包含许多子节点的元素,返回的结果稍微复杂点:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$props</span><span style="color:gray">=</span><span style="color:blue">get_object_vars</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">var_dump</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$props</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 输出:</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> array(3) {<br> [”title”]=><br> string(18) “SimpleXML in PHP 5″<br> [”author”]=><br> object(SimpleXMLElement)#4 (2) {<br> [”firstname”]=><br> string(3) “Rob”<br> [”surname”]=><br> string(8) “Richards”<br> }<br> [”copyright”]=><br> object(SimpleXMLElement)#5 (2) {<br> [”year”]=><br> string(4) “2005″<br> [”holder”]=><br> string(12) “Rob Richards”<br> }<br> }</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 使用DOM互操作</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 另一种访问未知元素的方法是使用DOM,可以将一个节点导入DOM扩展,然后使用DOM的属性和方法来处理。</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$author</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:gray">-></span><span style="color:blue">author</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$children</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$author</span><span style="color:gray">-></span><span style="color:blue">children</span><span style="color:olive">()</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:green">foreach</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$children</span><span style="color:gray"></span><span style="color:green">AS</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$child</span><span style="color:olive">)</span><span style="color:gray"></span><span style="color:olive">{</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:rgb(255,165,0)">/* Import node into DOM, and get nodeName */</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:rgb(0,0,139)">$element</span><span style="color:gray">=</span><span style="color:blue">dom_import_simplexml</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$child</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:rgb(0,0,139)">$name</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$element</span><span style="color:gray">-></span><span style="color:blue">nodeName</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$name</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">:</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">.</span><span style="color:rgb(0,0,139)">$child</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive">}</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 将节点导入DOM扩展时并没有创建一个节点的副本(copy),而是直接访问导入的节点(JIMMY注:这个概念很重要)。</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 修改内容</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 利用SimpleXML修改元素内容非常方便,你可以改变或移除树中的某个元素,但是不能直接在树中添加一个元素。要添加一个元素,可以使用DOM的互操作性:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$xml</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red"><root><node1>content</node1></root></span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$sxe</span><span style="color:gray">=</span><span style="color:green">new</span><span style="color:gray"></span><span style="color:blue">SimpleXMLElement</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$xml</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$dom</span><span style="color:gray">=</span><span style="color:blue">dom_import_simplexml</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$sxe</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$dom</span><span style="color:gray">-></span><span style="color:blue">appendChild</span><span style="color:olive">(</span><span style="color:green">new</span><span style="color:gray"></span><span style="color:blue">DOMElement</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">node2</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">,</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">content2</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">))</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$sxe</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 输出:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><?</span><span style="color:green">xml</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">version</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">1.0</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">?></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">root</span><span style="color:olive">><</span><span style="color:green">node1</span><span style="color:olive">></span><span style="color:gray">content</span><span style="color:olive"></</span><span style="color:green">node1</span><span style="color:olive">><</span><span style="color:green">node2</span><span style="color:olive">></span><span style="color:gray">content2</span><span style="color:olive"></</span><span style="color:green">node2</span><span style="color:olive">></</span><span style="color:green">root</span><span style="color:olive">></span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 编辑文本内容</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 可以利用SimpleXML的属性赋值方法来直接编辑一个元素的内容,要主意的是如果文档中有多个元素名一样的元素,如果没有使用索引来指定要编辑哪个元素时PHP将发出一个警告。如:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)">/* Modify an unspecified para element where multiple para elements exist */</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">para</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">Removed CDATA</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 输出:<br> Warning: main() [/phpmanual/function.main.html]: Cannot assign to an array of nodes<br> (duplicate subnodes or attr detected)</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 必须给para指定索引告诉程序你要编辑哪个元素:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">para</span><span style="color:olive">[</span><span style="color:maroon">1</span><span style="color:olive">]</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">Removed CDATA</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 输出:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">chapter</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">id</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">navigation</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">title</span><span style="color:olive">></span><span style="color:gray">Acessing Elements</span><span style="color:olive"></</span><span style="color:green">title</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">para</span><span style="color:olive">></span><span style="color:gray">Elements are accessed as properties</span><span style="color:olive"></</span><span style="color:green">para</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">para</span><span style="color:olive">></span><span style="color:gray">Removed CDATA</span><span style="color:olive"></</span><span style="color:green">para</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"></</span><span style="color:green">chapter</span><span style="color:olive">></span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 这样,第二个para元素的内容被改为Removed CDATA。如果要编辑一个在文档中唯一存在的元素可不必指定索引,直接修改。如修改title:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">title</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">New Title</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">para</span><span style="color:olive">[</span><span style="color:maroon">1</span><span style="color:olive">]</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">Removed CDATA</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 输出:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">chapter</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">id</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">navigation</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">title</span><span style="color:olive">></span><span style="color:gray">New Title</span><span style="color:olive"></</span><span style="color:green">title</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">para</span><span style="color:olive">></span><span style="color:gray">Elements are accessed as properties</span><span style="color:olive"></</span><span style="color:green">para</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">para</span><span style="color:olive">></span><span style="color:gray">Removed CDATA</span><span style="color:olive"></</span><span style="color:green">para</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"></</span><span style="color:green">chapter</span><span style="color:olive">></span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 强烈建议使用索引来编辑元素,除非你对文档的结果非常确定。使用索引来编辑title元素会比较安全,如$book->chapter->title[0] = “New Title”;这行代码用索引[0]指定要编辑第一个title。</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 编辑有子树的元素</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$cholder</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:gray">-></span><span style="color:blue">copyright</span><span style="color:gray">-></span><span style="color:blue">holder</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$cholder</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">No Book Info</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$cholder</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 输出:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">holder</span><span style="color:olive">></span><span style="color:gray">Rob Richards</span><span style="color:olive"></</span><span style="color:green">holder</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">bookinfo</span><span style="color:olive">></span><span style="color:gray">No Book Info</span><span style="color:olive"></</span><span style="color:green">bookinfo</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray">Warning: SimpleXMLElement::asXML() [/phpmanual/function.asXML.html]: Node no</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray">longer exists in N:\CVS Projects\php5\Debug_TS\booksxe.php on line 7</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 这 段代码中,首先将文档中的holder元素赋值给$cholder变量,然后打印该变量。bookinfo元素包含有title,author和 copyright子树,它的内容被字符串No Book Info代替,从bookinfo的输出结果可以看出,它的子树被清空并且被字符串代替了。接着试图再次打印$cholder变量的XML内容,程序输出 一个警告,这个变量依然是一个SimpleXMLElement对象,但它所属的节点在bookinfo元素改变时已经被破坏了。</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 另一种情况。将bookinfo元素中的子元素用字符串 代替。</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red"><title>SimpleXML in PHP 5</title></span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 如果你认为上述代码将bookinfo中的内容清空后再给bookinfo创建了一个子节点title,那么你错了。输出结果是<br> <title>SimpleXML in PHP 5</title><br> 实际上bookinfo元素的子元素都被移除了,但是新赋值的XML数据被转义成文本内容,而不是一个新的子元素。</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 如果想用一个子树代替另一个子树,可以利用DOM扩展:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$bookinfo</span><span style="color:gray">=</span><span style="color:blue">dom_import_simplexml</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)">/* 移除bookinfo元素下的所有子元素*/</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:green">while</span><span style="color:gray"></span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$bookinfo</span><span style="color:gray">-></span><span style="color:blue">firstChild</span><span style="color:olive">)</span><span style="color:gray"></span><span style="color:olive">{</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$bookinfo</span><span style="color:gray">-></span><span style="color:blue">removeChild</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$bookinfo</span><span style="color:gray">-></span><span style="color:blue">firstChild</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive">}</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$bookinfo</span><span style="color:gray">-></span><span style="color:blue">appendChild</span><span style="color:olive">(</span><span style="color:green">new</span><span style="color:gray"></span><span style="color:blue">DOMElement</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">title</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">,</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">SimpleXML in PHP 5</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">))</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 输出结果:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">bookinfo</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">title</span><span style="color:olive">></span><span style="color:gray">SimpleXML in PHP 5</span><span style="color:olive"></</span><span style="color:green">title</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"></</span><span style="color:green">bookinfo</span><span style="color:olive">></span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 移除元素</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 可以用PHP内置函数unset()来将一个元素从树中移除。unset()的参数必须是一个SimpleXMLElement,用属性方法来访问要移除的元素。例如,从chapter节点移除title元素:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">para</span><span style="color:olive">[</span><span style="color:maroon">1</span><span style="color:olive">]</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">Removed CDATA</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:green">unset</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">title</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 上述代码执行后,chapter的结构为:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">chapter</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">id</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">navigation</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">para</span><span style="color:olive">></span><span style="color:gray">Elements are accessed as properties</span><span style="color:olive"></</span><span style="color:green">para</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">para</span><span style="color:olive">></span><span style="color:gray">Removed CDATA</span><span style="color:olive"></</span><span style="color:green">para</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"></</span><span style="color:green">chapter</span><span style="color:olive">></span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 将这个结果与下面代码执行的结果想比较:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">para</span><span style="color:olive">[</span><span style="color:maroon">1</span><span style="color:olive">]</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">Removed CDATA</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$title</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">title</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:green">unset</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$title</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 输出结果</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">chapter</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">id</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">navigation</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">title</span><span style="color:olive">></span><span style="color:gray">Acessing Elements</span><span style="color:olive"></</span><span style="color:green">title</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">para</span><span style="color:olive">></span><span style="color:gray">Elements are accessed as properties</span><span style="color:olive"></</span><span style="color:green">para</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">para</span><span style="color:olive">></span><span style="color:gray">Removed CDATA</span><span style="color:olive"></</span><span style="color:green">para</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"></</span><span style="color:green">chapter</span><span style="color:olive">></span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> title元素没有被移除,unset函数只对$title变量作用并没有将title元素从树中移除。</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 在移除一个元素时必须注意,用索引来指定的特定元素不会被移除:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">para</span><span style="color:olive">[</span><span style="color:maroon">1</span><span style="color:olive">]</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">Removed CDATA</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:green">unset</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">title</span><span style="color:olive">[</span><span style="color:maroon">0</span><span style="color:olive">])</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 输出:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">chapter</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">id</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">navigation</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">title</span><span style="color:olive">></span><span style="color:gray">Acessing Elements</span><span style="color:olive"></</span><span style="color:green">title</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">para</span><span style="color:olive">></span><span style="color:gray">Elements are accessed as properties</span><span style="color:olive"></</span><span style="color:green">para</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">para</span><span style="color:olive">></span><span style="color:gray">Removed CDATA</span><span style="color:olive"></</span><span style="color:green">para</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"></</span><span style="color:green">chapter</span><span style="color:olive">></span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 如果要移除所有的para元素时可以利用下面代码:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:green">unset</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">para</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 输出:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">chapter</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">id</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">navigation</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">title</span><span style="color:olive">></span><span style="color:gray">Acessing Elements</span><span style="color:olive"></</span><span style="color:green">title</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"></</span><span style="color:green">chapter</span><span style="color:olive">></span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 问题是如果你只想移除其中的一个para元素时要怎么办。这时可以再次用到DOM扩展:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$chapter</span><span style="color:gray">=</span><span style="color:blue">dom_import_simplexml</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$node</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$chapter</span><span style="color:gray">-></span><span style="color:blue">lastChild</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:green">while</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$node</span><span style="color:olive">)</span><span style="color:gray"></span><span style="color:olive">{</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:green">if</span><span style="color:gray"></span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$node</span><span style="color:gray">-></span><span style="color:blue">nodeName</span><span style="color:gray">==</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">para</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">)</span><span style="color:gray"></span><span style="color:olive">{</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:rgb(0,0,139)">$chapter</span><span style="color:gray">-></span><span style="color:blue">removeChild</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$node</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:rgb(0,0,139)">$node</span><span style="color:gray">=</span><span style="color:green">NULL</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:green">break</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive">}</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:rgb(0,0,139)">$node</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$node</span><span style="color:gray">-></span><span style="color:blue">previousSibling</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive">}</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 输出:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">chapter</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">id</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">navigation</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">title</span><span style="color:olive">></span><span style="color:gray">Acessing Elements</span><span style="color:olive"></</span><span style="color:green">title</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">para</span><span style="color:olive">></span><span style="color:gray">Elements are accessed as properties</span><span style="color:olive"></</span><span style="color:green">para</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"></</span><span style="color:green">chapter</span><span style="color:olive">></span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 所幸的是PHP5.2开始已经支持删除用索引指定的元素了:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:green">unset</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">para</span><span style="color:olive">[</span><span style="color:maroon">1</span><span style="color:olive">])</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 访问属性</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 读取属性</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 下面的代码输出了book元素中的lang属性</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:olive">[</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">lang</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">]</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 访问用索引指定的元素的属性:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:olive">[</span><span style="color:maroon">0</span><span style="color:olive">][</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">id</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">]</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 在不知道属性名的情况下可以用attributes()方法来输出属性:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:green">foreach</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">attributes</span><span style="color:olive">()</span><span style="color:gray"></span><span style="color:green">AS</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$attribute</span><span style="color:olive">)</span><span style="color:gray"></span><span style="color:olive">{</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$attribute</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive">}</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 如果要获得位置的属性名,可以使用DOM扩展:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:green">foreach</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">chapter</span><span style="color:gray">-></span><span style="color:blue">attributes</span><span style="color:olive">()</span><span style="color:gray"></span><span style="color:green">AS</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$attribute</span><span style="color:olive">)</span><span style="color:gray"></span><span style="color:olive">{</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:rgb(0,0,139)">$att</span><span style="color:gray">=</span><span style="color:blue">dom_import_simplexml</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$attribute</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$att</span><span style="color:gray">-></span><span style="color:blue">nodeName</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$attribute</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive">}</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 修改属性<br> 修改属性的值跟修改元素的值一样,直接对其赋值就可以了:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:olive">[</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">lang</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">]</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">es</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:olive">[</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">lang</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">]</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 添加一个属性也很简单,如果对一个不存在的属性名进行赋值就给元素创建一个新属性</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:gray">-></span><span style="color:blue">author</span><span style="color:gray">-></span><span style="color:blue">firstname</span><span style="color:olive">[</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">prefix</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">]</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">Mr.</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:gray">-></span><span style="color:blue">author</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 输出:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">author</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">firstname</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">prefix</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">Mr.</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">></span><span style="color:gray">Rob</span><span style="color:olive"></</span><span style="color:green">firstname</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">surname</span><span style="color:olive">></span><span style="color:gray">Richards</span><span style="color:olive"></</span><span style="color:green">surname</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"></</span><span style="color:green">author</span><span style="color:olive">></span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 移除属性</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 移除属性也用到unset()函数:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxml.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:gray">-></span><span style="color:blue">author</span><span style="color:gray">-></span><span style="color:blue">firstname</span><span style="color:olive">[</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">prefix</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">]</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">Mr.</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:gray">-></span><span style="color:blue">author</span><span style="color:gray">-></span><span style="color:blue">firstname</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:green">unset</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:gray">-></span><span style="color:blue">author</span><span style="color:gray">-></span><span style="color:blue">firstname</span><span style="color:olive">[</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">prefix</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">])</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:gray">-></span><span style="color:blue">author</span><span style="color:gray">-></span><span style="color:blue">firstname</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 输出:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">firstname</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">prefix</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">Mr.</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">></span><span style="color:gray">Rob</span><span style="color:olive"></</span><span style="color:green">firstname</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">firstname</span><span style="color:olive">></span><span style="color:gray">Rob</span><span style="color:olive"></</span><span style="color:green">firstname</span><span style="color:olive">></span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 扩展SimpleXMLElement类</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:green">class</span><span style="color:gray"></span><span style="color:blue">mySXE</span><span style="color:gray"></span><span style="color:green">extends</span><span style="color:gray"></span><span style="color:blue">SimpleXMLElement</span><span style="color:gray"></span><span style="color:olive">{</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:green">function</span><span style="color:gray"></span><span style="color:blue">appendChild</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$name</span><span style="color:gray">,</span><span style="color:rgb(0,0,139)">$content</span><span style="color:olive">)</span><span style="color:gray"></span><span style="color:olive">{</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:rgb(0,0,139)">$dom</span><span style="color:gray">=</span><span style="color:blue">dom_import_simplexml</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$this</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:rgb(0,0,139)">$dom</span><span style="color:gray">-></span><span style="color:blue">appendChild</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$dom</span><span style="color:gray">-></span><span style="color:blue">ownerDocument</span><span style="color:gray">-></span><span style="color:blue">createElement</span><span style="color:olive">(</span><span style="color:rgb(0,0,139)">$name</span><span style="color:gray">,</span><span style="color:rgb(0,0,139)">$content</span><span style="color:olive">))</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive">}</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive">}</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 当实例化扩展的类时,文档的每个节点对象的类型都是扩展类的类型。</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$sxe</span><span style="color:gray">=</span><span style="color:green">new</span><span style="color:gray"></span><span style="color:blue">mySXE</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">"</span><span style="color:red"><root><node1></node1></root></span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$sxe</span><span style="color:gray">-></span><span style="color:blue">node1</span><span style="color:gray">-></span><span style="color:blue">appendChild</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">node2</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">,</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">content</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$sxe</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 输出:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><?</span><span style="color:green">xml</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">version</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">1.0</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">?></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">root</span><span style="color:olive">><</span><span style="color:green">node1</span><span style="color:olive">><</span><span style="color:green">node2</span><span style="color:olive">></span><span style="color:gray">content</span><span style="color:olive"></</span><span style="color:green">node2</span><span style="color:olive">></</span><span style="color:green">node1</span><span style="color:olive">></</span><span style="color:green">root</span><span style="color:olive">></span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 使用new方法可以用来处理字符串类型的XML,如果XML保存在一个文件中,那么可以将扩展的类名作为第二个参数传给simplexml_load_string或simplexml_load_file</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$sxe</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_string</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">"</span><span style="color:red"><root><node1></node1></root></span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">,</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">mySXE</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$sxe</span><span style="color:gray">-></span><span style="color:blue">node1</span><span style="color:gray">-></span><span style="color:blue">appendChild</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">node2</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">,</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">content</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$sxe</span><span style="color:gray">-></span><span style="color:blue">asXML</span><span style="color:olive">()</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 输出结果与用new关键字输出的结果一样。</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 在SimpleXML中使用命名空间</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 将清单7-1的内容改为</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><?</span><span style="color:green">xml</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">version</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">1.0</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">encoding</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">UTF-8</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">?></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">book</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">lang</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">en</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"><</span><span style="color:green">bookinfo</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">xmlns</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">http://www.example.com/ns1</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">title</span><span style="color:olive">></span><span style="color:gray">SimpleXML in PHP 5</span><span style="color:olive"></</span><span style="color:green">title</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">author</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">firstname</span><span style="color:olive">></span><span style="color:gray">Rob</span><span style="color:olive"></</span><span style="color:green">firstname</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">surname</span><span style="color:olive">></span><span style="color:gray">Richards</span><span style="color:olive"></</span><span style="color:green">surname</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"></</span><span style="color:green">author</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">copyright</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">year</span><span style="color:olive">></span><span style="color:gray">2005</span><span style="color:olive"></</span><span style="color:green">year</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"><</span><span style="color:green">holder</span><span style="color:olive">></span><span style="color:gray">Rob Richards</span><span style="color:olive"></</span><span style="color:green">holder</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"></</span><span style="color:green">copyright</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:gray"> </span><span style="color:olive"></</span><span style="color:green">bookinfo</span><span style="color:olive">></span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:olive"></</span><span style="color:green">book</span><span style="color:olive">></span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 如果试图用普通的方法来访问元素或属性,你会分析这并不可行,例如:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxmlns.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:olive">[</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">lang</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">]</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:gray">-></span><span style="color:blue">title</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 输出的结果是两行空白。</p> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 在 访问命名空间节点前,必须使用children()和attributes()方法。这两个方法不仅可以在没有指定参数的时候使用,也可以在指定一个 URI命名空间作为参数使用。如果一个SimleXMLElement对象是从这两个方法返回的,那么你就可以像普通的元素和属性一样访问命名空间下的元 素和属性:</p> <div style="line-height:25px"> <ol title="Double click to hide line number." style="margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:35px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none"> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$book</span><span style="color:gray">=</span><span style="color:blue">simplexml_load_file</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">'</span><span style="color:red">sxmlns.xml</span><span style="color:rgb(139,0,0)">'</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)">/* 返回</span><span style="color:blue">http://www.example.com/ns2</span><span style="color:rgb(255,165,0)">命名空间下的所有属性 */</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$bookatts</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">attributes</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">http://www.example.com/ns2</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$bookatts</span><span style="color:olive">[</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">lang</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">]</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)">/* 返回</span><span style="color:blue">http://www.example.com/ns1</span><span style="color:rgb(255,165,0)">命名空间下的所有元素*/</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$bookns</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$book</span><span style="color:gray">-></span><span style="color:blue">children</span><span style="color:olive">(</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">http://www.example.com/ns1</span><span style="color:rgb(139,0,0)">"</span><span style="color:olive">)</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$bookinfo</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$bookns</span><span style="color:gray">-></span><span style="color:blue">bookinfo</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(255,165,0)">/* 重置命名空间来访问非命名空间的元素 */</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:rgb(0,0,139)">$nonsbkinfo</span><span style="color:gray">=</span><span style="color:rgb(0,0,139)">$bookinfo</span><span style="color:gray">-></span><span style="color:blue">children</span><span style="color:olive">()</span><span style="color:gray">;</span> </li> <li style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px; list-style-type:decimal"> <span style="color:blue">print</span><span style="color:gray"></span><span style="color:rgb(0,0,139)">$nonsbkinfo</span><span style="color:gray">-></span><span style="color:blue">title</span><span style="color:gray">.</span><span style="color:rgb(139,0,0)">"</span><span style="color:navy">\n</span><span style="color:rgb(139,0,0)">"</span><span style="color:gray">;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> children ()和attributes()方法可以被看做是过滤器,如果没有参数或者传递一个NULL作为参数,这两个方法将返回非命名空间下的元素或属性;否则将 返回特定命名空间下的元素或属性。在重置之前,命名空间仍然起作用并且被子节点继承。例如,使用$bookinfo被设置为命名空间 http://www.example.com/ns1的对象,可以使用 print $bookinfo->author->firstname来但因author中的firstname元素。所有的元素都在命名空间下,因此 你在创建$bookinfo对象时不必一直使用children()设置命名空间。</p> </div> </wbr>