searching XML documents using php
Posted
by dbomb101
on Stack Overflow
See other posts from Stack Overflow
or by dbomb101
Published on 2010-03-21T17:13:22Z
Indexed on
2010/03/21
17:21 UTC
Read the original article
Hit count: 395
I am trying to make a search function using the combination of DOM, PHP and XML. I got something up and running but the problem is that my search function will only accept exact terms, on top of this am wondering if the method I picked the most efficient
$searchTerm = "Lupe";
$doc = new DOMDocument();
foreach (file('musicInformation.xml')as $node)
{
$xmlString .= trim($node);
}
$doc->loadXML($xmlString);
$records = $doc->documentElement->childNodes;
$records = $doc->getElementsByTagName("musicdetails");
foreach( $records as $record )
{
$artistnames = $record->getElementsByTagName("artistname");
$artistname = $artistnames->item(0)->nodeValue;
$recordnames = $record->getElementsByTagName("recordname");
$recordname = $recordnames->item(0)->nodeValue;
$recordtypes = $record->getElementsByTagName("recrodtype");
$recordtype = $recordtypes->item(0)->nodeValue;
$formats = $record->getElementsByTagName("format");
$format = $formats->item(0)->nodeValue;
$prices = $record->getElementsByTagName("price");
$price = $prices->item(0)->nodeValue;
if($searchTerm == $artistname|| $searchTerm == $recordname || $searchTerm == $recordtype ||$searchTerm == $format || $searchTerm == $price)
{
echo "$artistname - $recordname - $recordtype - $format -$price\n";
}
© Stack Overflow or respective owner