|
<><?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->add_root("faq" ); <BR> $root->setattr("page", "32" ); </P><> //子节点<BR> $one = $root->new_child("question", ""); <BR> //为子节点设置属性<BR> $one->setattr("number", "1"); <BR> //question也创建子节点,并且给它赋值 <BR> $one->new_child("text", "1. Where to get libxml-2.0.0?"); <BR> $one->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->new_child("question", "" ); <BR> $two->setattr("number", "2"); <BR> $two->new_child("text", "2. How to configure PHP4?" ); <BR> // 创建一个不直接赋值的节点<BR> $twoone = $two->new_child("answer", ""); <BR> // 然后给它单独赋值<BR> $twoone->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->new_child("question", "" ); <BR> $three->setattr("number", "7" ); <BR> $three->new_child("text", "7. How to use DOM XML function ?" ); <BR> $three->new_child("answer", "Read this document source for <BR> a simple example." ); </P><> //输出到Browser <BR> print("<pre>".htmlspecialchars($doc->dumpmem() )."</pre>" ); </P><> // write to file<BR> //写回到文件 <BR> $fp = fopen("test_dom.xml", "w+" ); <BR> fwrite($fp, $doc->dumpmem(), strlen($doc->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("<br/><br/>"); <BR> //text node 对象<BR> $foo = xpath_eval($ctx, "//text"); <BR> print_r($foo); <BR> print("<br/><br/>"); <BR> // 第一个text node对象<BR> $foo = xpath_eval($ctx, "//text[1]"); <BR> print_r($foo); <BR> print("<br/><br/>"); <BR> // 第二个text node对象<BR> $foo = xpath_eval($ctx, "//text[2]"); <BR> print_r($foo); <BR> print("<br/><br/>"); <BR> // 第三个answer对象<BR> $foo = xpath_eval($ctx, "//answer[3]"); <BR> print_r($foo); <BR> print("<br/><br/>"); </P><> //第三个text node的类型,名称和内容 <BR> $foo = xpath_eval($ctx, "//text[3]"); <BR> $tmp = $foo->nodeset; <BR> print_r($tmp); <BR>print("<br/>"); <BR> print($tmp[0]->type) . "; "; <BR> print($tmp[0]->name) . "; "; <BR> print($tmp[0]->content); </P><>?> </P><>需要说明,PHP DOM 只能在PHP PHP4.0.x + Linux上运行</P><>HPDOM类库请到<A href="http://www.zend.com/download">www.zend.com/download</A>下载</P> |
|