简体   繁体   English

在PHP中解析XML将返回SimpleXMLElement对象。 接下来是什么?

[英]Parsing XML in PHP returns SimpleXMLElement Object. What next?

Below is the response I get when I try to parse the XML code returned from Library of Congress SRU Service for a book search. 下面是我尝试解析从美国国会图书馆SRU服务返回的XML代码进行图书搜索时得到的响应。 How do I look into [recordData] => SimpleXMLElement Object to get the title, creator, publisher information? 如何查看[recordData] => SimpleXMLElement对象以获取标题,创建者,发布者信息? This is the first time I am delving into XML. 这是我第一次钻研XML。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Here is the response I get. 这是我得到的回应。 I found code to deal with the name spaces, however cannot figure out how to get beyond the recordData tag. 我找到了处理名称空间的代码,但无法弄清楚如何超越recordData标记。

SimpleXMLElement Object
        (
            [version] => 1.1
            [numberOfRecords] => 1
            [records] => SimpleXMLElement Object
                (
                    [record] => SimpleXMLElement Object
                        (
                            [recordSchema] => info:srw/schema/1/dc-v1.1
                            [recordPacking] => xml
                            [recordData] => SimpleXMLElement Object
                                (
                                )

                            [recordPosition] => 1
                        )

                )


        $entry=simplexml_load_file('xml_data.xml');
        $namespaces = $entry->getNameSpaces(true);
        $yr = $entry->children($namespaces['zs']); 
        print_r($yr);


        <zs:searchRetrieveResponse xmlns:zs="http://www.loc.gov/zing/srw/">
        <zs:version>1.1</zs:version>
        <zs:numberOfRecords>1</zs:numberOfRecords>
        <zs:records>
        <zs:record>
        <zs:recordSchema>info:srw/schema/1/dc-v1.1</zs:recordSchema>
        <zs:recordPacking>xml</zs:recordPacking>
        <zs:recordData>
        <srw_dc:dc xmlns:srw_dc="info:srw/schema/1/dc-schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://purl.org/dc/elements/1.1/" xsi:schemaLocation="info:srw/schema/1/dc-schema http://www.loc.gov/standards/sru/resources/dc-schema.xsd">
        <title>My life /</title>
        <creator>Clinton, Bill, 1946-</creator>
        <type>text</type>
        <publisher>New York : Knopf,</publisher>
        <date>2004.</date>
        <language>eng</language>
        <description>Includes index.</description>
        <subject>Clinton, Bill, 1946-</subject>
        <subject>Clinton, Bill, 1946---Family.</subject>
        <subject>Clinton family.</subject>
        <subject>Presidents--United States--Biography.</subject>
        <coverage>United States--Politics and government--1993-2001.</coverage>
        <identifier>
        http://www.loc.gov/catdir/samples/random051/2004107564.html
        </identifier>
        <identifier>
        http://www.loc.gov/catdir/description/random051/2004107564.html
        </identifier>
        <identifier>URN:ISBN:0375414576</identifier>
        </srw_dc:dc>
        </zs:recordData>
        <zs:recordPosition>1</zs:recordPosition>
        </zs:record>
        </zs:records>
        </zs:searchRetrieveResponse>

See the docs : 查看文档:

http://us3.php.net/SimpleXMLElement http://us3.php.net/SimpleXMLElement

A simple example there shows: 一个简单的例子显示:

foreach( $xmldata->children() AS $child )
{
    //run any query you want on the children.. they are also nodes.
    $name = $child->getName();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM