【问题标题】:Getting XML values in PHP with namespace prefix在 PHP 中使用命名空间前缀获取 XML 值
【发布时间】:2012-09-10 09:08:00
【问题描述】:

这是一个后续问题: Getting XML attributes in PHP

我正在努力获取带有命名空间前缀的元素值。从这里查看这个例子:http://itunes.apple.com/us/rss/toppaidapplications/limit=10/genre=6014/xml

<im:image height="53">http://a2.mzstatic.com/us/r1000/114/Purple/v4/bc/0e/86/bc0e8619-5d48-1efe-2ac3-662696fb9a34/mzl.bimldzcn.53x53-50.png</im:image>          
<im:image height="75">http://a3.mzstatic.com/us/r1000/114/Purple/v4/bc/0e/86/bc0e8619-5d48-1efe-2ac3-662696fb9a34/mzl.bimldzcn.75x75-65.png</im:image>

如果我尝试使用 $e->im:image 引用元素,则会收到以下错误:

PHP Parse error:  syntax error, unexpected ':' 

$e->'im:image' 给了我:

PHP Parse error:  syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_STRING or T_VARIABLE or '{' or '$' 

有什么建议吗?

更新:我编辑了具有误导性的问题标题。它实际上是我感兴趣的带有命名空间前缀(不是属性)的元素值。

【问题讨论】:

    标签: php xml namespaces


    【解决方案1】:

    使用 SimpleXML 时(如 your previous question 所建议),您必须使用 children() 方法来获取某个命名空间的所有子元素,与 attributes() 相同。

    例如

    <?php
    define('URI_RSS', 'http://itunes.apple.com/rss');
    $feed = new SimpleXMLElement(getData());
    
    foreach( $feed->entry as $entry ) {
        $attributes = $entry->id->attributes(URI_RSS);
        echo 'id[im:id]: ', $attributes['id'], "\n";
    
        $attributes = $entry->category->attributes(URI_RSS);
        echo 'category[im:id]: ', $attributes['id'], "\n";
    
        $imElements = $entry->children(URI_RSS);
        foreach( $imElements as $name=>$c ) {
            echo $name, '=', $c, "\n";
        }
    }
    
    function getData() {
        return <<< eot
    <?xml version="1.0" encoding="utf-8"?>
    
        <feed xmlns:im="http://itunes.apple.com/rss" xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
            <id>someid</id><title>some title</title><updated>2012-09-10T02:12:06-07:00</updated><link rel="alternate" type="text/html" href="http://link1"/><link rel="self" href="http://link12"/><icon>http://iconlink1</icon><author><name>some name</name><uri>http://uri</uri></author><rights>all rights wronged</rights>
    
                <entry>
                    <updated>2012-09-10T02:12:06-07:00</updated>
    
                        <id im:id="123456789" im:bundleId="a.bundle.id">http://id</id>
    
                        <title>some title</title>
    
                        <summary>This is the summary.
    
    A short text summarizing the complete article
    </summary>
    
                        <im:name>imname</im:name>
    
                        <link rel="alternate" type="text/html" href="http://alternate"/>
    
                        <im:contentType term="Application" label="Application"/>
    
                        <category im:id="7890" term="example" scheme="http://scheme" label="example"/>
    
                        <link title="Preview" rel="enclosure" type="image/jpeg" href="http://preview" im:assetType="preview"><im:duration>0</im:duration></link>
    
                        <im:artist href="http://artist">sample artist</im:artist>
    
                        <im:price amount="0.99000" currency="USD">$0.99</im:price>
    
                        <im:image height="53">http://1.png</im:image>
    
                        <im:image height="75">http://2.png</im:image>
    
                        <im:image height="100">http://3.png</im:image>
    
                        <rights>all rights wronged</rights>
    
                        <im:releaseDate label="August 28, 2012">2012-08-28T00:00:00-07:00</im:releaseDate>
    
    
                        <content type="html">content
                        content
                        content
    </content>
    
                </entry>
            </feed>
    eot;
    }
    

    打印

    id[im:id]: 123456789
    category[im:id]: 7890
    name=imname
    contentType=
    artist=sample artist
    price=$0.99
    image=http://1.png
    image=http://2.png
    image=http://3.png
    releaseDate=2012-08-28T00:00:00-07:00
    

    【讨论】:

    • 抱歉 - 更新了问题以使其更准确。我感兴趣的是元素值而不是属性。
    • 好的。显然,尽管我最终使用了一种更简单的方法(请参阅我的答案),但这很有效,但我会在你的方法上打勾,因为它更全面。
    • 同样的方法,$entry-&gt;children(URI_RSS);,我刚刚定义了URI_RSS ;-)
    【解决方案2】:
    【解决方案3】:

    好的,明白了。

    答案是指定命名空间。

    例如请参阅上一个问题和使用:

    $im = $e->children('http://itunes.apple.com/rss');
    

    【讨论】:

      猜你喜欢
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多