SimpleXML学习笔记

SimpleXML学习笔记

simplexml 使用实例

搞了几天php处理xml文件,终于有点头绪,记录下来分享一下。
simplexml 是php处理xml文件的一个方法,另一个是dom处理,这里只说simplexml。
目前php处理xml用的比较多,比较成熟的还是dom。但dom在速度和代码量上还是比较受人诟病的。

simplexml的有些函数需要比较高版本的php,建议是php5.2以上。

一,新建xml

方法1(普通新建文件):

$fp=fopen('111.xml','w');
$xmlContent='<?xml version="1.0" encoding="utf-8" ?> ';
$xmlContent.='<navlist>';
$xmlContent.='<nav id="1">';
$xmlContent.='<name>我我我</nname> ';
$xmlContent.='<color>#000000</ncolor> ';
$xmlContent.='</nav>';

$xmlContent.='<nav id="2">';

$xmlContent.='<name>你你你</nname> ';
$xmlContent.='<color>#ffffff</ncolor> ';
$xmlContent.='</nav>';

$xmlContent.='<nav id="3">';
$xmlContent.='<nname>他他他</nname> ';
$xmlContent.='<ncolor>#cccccc</ncolor> ';
$xmlContent.='</nav>';

$xmlContent.='</navlist>';
fwrite($fp,$xmlContent);
fclose($fp);

方法2(使用simplexml新建):

