<br> <font color="#b8860b">/** <br><br> * Topic: Create and parse XML files using PHP DOM-XML <br><br> * Source: http://www.php.net/domxml <br><br> * Reference: http://www.zugeschaut-und-mitgebaut.de/php/extension.domxml.html <br><br> * Author: urs@circle.ch, 16-1-2001 <br><br> * <br><br> */</font><font color="#0000cc"> <br><br></font><font color="#b8860b">// 使用PHP DOM-XML创建和解析XML文件<br><br><br><br> //创建XML文档对象;以后的处理过程将在此基础上进行<br><br> $doc = new_xmldoc("1.0" ); <br><br><br><br> //创建根节点,并设置一个属性<br><br> $root = $doc->add_root("faq" ); <br><br> $root->setattr("page", "32" ); <br><br><br><br> //子节点<br><br> $one = $root->new_child("question", ""); <br><br> //为子节点设置属性<br><br> $one->setattr("number", "1"); <br><br> //question也创建子节点,并且给它赋值 <br><br> $one->new_child("text", "1. Where to get libxml-2.0.0?"); <br><br> $one->new_child("answer", "You can download the latest <br><br> release of libxml either as a source archive or <br><br> RPM package from http://www.xmlsoft.org. <br><br> The current version is libxml2-2.2.1." ); <br><br><br><br> $two = $root->new_child("question", "" ); <br><br> $two->setattr("number", "2"); <br><br> $two->new_child("text", "2. How to configure PHP4?" ); <br><br> // 创建一个不直接赋值的节点<br><br> $twoone = $two->new_child("answer", ""); <br><br> // 然后给它单独赋值<br><br> $twoone->set_content("DIR is the libxml install directory <br><br> (if you just use --with-dom it defaults <br><br> to /usr), I needed to use --with-dom=/usr/local" ); <br><br><br><br> $three = $root->new_child("question", "" ); <br><br> $three->setattr("number", "7" ); <br><br> $three->new_child("text", "7. How to use DOM XML function ?" ); <br><br> $three->new_child("answer", "Read this document source for <br><br> a simple example." ); <br><br><br><br> //输出到Browser <br><br> print("<pre>".htmlspecialchars($doc->dumpmem() )."</pre>" ); <br><br><br><br> // write to file<br><br> //写回到文件 <br><br> $fp = fopen("test_dom.xml", "w+" ); <br><br> fwrite($fp, $doc->dumpmem(), strlen($doc->dumpmem() )); <br><br> fclose($fp); <br><br><br><br> // ------------------------------------------------------ <br><br> //现在使用xpath从XML文档中得到内容<br><br> <br><br> $doc = xmldoc(join("", file("test_dom.xml")) ); <br><br> $ctx = xpath_new_context($doc ); <br><br><br><br> //所有对象<br><br> $foo = xpath_eval($ctx, "//child::*"); <br><br> print_r($foo); <br><br> print("<br><br>"); <br><br> //text node 对象<br><br> $foo = xpath_eval($ctx, "//text"); <br><br> print_r($foo); <br><br> print("<br><br>"); <br><br> // 第一个text node对象<br><br> $foo = xpath_eval($ctx, "//text[1]"); <br><br> print_r($foo); <br><br> print("<br><br>"); <br><br> // 第二个text node对象<br><br> $foo = xpath_eval($ctx, "//text[2]"); <br><br> print_r($foo); <br><br> print("<br><br>"); <br><br> // 第三个answer对象<br><br> $foo = xpath_eval($ctx, "//answer[3]"); <br><br> print_r($foo); <br><br> print("<br><br>"); <br><br><br><br> //第三个text node的类型,名称和内容 <br><br> $foo = xpath_eval($ctx, "//text[3]"); <br><br> $tmp = $foo->nodeset; <br><br> print_r($tmp); <br><br> print("<br>"); <br><br> print($tmp[0]->type) . "; "; <br><br> print($tmp[0]->name) . "; "; <br><br> print($tmp[0]->content); <br><br><br><br></font><font color="#0000cc">?> <br><br><br><br>需要说明</font><font color="#006600">,</font><font color="#0000cc">PHP DOM 只能在PHP PHP4</font><font color="#006600">.</font><font color="#0000cc">0</font><font color="#006600">.</font><font color="#0000cc">x </font><font color="#006600">+</font><font color="#0000cc"> linux上运行<br><br><br><br>PHPDOM类库请到http</font><font color="#006600">:</font><font color="#006600">/</font><font color="#006600">/</font><font color="#0000cc">www</font><font color="#006600">.</font><font color="#0000cc">zend</font><font color="#006600">.</font><font color="#0000cc">com</font><font color="#006600">/</font><font color="#0000cc">download</font>