Friday, 1 August 2014

How to load xml file saved generated by DOMDocument?



I am generating an xml file using DOMDocument of php.


php code for generating xml file: 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://ift.tt/1scag5L');
$utext = $url->appendChild($utext);

echo $doc->saveXML('links1.xml');
?>


In the above code {192.168.44.128}= 192.168.44.128


php code for loading xml file: 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 and livesearch.php are in the same folder called 'www'. '$xmlDoc->load("links1.xml");' this statement loads the xml file. Using this code am getting the following error:



DOMDocument::load(): I/O warning : failed to load external entity "/usr/local/www/links1.xml"


I haven't created 'links1.xml' file explicitly, or should I? So how to load the xml file, generated by 'links.php', in 'livesearch.php?


No comments:

Post a Comment