0

你好,我有一个简单的问题。当我尝试将原子 XML 字符串转换为 simplexml 对象或使用 simplexml_load_data 函数时,它似乎删除了许多属性数据..

就像我原来的字符串一样

<?xml version="1.0" encoding="UTF-8"?>
<feed gd:kind="customsearch#search" xmlns="http://www.w3.org/2005/Atom" xmlns:cse="http://schemas.google.com/cseapi/2010" xmlns:gd="http://schemas.google.com/g/2005" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
 <title>Google Custom Search - flower</title>
 <id>tag:www.googleapis.com,2010-09-29:/customsearch/v1?q=flower&amp;&amp;num=10&amp;start=1&amp;safe=off</id>
 <author>
  <name>- Google Custom Search</name>
 </author>
 <updated>1970-01-16T10:55:22.093Z</updated>
 <opensearch:Url type="application/atom+xml" template="https://www.googleapis.com/customsearch/v1?q={searchTerms}&amp;num={count?}&amp;start={startIndex?}&amp;lr={language?}&amp;safe={cse:safe?}&amp;cx={cse:cx?}&amp;cref={cse:cref?}&amp;sort={cse:sort?}&amp;filter={cse:filter?}&amp;gl={cse:gl?}&amp;cr={cse:cr?}}&amp;googlehost={cse:googleHost?}&amp;c2coff={?cse:disableCnTwTranslation}&amp;hq={cse:hq?}&amp;hl={cse:hl?}&amp;siteSearch={cse:siteSearch?}&amp;siteSearchFilter={cse:siteSearchFilter?}&amp;exactTerms={cse:exactTerms?}&amp;excludeTerms={cse:excludeTerms?}&amp;linkSite={cse:linkSite?}&amp;orTerms={cse:orTerms?}&amp;relatedSite={cse:relatedSite?}&amp;dateRestrict={cse:dateRestrict?}&amp;lowRange={cse:lowRange?}&amp;highRange={cse:highRange?}&amp;searchType={cse:searchType?}&amp;fileType={cse:fileType?}&amp;rights={cse:rights?}&amp;imgsz={cse:imgsz?}&amp;imgtype={cse:imgtype?}&amp;imgc={cse:imgc?}&amp;imgcolor={cse:imgcolor?}&amp;alt=atom"/>
 <opensearch:Query role="request" title="Google Custom Search - flower" totalResults="98800000" searchTerms="flower" count="10" startIndex="1" inputEncoding="utf8" outputEncoding="utf8" cse:safe="off" cse:cx="g"/>
 <opensearch:Query role="cse:nextPage" title="Google Custom Search - flower" totalResults="98800000" searchTerms="flower" count="10" startIndex="11" inputEncoding="utf8" outputEncoding="utf8" cse:safe="off" cse:cx="0vv1rbg"/>
 <opensearch:totalResults>98800000</opensearch:totalResults>
 <opensearch:startIndex>1</opensearch:startIndex>

但是当我将它转换为 xml 使用

        $xml = simplexml_load_string($rs);
        print_r($xml);

我的输出如下所示。搜索次数、总结果集等所有关键信息都消失了。

SimpleXMLElement Object
(
    [title] => Google Custom Search - flower
    [id] => tag:www.googleapis.com,2010-09-29:/customsearch/v1?q=flower&cx=&num=10&start=1&safe=off
    [author] => SimpleXMLElement Object
        (
            [name] => Search Engine - Google Custom Search
        )

    [updated] => 1970-01-16T10:55:22.093Z
    [entry] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [id] => http://en.wikipedia.org/wiki/Flower
                    [updated] => 1970-01-16T10:55:22.093Z
                    [title] => <b>Flower</b> - Wikipedia, the free encyclopedia
                    [link] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [href] => http://en.wikipedia.org/wiki/Flower
                                    [title] => en.wikipedia.org
                                )

                        )

                    [summary] => A <b>flower</b>, sometimes known as a bloom or blossom, is the reproductive structure <br>  found in <b>flowering</b> plants (plants of the division Magnoliophyta, also called <b>...</b>
                )
4

1 回答 1

2

这些元素并没有消失,您只是无法在标准转储中“看到”它们,因为它们位于不同的命名空间中。您仍然可以像这样遍历它们:

foreach ($xml->children('opensearch', TRUE) as $element) {
    echo $element->getName(), "\n";
}

Where'opensearch'表示命名空间前缀,TRUE表示您使用了文档特定的前缀而不是完整的命名空间 url。

于 2012-05-08T10:29:00.180 回答