|
<br><TABLE cellSpacing=0 cellPadding=0 width="85%"><TBODY><TR><TD style="BORDER-RIGHT: rgb(0,0,0) 1px groove; BORDER-TOP: rgb(0,0,0) 1px groove; BORDER-LEFT: rgb(0,0,0) 1px groove; BORDER-BOTTOM: rgb(0,0,0) 1px groove" bgColor=#eeeeee><?php <BR>/** <BR>* Topic: Create and parse XML files using PHP DOM-XML <BR>* Source: http://www.php.net/domxml <BR>* Reference: http://www.zugeschaut-und-mitgebaut.de/php/extension.domxml.html <BR>* Author: urs@circle.ch, 16-1-2001 <BR>* <BR>*/ <BR>// 使用PHP DOM-XML创建和解析XML文件 <BR><BR>//创建XML文档对象;以后的处理过程将在此基础上进行 <BR>$doc = new_xmldoc("1.0" ); <BR><BR>//创建根节点,并设置一个属性 <BR>$root = $doc->add_root("faq" ); <BR>$root->setattr("page", "32" ); <BR><BR>//子节点 <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 http://www.xmlsoft.org. <BR>The current version is libxml2-2.2.1." ); <BR><BR>$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 <BR>to /usr), I needed to use --with-dom=/usr/local" ); <BR><BR>$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." ); <BR><BR>//输出到Browser <BR>print("<pre>".htmlspecialchars($doc->dumpmem() )."</pre>" ); <BR><BR>// write to file <BR>//写回到文件 <BR>$fp = fopen("test_dom.xml", "w+" ); <BR>fwrite($fp, $doc->dumpmem(), strlen($doc->dumpmem() )); <BR>fclose($fp); <BR><BR>// ------------------------------------------------------ <BR>//现在使用xpath从XML文档中得到内容 <BR><BR>$doc = xmldoc(join("", file("test_dom.xml")) ); <BR>$ctx = xpath_new_context($doc ); <BR><BR>//所有对象 <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/>"); <BR><BR>//第三个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); <BR><BR>?> </TD></TR></TBODY></TABLE><BR><BR>需要说明,PHP DOM 只能在PHP PHP4.0.x + linux上运行 <BR><BR> HPDOM类库请到http://www.zend.com/download下载 <br><br> |
|