作者:多米音乐_34216717 | 来源:互联网 | 2014-05-04 00:43
应用PHPDOM-XML创立和解析XML文件?php/***Topic:CreateandparseXMLfilesusingPHPDOM-XML*Source:http://www.php.net/domxml*Reference:http://www.zugeschaut-un
应用PHP DOM-XML创立和解析XML文件
/**
* 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 *** example." );
//输出到Browser
print("".htmlspecialchars($doc->dumpmem() )."
" );
// 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 );
共2页: 上一页 1 [2] 下一页