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