谁知道如何用PHP操作XML?
好像可以用DOM操作,有没有相关的例子呀? 问题点数:100、回复次数:7Top
1 楼DFlyingchen(弱水三千)回复于 2003-09-03 22:29:47 得分 100
<?php
/**
* Topic: Create and parse XML files using PHP DOM-XML
* Source: http://www.php.net/domxml
* Reference: http://www.zugeschaut-und-mitgebaut.de/php/extension.domxml.html
* Author: urs@circle.ch, 16-1-2001
*
*/
// 使用PHP DOM-XML创建和解析XML文件
//创建XML文档对象;以后的处理过程将在此基础上进行
$doc = new_xmldoc("1.0" );
//创建根节点,并设置一个属性
$root = $doc->add_root("faq" );
$root->setattr("page", "32" );
//子节点
$one = $root->new_child("question", "");
//为子节点设置属性
$one->setattr("number", "1");
//question也创建子节点,并且给它赋值
$one->new_child("text", "1. Where to get libxml-2.0.0?");
$one->new_child("answer", "You can download the latest
release of libxml either as a source archive or
RPM package from http://www.xmlsoft.org.
The current version is libxml2-2.2.1." );
$two = $root->new_child("question", "" );
$two->setattr("number", "2");
$two->new_child("text", "2. How to configure PHP4?" );
// 创建一个不直接赋值的节点
$twoone = $two->new_child("answer", "");
// 然后给它单独赋值
$twoone->set_content("DIR is the libxml install directory
(if you just use --with-dom it defaults
to /usr), I needed to use --with-dom=/usr/local" );
$three = $root->new_child("question", "" );
$three->setattr("number", "7" );
$three->new_child("text", "7. How to use DOM XML function ?" );
$three->new_child("answer", "Read this document source for
a simple example." );
//输出到Browser
print("<pre>".htmlspecialchars($doc->dumpmem() )."</pre>" );
// write to file
//写回到文件
$fp = fopen("test_dom.xml", "w+" );
fwrite($fp, $doc->dumpmem(), strlen($doc->dumpmem() ));
fclose($fp);
// ------------------------------------------------------
//现在使用xpath从XML文档中得到内容
$doc = xmldoc(join("", file("test_dom.xml")) );
$ctx = xpath_new_context($doc );
//所有对象
$foo = xpath_eval($ctx, "//child::*");
print_r($foo);
print("<br/><br/>");
//text node 对象
$foo = xpath_eval($ctx, "//text");
print_r($foo);
print("<br/><br/>");
// 第一个text node对象
$foo = xpath_eval($ctx, "//text[1]");
print_r($foo);
print("<br/><br/>");
// 第二个text node对象
$foo = xpath_eval($ctx, "//text[2]");
print_r($foo);
print("<br/><br/>");
// 第三个answer对象
$foo = xpath_eval($ctx, "//answer[3]");
print_r($foo);
print("<br/><br/>");
//第三个text node的类型,名称和内容
$foo = xpath_eval($ctx, "//text[3]");
$tmp = $foo->nodeset;
print_r($tmp);
print("<br/>");
print($tmp[0]->type) . "; ";
print($tmp[0]->name) . "; ";
print($tmp[0]->content);
?>Top
2 楼DFlyingchen(弱水三千)回复于 2003-09-03 22:38:24 得分 0
这个文章有点老了,你可以到Google搜索一下最新的文章Top
3 楼deng2001(基督山伯爵)回复于 2003-09-03 22:40:13 得分 0
我知道Top
4 楼xuzuning(唠叨)回复于 2003-09-04 08:37:00 得分 0
http://expert.csdn.net/Expert/TopicView1.asp?id=1597294Top
5 楼windy3000(windy)回复于 2003-09-04 10:05:05 得分 0
我写的用 sax 处理 xml 的 类,欢迎指教~~~~~~
http://www.mbavoice.net/windy/xml/
xml 源文件:
http://www.mbavoice.net/windy/xml/template/classic.xml
测试用页面,请自觉不要删除根节点!要删,可以先建立新的节点!!!!Top
6 楼myblessu(继续混着,到被人赶走为止)回复于 2003-09-04 10:44:47 得分 0
这里将的也很清楚了: http://cn.php.net/xmlTop
7 楼kedazhang(淡蓝忧伤)回复于 2003-10-13 16:09:28 得分 0
upTop



