surfnjunkie Posted May 2, 2009 Share Posted May 2, 2009 Basically what I am trying to do is search an xml files elements. In this example I am searching for a books ISBN. If the ISBN is found I would like it to show the book title. If it is not found in any of the <isbn> element tags then I would like to be able to have it echo a message that the ISBN can not be found only 1 time. Here is the sample xml: <?xml version="1.0" encoding="utf-8"?> <catalog> <booklist> <book> <title>Cat in the hat</title> <image>cat.jpg</image> <isbn>12345</isbn> <link>http://amazon.com/book/cat_in_the_hat/</link> </book> <book> <title>Guerrila Marketing</title> <image>guerrilla.jpg</image> <isbn>54321</isbn> <link>http://amazon.com/book/guerrilla/</link> </book> <book> <title>The 48 Laws of Power</title> <image>power.jpg</image> <isbn>86543</isbn> <link>http://amazon.com/book/48_Laws_of_Power</link> </book> </booklist> </catalog> Here is the code: <?php // Will use simplexml to load xml file $xml = simplexml_load_file('booklist.xml'); // Will search the isbn 12345 $isbnSearch = "54321"; foreach($xml->booklist->book as $book){ if($book->isbn == $isbnSearch) echo "Your book is: " . $book->title . "<br>"; // would like it to stop here if <isbn> and $isbnSearch match // if no <isbn> elements match $isbnSearch if($book->isbn != $isbnSearch) // would like it to echo 1 time and not for each isbn. echo "Your book with ISBN:" . $isbnSearch . " could not be found<br>"; } ?> I have tried a few different things, but I keep hitting a wall. I tried setting up the xml into an array, but I couldn't get that quite right. If anyone can get me pointed in a better direction any help would be greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/156592-xml-query-or-xml-array-help-needed/ Share on other sites More sharing options...
semlabs Posted May 2, 2009 Share Posted May 2, 2009 Hi. You are using the old PHP4 XML functions. Try the newer ones: http://uk.php.net/manual/en/book.dom.php An example: $doc = new DOMDocument; $doc->Load( 'file.xml' ); // Create an XPath object $xpath = new DOMXPath( $doc ); //Query the document $query = '//isbn[text()=12345]'; $entries = $xpath->query( $query ); if( $enteries->length > 0 ) { echo $entries->item( 0 )->nodeValue; } else echo "no ISBN found\n"; This uses XPath. It's a query language for XML. You can get some schooling on it at w3schools Link to comment https://forums.phpfreaks.com/topic/156592-xml-query-or-xml-array-help-needed/#findComment-824554 Share on other sites More sharing options...
surfnjunkie Posted May 5, 2009 Author Share Posted May 5, 2009 I figured it out, and was able to get the script to do what I wanted it to do. I thought I would post my solution. <?php $mySearch = 0; $isbnSearch = "54321"; $xml = simplexml_load_file('booklist.xml'); foreach($xml->booklist->book as $book){ if($book->isbn == $isbnSearch){ $title = $book->title; $mySearch = 1; } } if($mySearch == 1){ echo "We Found your Book" . $title; } else { echo "Sorry we could not find your book" . $isbnSearch; } ?> Link to comment https://forums.phpfreaks.com/topic/156592-xml-query-or-xml-array-help-needed/#findComment-826463 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.