【发布时间】:2014-08-02 06:35:29
【问题描述】:
我正在使用 php 的 DOMDocument 生成一个 xml 文件。
生成xml文件的php代码: 链接.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->load("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文件呢?
【问题讨论】:
-
在您可以打开文件之前,必须明确创建它。那是你的问题吗? (只是要求澄清)
标签: php xml domdocument