设为首页收藏本站language→→ 语言切换

鸿鹄论坛

 找回密码
 论坛注册

QQ登录

先注册再绑定QQ

查看: 1917|回复: 0
收起左侧

使用PHP DOM-XML创建和解析XML文件

[复制链接]
发表于 2010-2-24 13:44:08 | 显示全部楼层 |阅读模式
<>&lt;?PHP  <BR>  /** <BR>  * Topic:       Create_and_parse XML files using PHP DOM-XML <BR>  * Source:      <A href="http://www.php.net/domxml">http://www.php.net/domxml</A><BR>  * Reference:   <A href="http://www.zugeschaut-und-mitgebaut.de/php/extension.domxml.html">http://www.zugeschaut-und-mitgebaut.de/php/extension.domxml.html</A><BR>  * Author:      <A href="mailto:urs@circle.ch">urs@circle.ch</A>, 16-1-2001 <BR>  * <BR>  */  <BR>// 使用PHP DOM-XML创建和解析XML文件</P><>  //创建XML文档对象;以后的处理过程将在此基础上进行<BR>  $doc = new_xmldoc("1.0" );  </P><>  //创建根节点,并设置一个属性<BR>  $root = $doc-&gt;add_root("faq" );  <BR>  $root-&gt;setattr("page", "32" );  </P><>  //子节点<BR>  $one = $root-&gt;new_child("question", "");  <BR>  //为子节点设置属性<BR>  $one-&gt;setattr("number", "1");  <BR>  //question也创建子节点,并且给它赋值 <BR>  $one-&gt;new_child("text", "1. Where to get libxml-2.0.0?");  <BR>  $one-&gt;new_child("answer", "You can download the latest <BR>  release of libxml   either as a source archive_or_<BR>  RPM package from <A href="http://www.xmlsoft.org">http://www.xmlsoft.org</A>. <BR>  The current version is libxml2-2.2.1." );  </P><>  $two = $root-&gt;new_child("question", "" );  <BR>  $two-&gt;setattr("number", "2");  <BR>  $two-&gt;new_child("text", "2. How to configure PHP4?" );  <BR>  // 创建一个不直接赋值的节点<BR>  $twoone = $two-&gt;new_child("answer", "");  <BR>  // 然后给它单独赋值<BR>  $twoone-&gt;set_content("DIR is the libxml install directory <BR>  (if you just use --with-dom it defaults to /usr), I needed to use --with-dom=/usr/local" );  </P><>  $three = $root-&gt;new_child("question", "" );  <BR>  $three-&gt;setattr("number", "7" );  <BR>  $three-&gt;new_child("text", "7. How to use DOM XML function ?" );  <BR>  $three-&gt;new_child("answer", "Read this document source for <BR>  a simple example." );  </P><>  //输出到Browser <BR>  print("&lt;pre&gt;".htmlspecialchars($doc-&gt;dumpmem() )."&lt;/pre&gt;" );  </P><>  // write to file<BR>  //写回到文件 <BR>  $fp = fopen("test_dom.xml", "w+" );  <BR>  fwrite($fp, $doc-&gt;dumpmem(), strlen($doc-&gt;dumpmem() ));  <BR>  fclose($fp);  </P><>  // ------------------------------------------------------ <BR>  //现在使用XPath从XML文档中得到内容<BR>  <BR>  $doc = xmldoc(join("", file("test_dom.xml")) );  <BR>  $ctx = xpath_new_context($doc );  </P><>  //所有对象<BR>  $foo = xpath_eval($ctx, "//child::*");  <BR>  print_r($foo);  <BR>  print("&lt;br/&gt;&lt;br/&gt;");  <BR>  //text node 对象<BR>  $foo = xpath_eval($ctx, "//text");  <BR>  print_r($foo);  <BR>  print("&lt;br/&gt;&lt;br/&gt;");  <BR>  // 第一个text node对象<BR>  $foo = xpath_eval($ctx, "//text[1]");  <BR>  print_r($foo);  <BR>  print("&lt;br/&gt;&lt;br/&gt;");  <BR>  // 第二个text node对象<BR>  $foo = xpath_eval($ctx, "//text[2]");  <BR>  print_r($foo);  <BR>  print("&lt;br/&gt;&lt;br/&gt;");  <BR>  // 第三个answer对象<BR>  $foo = xpath_eval($ctx, "//answer[3]");  <BR>  print_r($foo);  <BR>  print("&lt;br/&gt;&lt;br/&gt;");  </P><>  //第三个text node的类型,名称和内容 <BR>  $foo = xpath_eval($ctx, "//text[3]");  <BR>  $tmp = $foo-&gt;nodeset;  <BR>  print_r($tmp); <BR>print("&lt;br/&gt;");  <BR>  print($tmp[0]-&gt;type) . "; ";  <BR>  print($tmp[0]-&gt;name) . "; ";  <BR>  print($tmp[0]-&gt;content);  </P><>?&gt;  </P><>需要说明,PHP DOM 只能在PHP  PHP4.0.x + Linux上运行</P><>HPDOM类库请到<A href="http://www.zend.com/download">www.zend.com/download</A>下载</P>
您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2024-11-23 01:59 , Processed in 0.058464 second(s), 11 queries , Redis On.  

  Powered by Discuz!

  © 2001-2024 HH010.COM

快速回复 返回顶部 返回列表