background image

使用

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

<?PHP
/**
*Topic:CreateandparseXMLfilesusingPHPDOM-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
*
*/
//使用 PHPDOM-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.Wheretogetlibxml-2.0.0?");
$one->new_child("answer","Youcandownloadthelatest
releaseoflibxmleitherasasourcearchiveor
RPMpackagefromhttp://www.xmlsoft.org.
Thecurrentversionislibxml2-2.2.1.");

$two=$root->new_child("question","");
$two->setattr("number","2");
$two->new_child("text","2.HowtoconfigurePHP4?");
//创建一个不直接赋值的节点
$twoone=$two->new_child("answer","");
//然后给它单独赋值
$twoone->set_content("DIRisthelibxmlinstalldirectory
(ifyoujustuse--with-domitdefaults
to/usr),Ineededtouse--with-dom=/usr/local");