我正在使用php的DOMDocument生成一个xml文件。
用于生成xml文件的php代码: links.php
<?php
$doc = new DOMDocument('1.0','utf-8');
// we want a nice output
$doc->formatOutput = true;
$pages = $doc->createElement('pages');
$root = $doc->appendChild($pages);
$link = $doc->createElement('link');
$link = $root->appendChild($link);
$title = $doc->createElement('title');
$title = $link->appendChild($title);
$text = $doc->createTextNode('Advanced');
$text = $title->appendChild($text);
$url = $doc->createElement('url');
$url = $link->appendChild($url);
$utext = $doc->createTextNode('http://{192.168.44.128}/system.php');
$utext = $url->appendChild($utext);
echo $doc->saveXML('links1.xml');
?>
在上面的代码{192.168.44.128} = 192.168.44.128
中用于加载xml文件的php代码: livesearch.php
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links1.xml");
$x=$xmlDoc->getElementsByTagName('link');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
$hint="";
for($i=0; $i<($x->length); $i++) {
$y=$x->item($i)->getElementsByTagName('title');
$z=$x->item($i)->getElementsByTagName('url');
if ($y->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
} else {
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
}
}
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint=="") {
$response="no suggestion";
} else {
$response=$hint;
}
//output the response
echo $response;
?>
links.php和livesearch.php位于名为“www”的同一文件夹中。 '$ xmlDoc-&GT;负载( “links1.xml”);'此语句加载xml文件。使用此代码会出现以下错误:
DOMDocument::load(): I/O warning : failed to load external entity "/usr/local/www/links1.xml"
我没有明确创建'links1.xml'文件,或者我应该?那么如何在'livesearch.php中加载'links.php'生成的xml文件?
答案 0 :(得分:0)
$doc->saveXML();
无法保存文件,$doc->save('link1.xml')
会。我不希望直接工作,权限可能会阻止在文档根目录中写入文件。