$fp=fopen(111.xml','w');
$xmlContent='<?xml version="1.0" encoding="utf-8" ?><navlist></navlist>';
fwrite($fp,$xmlContent);
fclose($fp);


$xml=simplexml_load_file('111.xml');


$app=$xml->addChild('nav');
$app->addAttribute('id',"1");
$app->addChild('name','我我我');
$app->addChild('color','#000000');


$app=$xml->addChild('nav');
$app->addAttribute('id',"2");
$app->addChild('name','你你你');
$app->addChild('color','#ffffff);

$app=$xml->addChild('nav');
$app->addAttribute('id',"3");
$app->addChild('name','他他他');
$app->addChild('color','#cccccc);


$xml->asXML('111.xml');

可以看到第一种方法其实只是普通的写入文件,第二种方法才是使用simplexml新建xml文件,但为什么我要把第一种普通方法放前面呢?因为本人测试了一下两种方法的运行时间,发现第一种方法使用的时间更少,所以建议使用普通的新建文件方法新建xml文件就行了!

新建的文件结构如下:

<?xml version="1.0" encoding="utf-8"?>
<navlist>
 <nav id="1">
  <name>我我我</nname>
  <color>#000000</ncolor>
 </nav>
 <nav id="2">
  <name>你你你</nname>
  <color>#ffffff</ncolor>
 </nav>
 <nav id="3">
  <name>他他他</nname>
  <color>#cccccc</ncolor>
 </nav>
</navlist>

二,读取xml

if($xml=simplexml_load_file('111.xml')){
foreach($xml->nav as $list){
$value[]=get_object_vars($list);
}
}else{
echo('load xml error!');
}

get_object_vars函数是把对象转化为数组,这样$value就是一个Php的普通数组了,大家可以print_r一下这个数组,Php数组的读取就不用我说了吧。

三,修改xml

1,新建节点

$xml=simplexml_load_file('111.xml');
$app=$xml->addChild('nav');
$app->addAttribute('id',"4");
$app->addChild('name','它它它');
$app->addChild('color','#000000');
$xml->asXML(111.xml');

将追加作为最后一个节点

2,修改节点

$xml=simplexml_load_file('111.xml');

$xg=$xml->xpath("/navlist/nav[@id=‘3’]");
$xg[0]->name="他啊他啊";
$xg[0]->color="#444444";
$xml->asXML(111.xml');

修改了nav属性id为3的节点

3,删除节点

删除节点用unset函数就行,但要删除指定节点比较麻烦,删除指定节点只能使用节点的索引值。

比如删除第二个节点(id为2的):

$xml=simplexml_load_file('111.xml');

unset($xml->nav[1]);

$xml->asXML(111.xml');

索引从0开始,所以第二个节点的索引是1。

如果想通过指定的属性删除,则需要循环所有节点获取指定属性的节点索引
如下:

$xml=simplexml_load_file('111.xml');

$i=0;

foreach($xml as $dup){
$sc=$dup->attributes();
if($sc['id']==2){
unset($xml->nav[$i]);
}
$i++;
}

$xml->asXML('111.xml');

四,删除xml

if(file_exists('111.xml')){
unlink('111.xml');
}







--------------------------------------------------------------------------------------------------------------------------------------------------


相关的三个代码文件:

data.dtd 定义XML合法词汇
data.xml XML文档
index.php 主索引文件

--------------------------------------------------------------------------------

代码如下:

data.dtd

  1. <!ELEMENTfriendLinkLib(friendLink)>
  2. <!ELEMENTfriendLink(name,url,info,logoPath,qqMsn)>
  3. <!ELEMENTname(#PCDATA)>
  4. <!ELEMENTurl(#PCDATA)>
  5. <!ELEMENTinfo(#PCDATA)>
  6. <!ELEMENTlogoPath(#PCDATA)>
  7. <!ELEMENTqqMsn(#PCDATA)>
  8. <!ATTLISTfriendLinkidCDATA#REQUIRED>
  9. <!ATTLISTfriendLinkisPass(0|1)"0">
  10. <!ATTLISTfriendLinkorderIdCDATA#IMPLIED>
  11. <!ATTLISTfriendLinkisImage(0|1)"1">
  12. <!ATTLISTfriendLinkisDisplay(0|1)"1">
  13. <!ATTLISTfriendLinkposition(supe|bbs|home)"supe">

data.xml

  1. <?xmlversion="1.0"encoding="gbk"?>
  2. <!DOCTYPEFriendLinkSYSTEM"http://localhost/eg/data.dtd">
  3. <friendLinkLib>
  4. <friendLinkorderId="0"id="1"position="supe">
  5. <name>邯城正午</name>
  6. <url>http://www.cityxii.cn</url>
  7. <info>邯郸人的网上家园</info>
  8. <logoPath>http://localhost/images/xmllogo.jpg</logoPath>
  9. <qqMsn>QQ:250660802</qqMsn>
  10. </friendLink>
  11. <friendLinkorderId="1"id="2"position="supe">
  12. <name>邯城社区</name>
  13. <url>http://www.cityxii.cn</url>
  14. <info>邯郸人的网上家园</info>
  15. <logoPath>http://localhost/images/xmllogo.jpg</logoPath>
  16. <qqMsn>QQ:250660802</qqMsn>
  17. </friendLink>
  18. </friendLinkLib>

index.php

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. <html>
  2. <head>
  3. <metahttp-equiv="Content-Type"Content="text/html;charset=gbk"/>
  4. <title>使用SimpleXML解析xml文件</title></head>
  5. <body>
  6. <?php
  7. include'FL_inc/friendLink.inc.php';
  8. $xml=simplexml_load_file("./FL_data/data.xml");
  9. /*
  10. *1.访问节点值,并执行转码
  11. echo'PrintTheContentofnode:';
  12. $name=$xml->friendLink->name;
  13. echoiconv('utf-8','gbk',$name),'<br/>';
  14. *2.访问属性值,注意:只有中文需要转码,数字和英文以及英文标点无需转码
  15. echo'PrintTheAttofrootnode:';
  16. //echoiconv('utf-8','gbk',$xml->friendLink[1]['id']);
  17. echo$xml->friendLink[1]['id'];
  18. echo'<hr/>END<hr/>';
  19. */
  20. /*
  21. *3.增加节点,并且设置节点值,以及节点属性。
  22. *注意:增加值的时候需要转码,同样只是中文需要转码。因为页面index.php的编码是GBK,所以要转成utf-8
  23. $newLink=$xml->addChild('friendLink');
  24. $newLink->addChild('name',iconv('gbk','utf-8','众里寻他千百度'));
  25. $newLink->addChild('url','http://www.baidu.com');
  26. $newLink->addChild('info','Baidudu');
  27. $newLink->addChild('logoPath','d:/image.jpg');
  28. $newLink->addChild('qqMsn','MSN:[email protected]');
  29. $newLink->addAttribute('id','3');
  30. $newLink->addAttribute('orderId','3');
  31. */
  32. /*
  33. *4.修改节点以及属性的值,同样中文要注意转码
  34. $xml->friendLink[2]->name=iconv('gbk','utf-8','新浪网');//设置节点3name的值为新浪网。注意索引从0开始
  35. $xml->friendLink[2]['id']=3;//节点3的friendLink元素的属性(id=3)。
  36. */
  37. /*
  38. *5.遍历xml文档
  39. *5.1遍历所有一级节点的属性值以及内容
  40. foreach($xml->friendLinkas$link){
  41. echo'<hr/>';
  42. echo$link['id'],'<br/>';
  43. echo$link['orderId'],'<br/>';
  44. foreach($linkas$key=>$value){
  45. echo$key,':',iconv('utf-8','gbk',$value),'<br/>';
  46. }
  47. }
  48. */
  49. /*5.2遍历XML文档中某一个节点的值,比如打印出所有元素的name节点的值
  50. foreach($xml->xpath('//info')as$value){
  51. echominIconv($value),'<br/>';
  52. }
  53. */
  54. /*5.3遍历出某个元素的所有子节点值
  55. foreach($xml->friendLink[1]->children()as$field){
  56. echominIconv($field),'<br/>';
  57. }
  58. foreach($xml->children()as$var){
  59. //echo$var['id'],'<br/>';
  60. //echominIconv($var->name),'<br/>';
  61. echo$var->getName(),'<br/>';//获得当前节点/元素的名称
  62. }
  63. */
  64. /*5.4遍历XML文档中某一个节点的属性,比如打印出第二个元素的所有属性值
  65. foreach($xml->friendLink[1]->attributes()as$key=>$value){
  66. echo$key,'=',$value,'<br/>';
  67. }
  68. */
  69. //6.保存内容到xml文档
  70. //$xml->asXML('FL_data/data.xml');
  71. /*
  72. $namespace=$xml->getDocNamespaces();
  73. var_dump($namespace);
  74. */
  75. ?>
  76. </body>
  77. </html>

注意:

data.xml文件是GBK编码

index.php文件是GBK编码

但是simpleXML解析出来的是UTF-8编码

所以就使用到iconv()函数

用到一个简写的转换函数minIconv()

function minIconv($str, $in='utf-8', $out='gbk'){
$str = iconv($in, $out, $str);
return $str;
}


-----------------------------------------------------------

$data = array();
$data['node_type'] = 3;
$data['node_name'] = (string)$targetNode->node_name[0];
$data['node_id'] = (string)$targetNode->target_node->node_id[0];
echo json_encode(array('success' => true,'data' => $data));



--------------------------------------------------------------------------------------------------------------------------------------------------


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">&lt;?</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">?&gt;</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">&lt;</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">&gt;</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">&lt;</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">&gt;</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">&lt;</span><span style="color:green">bookinfo</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">title</span><span style="color:olive">&gt;</span><span style="color:gray">SimpleXML in PHP 5</span><span style="color:olive">&lt;/</span><span style="color:green">title</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">author</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">firstname</span><span style="color:olive">&gt;</span><span style="color:gray">Rob</span><span style="color:olive">&lt;/</span><span style="color:green">firstname</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">surname</span><span style="color:olive">&gt;</span><span style="color:gray">Richards</span><span style="color:olive">&lt;/</span><span style="color:green">surname</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">author</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">copyright</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">year</span><span style="color:olive">&gt;</span><span style="color:gray">2005</span><span style="color:olive">&lt;/</span><span style="color:green">year</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">holder</span><span style="color:olive">&gt;</span><span style="color:gray">Rob Richards</span><span style="color:olive">&lt;/</span><span style="color:green">holder</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">copyright</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">bookinfo</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">preface</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">title</span><span style="color:olive">&gt;</span><span style="color:gray">Using SimpleXML</span><span style="color:olive">&lt;/</span><span style="color:green">title</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">para</span><span style="color:olive">&gt;</span><span style="color:gray">An example DOM Tree using DocBook.</span><span style="color:olive">&lt;/</span><span style="color:green">para</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">preface</span><span style="color:olive">&gt;</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">&lt;</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">&gt;</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">&lt;</span><span style="color:green">title</span><span style="color:olive">&gt;</span><span style="color:gray">Accessing Elements</span><span style="color:olive">&lt;/</span><span style="color:green">title</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">para</span><span style="color:olive">&gt;</span><span style="color:gray">Elements are accessed as properties</span><span style="color:olive">&lt;/</span><span style="color:green">para</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">para</span><span style="color:olive">&gt;</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)">&lt;![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)"> &lt;?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 = '&lt;?xml version="1.0"?&gt;</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)"> &lt;root&gt;content&lt;/root&gt;';</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)"> ?&gt;</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)"> ]]&gt;</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">&lt;/</span><span style="color:green">para</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">chapter</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">book</span><span style="color:olive">&gt;</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">&lt;root&gt;&lt;node1&gt;Content&lt;/node1&gt;&lt;/root&gt;</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] =&gt; 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">&lt;root&gt;&lt;node1&gt;Content&lt;/node1&gt;&lt;/root&gt;</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] =&gt; 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">&lt;root&gt;&lt;node1&gt;content&lt;/node1&gt;&lt;/root&gt;</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">-&gt;</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">-&gt;</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">&lt;?</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">?&gt;</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">&lt;</span><span style="color:green">root</span><span style="color:olive">&gt;&lt;</span><span style="color:green">node1</span><span style="color:olive">&gt;</span><span style="color:gray">content</span><span style="color:olive">&lt;/</span><span style="color:green">node1</span><span style="color:olive">&gt;&lt;/</span><span style="color:green">root</span><span style="color:olive">&gt;</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">&lt;?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">-&gt;</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">-&gt;</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">?&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">&lt;?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">-&gt;</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">-&gt;</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">-&gt;</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">?&gt;</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]=&gt;<br> string(18) “SimpleXML in PHP 5″<br> }<br> Title: SimpleXML in PHP 5<br> object(SimpleXMLElement)#6 (2) {<br> [”firstname”]=&gt;<br> string(3) “Rob”<br> [”surname”]=&gt;<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">&lt;root&gt;some&lt;child1&gt;subtext&lt;/child1&gt;thing&lt;/root&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">bookinfo</span><span style="color:gray">-&gt;</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">-&gt;</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">=&gt;</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">-&gt;</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”]=&gt;<br> string(18) “SimpleXML in PHP 5″<br> [”author”]=&gt;<br> object(SimpleXMLElement)#4 (2) {<br> [”firstname”]=&gt;<br> string(3) “Rob”<br> [”surname”]=&gt;<br> string(8) “Richards”<br> }<br> [”copyright”]=&gt;<br> object(SimpleXMLElement)#5 (2) {<br> [”year”]=&gt;<br> string(4) “2005″<br> [”holder”]=&gt;<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">-&gt;</span><span style="color:blue">bookinfo</span><span style="color:gray">-&gt;</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">-&gt;</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">-&gt;</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">&lt;root&gt;&lt;node1&gt;content&lt;/node1&gt;&lt;/root&gt;</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">-&gt;</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">-&gt;</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">&lt;?</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">?&gt;</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">&lt;</span><span style="color:green">root</span><span style="color:olive">&gt;&lt;</span><span style="color:green">node1</span><span style="color:olive">&gt;</span><span style="color:gray">content</span><span style="color:olive">&lt;/</span><span style="color:green">node1</span><span style="color:olive">&gt;&lt;</span><span style="color:green">node2</span><span style="color:olive">&gt;</span><span style="color:gray">content2</span><span style="color:olive">&lt;/</span><span style="color:green">node2</span><span style="color:olive">&gt;&lt;/</span><span style="color:green">root</span><span style="color:olive">&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">&lt;</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">&gt;</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">&lt;</span><span style="color:green">title</span><span style="color:olive">&gt;</span><span style="color:gray">Acessing Elements</span><span style="color:olive">&lt;/</span><span style="color:green">title</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">para</span><span style="color:olive">&gt;</span><span style="color:gray">Elements are accessed as properties</span><span style="color:olive">&lt;/</span><span style="color:green">para</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">para</span><span style="color:olive">&gt;</span><span style="color:gray">Removed CDATA</span><span style="color:olive">&lt;/</span><span style="color:green">para</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">chapter</span><span style="color:olive">&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">&lt;</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">&gt;</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">&lt;</span><span style="color:green">title</span><span style="color:olive">&gt;</span><span style="color:gray">New Title</span><span style="color:olive">&lt;/</span><span style="color:green">title</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">para</span><span style="color:olive">&gt;</span><span style="color:gray">Elements are accessed as properties</span><span style="color:olive">&lt;/</span><span style="color:green">para</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">para</span><span style="color:olive">&gt;</span><span style="color:gray">Removed CDATA</span><span style="color:olive">&lt;/</span><span style="color:green">para</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">chapter</span><span style="color:olive">&gt;</span> </li> </ol> </div> <p style="line-height:25px; margin-top:1em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px"> 强烈建议使用索引来编辑元素,除非你对文档的结果非常确定。使用索引来编辑title元素会比较安全,如$book-&gt;chapter-&gt;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">-&gt;</span><span style="color:blue">bookinfo</span><span style="color:gray">-&gt;</span><span style="color:blue">copyright</span><span style="color:gray">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</span><span style="color:blue">bookinfo</span><span style="color:gray">-&gt;</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">-&gt;</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">&lt;</span><span style="color:green">holder</span><span style="color:olive">&gt;</span><span style="color:gray">Rob Richards</span><span style="color:olive">&lt;/</span><span style="color:green">holder</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">bookinfo</span><span style="color:olive">&gt;</span><span style="color:gray">No Book Info</span><span style="color:olive">&lt;/</span><span style="color:green">bookinfo</span><span style="color:olive">&gt;</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">-&gt;</span><span style="color:blue">bookinfo</span><span style="color:gray">=</span><span style="color:rgb(139,0,0)">"</span><span style="color:red">&lt;title&gt;SimpleXML in PHP 5&lt;/title&gt;</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">-&gt;</span><span style="color:blue">bookinfo</span><span style="color:gray">-&gt;</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> &lt;title&gt;SimpleXML in PHP 5&lt;/title&gt;<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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</span><span style="color:blue">bookinfo</span><span style="color:gray">-&gt;</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">&lt;</span><span style="color:green">bookinfo</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">title</span><span style="color:olive">&gt;</span><span style="color:gray">SimpleXML in PHP 5</span><span style="color:olive">&lt;/</span><span style="color:green">title</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">bookinfo</span><span style="color:olive">&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">&lt;</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">&gt;</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">&lt;</span><span style="color:green">para</span><span style="color:olive">&gt;</span><span style="color:gray">Elements are accessed as properties</span><span style="color:olive">&lt;/</span><span style="color:green">para</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">para</span><span style="color:olive">&gt;</span><span style="color:gray">Removed CDATA</span><span style="color:olive">&lt;/</span><span style="color:green">para</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">chapter</span><span style="color:olive">&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">&lt;</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">&gt;</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">&lt;</span><span style="color:green">title</span><span style="color:olive">&gt;</span><span style="color:gray">Acessing Elements</span><span style="color:olive">&lt;/</span><span style="color:green">title</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">para</span><span style="color:olive">&gt;</span><span style="color:gray">Elements are accessed as properties</span><span style="color:olive">&lt;/</span><span style="color:green">para</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">para</span><span style="color:olive">&gt;</span><span style="color:gray">Removed CDATA</span><span style="color:olive">&lt;/</span><span style="color:green">para</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">chapter</span><span style="color:olive">&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">&lt;</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">&gt;</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">&lt;</span><span style="color:green">title</span><span style="color:olive">&gt;</span><span style="color:gray">Acessing Elements</span><span style="color:olive">&lt;/</span><span style="color:green">title</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">para</span><span style="color:olive">&gt;</span><span style="color:gray">Elements are accessed as properties</span><span style="color:olive">&lt;/</span><span style="color:green">para</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">para</span><span style="color:olive">&gt;</span><span style="color:gray">Removed CDATA</span><span style="color:olive">&lt;/</span><span style="color:green">para</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">chapter</span><span style="color:olive">&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">&lt;</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">&gt;</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">&lt;</span><span style="color:green">title</span><span style="color:olive">&gt;</span><span style="color:gray">Acessing Elements</span><span style="color:olive">&lt;/</span><span style="color:green">title</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">chapter</span><span style="color:olive">&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">&lt;</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">&gt;</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">&lt;</span><span style="color:green">title</span><span style="color:olive">&gt;</span><span style="color:gray">Acessing Elements</span><span style="color:olive">&lt;/</span><span style="color:green">title</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">para</span><span style="color:olive">&gt;</span><span style="color:gray">Elements are accessed as properties</span><span style="color:olive">&lt;/</span><span style="color:green">para</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">chapter</span><span style="color:olive">&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">-&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">chapter</span><span style="color:gray">-&gt;</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">-&gt;</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">-&gt;</span><span style="color:blue">bookinfo</span><span style="color:gray">-&gt;</span><span style="color:blue">author</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">bookinfo</span><span style="color:gray">-&gt;</span><span style="color:blue">author</span><span style="color:gray">-&gt;</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">&lt;</span><span style="color:green">author</span><span style="color:olive">&gt;</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">&lt;</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">&gt;</span><span style="color:gray">Rob</span><span style="color:olive">&lt;/</span><span style="color:green">firstname</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">surname</span><span style="color:olive">&gt;</span><span style="color:gray">Richards</span><span style="color:olive">&lt;/</span><span style="color:green">surname</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">author</span><span style="color:olive">&gt;</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">-&gt;</span><span style="color:blue">bookinfo</span><span style="color:gray">-&gt;</span><span style="color:blue">author</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">bookinfo</span><span style="color:gray">-&gt;</span><span style="color:blue">author</span><span style="color:gray">-&gt;</span><span style="color:blue">firstname</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">bookinfo</span><span style="color:gray">-&gt;</span><span style="color:blue">author</span><span style="color:gray">-&gt;</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">-&gt;</span><span style="color:blue">bookinfo</span><span style="color:gray">-&gt;</span><span style="color:blue">author</span><span style="color:gray">-&gt;</span><span style="color:blue">firstname</span><span style="color:gray">-&gt;</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">&lt;</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">&gt;</span><span style="color:gray">Rob</span><span style="color:olive">&lt;/</span><span style="color:green">firstname</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">firstname</span><span style="color:olive">&gt;</span><span style="color:gray">Rob</span><span style="color:olive">&lt;/</span><span style="color:green">firstname</span><span style="color:olive">&gt;</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">-&gt;</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">-&gt;</span><span style="color:blue">ownerDocument</span><span style="color:gray">-&gt;</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">&lt;root&gt;&lt;node1&gt;&lt;/node1&gt;&lt;/root&gt;</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">-&gt;</span><span style="color:blue">node1</span><span style="color:gray">-&gt;</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">-&gt;</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">&lt;?</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">?&gt;</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">&lt;</span><span style="color:green">root</span><span style="color:olive">&gt;&lt;</span><span style="color:green">node1</span><span style="color:olive">&gt;&lt;</span><span style="color:green">node2</span><span style="color:olive">&gt;</span><span style="color:gray">content</span><span style="color:olive">&lt;/</span><span style="color:green">node2</span><span style="color:olive">&gt;&lt;/</span><span style="color:green">node1</span><span style="color:olive">&gt;&lt;/</span><span style="color:green">root</span><span style="color:olive">&gt;</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">&lt;root&gt;&lt;node1&gt;&lt;/node1&gt;&lt;/root&gt;</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">-&gt;</span><span style="color:blue">node1</span><span style="color:gray">-&gt;</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">-&gt;</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">&lt;?</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">?&gt;</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">&lt;</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">&gt;</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">&lt;</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">&gt;</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">&lt;</span><span style="color:green">title</span><span style="color:olive">&gt;</span><span style="color:gray">SimpleXML in PHP 5</span><span style="color:olive">&lt;/</span><span style="color:green">title</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">author</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">firstname</span><span style="color:olive">&gt;</span><span style="color:gray">Rob</span><span style="color:olive">&lt;/</span><span style="color:green">firstname</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">surname</span><span style="color:olive">&gt;</span><span style="color:gray">Richards</span><span style="color:olive">&lt;/</span><span style="color:green">surname</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">author</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">copyright</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">year</span><span style="color:olive">&gt;</span><span style="color:gray">2005</span><span style="color:olive">&lt;/</span><span style="color:green">year</span><span style="color:olive">&gt;</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">&lt;</span><span style="color:green">holder</span><span style="color:olive">&gt;</span><span style="color:gray">Rob Richards</span><span style="color:olive">&lt;/</span><span style="color:green">holder</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">copyright</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">bookinfo</span><span style="color:olive">&gt;</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">&lt;/</span><span style="color:green">book</span><span style="color:olive">&gt;</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">-&gt;</span><span style="color:blue">bookinfo</span><span style="color:gray">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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-&gt;author-&gt;firstname来但因author中的firstname元素。所有的元素都在命名空间下,因此 你在创建$bookinfo对象时不必一直使用children()设置命名空间。</p> </div> </wbr>

你可能感兴趣的:(simple)