haidong 发表于 2010-2-24 13:35:00

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

<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>&lt;?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-&gt;add_root("faq" );<BR>$root-&gt;setattr("page", "32" );<BR><BR>//子节点 <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 http://www.xmlsoft.org.<BR>The current version is libxml2-2.2.1." );<BR><BR>$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<BR>to /usr), I needed to use --with-dom=/usr/local" );<BR><BR>$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." );<BR><BR>//输出到Browser<BR>print("&lt;pre&gt;".htmlspecialchars($doc-&gt;dumpmem() )."&lt;/pre&gt;" );<BR><BR>// write to file <BR>//写回到文件<BR>$fp = fopen("test_dom.xml", "w+" );<BR>fwrite($fp, $doc-&gt;dumpmem(), strlen($doc-&gt;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("&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");<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>// 第三个answer对象 <BR>$foo = xpath_eval($ctx, "//answer");<BR>print_r($foo);<BR>print("&lt;br/&gt;&lt;br/&gt;");<BR><BR>//第三个text node的类型,名称和内容<BR>$foo = xpath_eval($ctx, "//text");<BR>$tmp = $foo-&gt;nodeset;<BR>print_r($tmp);<BR>print("&lt;br/&gt;");<BR>print($tmp-&gt;type) . "; ";<BR>print($tmp-&gt;name) . "; ";<BR>print($tmp-&gt;content);<BR><BR>?&gt; </TD></TR></TBODY></TABLE><BR><BR>需要说明,PHP DOM 只能在PHP PHP4.0.x + linux上运行 <BR><BR>PHPDOM类库请到http://www.zend.com/download下载                   <br><br>
页: [1]
查看完整版本: 使用PHP DOM-XML创建和解析XML文件