簡體   English   中英

在PHP中解析XML將返回SimpleXMLElement對象。 接下來是什么?

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

下面是我嘗試解析從美國國會圖書館SRU服務返回的XML代碼進行圖書搜索時得到的響應。 如何查看[recordData] => SimpleXMLElement對象以獲取標題,創建者,發布者信息? 這是我第一次鑽研XML。 任何幫助將不勝感激。 這是我得到的回應。 我找到了處理名稱空間的代碼,但無法弄清楚如何超越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>

查看文檔:

http://us3.php.net/SimpleXMLElement

一個簡單的例子顯示:

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