background image

$three=$root->new_child("question","");
$three->setattr("number","7");
$three->new_child("text","7.HowtouseDOMXMLfunction?");
$three->new_child("answer","Readthisdocumentsourcefor
asimpleexample.");

//输出到 Browser
print("<pre>".htmlspecialchars($doc->dumpmem())."</pre>");

//writetofile
//写回到文件
$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/>");
//textnode 对象
$foo=xpath_eval($ctx,"//text");
print_r($foo);
print("<br/><br/>");
//第一个 textnode 对象
$foo=xpath_eval($ctx,"//text[1]");
print_r($foo);
print("<br/><br/>");
//第二个 textnode 对象
$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/>");

//第三个 textnode 的类型,名称和内容