0

A similar question has been posted here by someone else before but unfortunately no one could give a sufficient answer to it: Parse XML (SRU) with php I want to use an XML file, which is generated by SRU, and parse it with PHP to get specific contents of this XML file. I'm totally new to PHP and SRU, so maybe you can help me here with some beginner's problems.

First I want to fetch the XML-file generated by SRU via URL:

<?php
 $datasource='http://pub.uni-bielefeld.de/sru';
 $fh = simplexml_load_file($datasource);
 print_r($fh);
 echo ("<br /> <br />");
 ?>

This gives me an output with a SimpleXMLElement, which is still fine. Additionally I check if the $fh is set and not empty:

<?php
 if (empty($fh)) {
      echo "fh is empty or isn't set.";
 }
 else {
       echo "fh contains a value.\n";
 }


 if (isset($fh)) {
     echo "fh is set.\n";
 }
 else {
 echo "fh is not set.\n";
 }
 ?>

This gives me the expected output that $fh contains a value and that it is set as could be seen by the fact that an output is generated. Afterwards I want to fetch an element of this $fh but it doesn't work this way:

<?php
$var1 = $fh->explainResponse->record->recordSchema;
echo "$var1";
?>

I also checked $var1 with isset and empty like I checke $fh in the above sample and find out, that $var1 isn't set and that it is empty, too. When trying to run the script I get the error message at the point where I try to fill in content into $var1:

Notice: Trying to get property of non-object in /var/www/testsite/sru_parse.php on line        
27

Maybe this is a really simple mistake which ruins my script. I would really appreciate your help, even if you're only pointing out what I might have forgotten.

Oh yes, and about the xml-file-structure:

    <explainResponse xmlns="http://www.loc.gov/zing/srw/">
      <version>1.1</version>
      <record>
         <recordSchema>http://explain.z3950.org/dtd/2.0/</recordSchema>
         <recordPacking>xml</recordPacking>
            <recordData> .....
            ....
          </recordData>
          </record>
         <echoedExplainRequest/>
    </explainResponse>
Community
  • 1
  • 1
JJJ
  • 25
  • 4
  • what part of the XML file are you looking to get? Are you attempting to get the value of recordSchema? – Jim Aug 07 '12 at 12:37

1 Answers1

1

Maybe only

$var1 = $fh->record->recordSchema;

You already have explainResponse in $fh variable.

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107