使用 php 解析 XML (SRU) [英] Parse XML (SRU) with php

查看:31
本文介绍了使用 php 解析 XML (SRU)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析一个不起作用的 XML 响应.在 API 文档中,它说它是一个基于 XML 的协议:SRU.那么这个 XML 解析代码会起作用吗?还是代码有问题?

I have tried to parse an XML response that does not work. In the API docs it says that it is a XML based protocol: SRU. Wont this XML parsing code work then? Or is it some error in the code?

代码:

$xmldoc = new DOMDocument();
$xmldoc->load('http://api.site.com/sru/query=' . $_GET['q']);
print_r($xmldoc);

$xpathvar = new Domxpath($xmldoc);
$queryResult = $xpathvar->query('//datafield[@tag="020"]/subfield[$code="a"]');
foreach ($queryResult as $result) {
    echo $result->textContent;
    print_r($result);
}

xml 结构:

<searchRetrieveResponse>
    <numberOfRecords>1</numberOfRecords>
    <datafield tag="020" ind1=" " ind2=" ">
        <subfield code="a">xyz</subfield>
    </datafield>

    ...

</searchRetrieveResponse>

推荐答案

$code 中的美元符号(在 xpathvar->query 中)看起来不像不属于那里.根据您的情况,尝试删除或转义美元符号(删除看起来很合适,给出示例 xml).

The dollar sign in $code (in the xpathvar->query) looks like it doesn't belong there. Try either removing or escaping the dollar sign, as appropriate in your case (removing looks appropriate, given the example xml).

这篇关于使用 php 解析 XML (SRU)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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