如何解析从SRU检索的XML文档? [英] How can I parse a XML document retrieved from SRU?

查看:95
本文介绍了如何解析从SRU检索的XML文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前有人在这里发布过一个类似的问题,但不幸的是没有人可以给出足够的答案:

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.

首先,我想通过URL提取SRU生成的XML文件:

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 />");
 ?>

这给了我一个带有SimpleXMLElement的输出,仍然可以.另外,我检查$ fh是否已设置并且不为空:

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";
 }
 ?>

这给了我预期的输出$ fh包含一个值,并且可以通过生成输出这一事实看出来进行设置. 之后,我想获取此$ fh的元素,但这种方式行不通:

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";
?>

我还用isset和empty检查了$ var1,就像我在上面的示例中检查$ fh一样,发现$ var1没有设置并且它也是空的.尝试运行脚本时,我在尝试将内容填充到$ 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.

是的,关于xml文件结构:

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>

推荐答案

也许只有

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

您在$fh变量中已经有explainResponse.

这篇关于如何解析从SRU检索的XML文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