Reading xml using php help required

Hi guys, this is my first post. Im a noob so please go easy. Basically i am trying to perform a search of authors in a xml file using a php file. I can’t seem to get it working right, but i don’t know what the error is. It displays as the following when doing a search of the author “Ritchie”. Any help would be greatly appreciated :slight_smile:

My New Book

Notice: Trying to get property of non-object in N:\ftp\compc\d09iw\Com506\Week4\authorSearch.php on line 25

The code of the xml file is:

[code]<?xml version="1.0"?>

106 Cut the grass Mr T Ritchie 16/10/2012 When you finish your work will you cut the grass please? Insert web address Get milk Mum Ritchie 16/10/2012 When you finish your work will you buy milk please? Costcutters Wash Car Dad Ritchie 16/10/2012 Wash the car Insert web address Get haircut Nichola Ritchie 16/10/2012 Please get a hair cut!! SuperCuts Complete Uni Work Dave Ritchie 16/10/2012 Get your uni work done Ulster University [/code] [u][b]The code of the php file is:[/b][/u] [php] <?php

if (isset($_POST[“submit”])) {

$file = “memo.xml”;
$fp = fopen($file, “rb”) or die(“cannot open file”);
$str = fread($fp, filesize($file));

$xml = new DOMDocument();
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->loadXML($str) or die(“Error”);

// get document element
$root = $xml->documentElement;
$memos = $root->childNodes->item(5);

$memosFound=0;
foreach($memos->childNodes as $memo) {
$memoTitleNode=$memo->childNodes->item(0);
$memoSenderNode=$memo->childNodes->item(1);
$memoRecipientNode=$memo->childNodes->item(2);
$memoDateNode=$memo->childNodes->item(3);
$memoMessageNode=$memo->childNodes->item(4);
$memoUrlNode=$memo->childNodes->item(5);
if ($memoSenderNode->nodeValue==$_POST[“sender”]) {
echo $memoTitleNode->nodeValue."
";
$memoSenderNode->nodeValue."
";

		   $memoRecipientNode->nodeValue."<br>";
		   $memoDateNode->nodeValue."<br>";
		   $memoMessageNode->nodeValue."<br>";
		   $memoUrlNode->nodeValue."<br>";
		   $memosFound++;
  }    

}
if ($memosFound==0) echo “No matching authors found”;

} else {

?>

XML Data Search

Author Search

Name: <?php } ?>[/php]

First please use the code blocks in the future second see this post which does some XML work and should answer your question. http://www.phphelp.com/forum/index.php?topic=17339.0

I think you could greatly simplify this code using simplexml_load_file or simplexml_load_string functions.

For example

[php]
$xml_data = simplexml_load_file(‘memo.xml’);
foreach($xml_data->database->memos as $memo) {
if ($memo->recipient == ‘Ritchie’) {
$memosFound++;
}
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service