0

これはこれに続く質問です: PHPでXML属性を取得する

名前空間プレフィックスを持つ要素値を取得するのに苦労しています。ここからこの例を参照してください: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 '$' 

助言がありますか?

更新:誤解を招くような質問のタイトルを編集しました。私が興味を持っているのは、実際には名前空間プレフィックス(属性ではない)を持つ要素値です。

4

3 に答える 3

3

SimpleXMLを使用する場合(前の質問で示唆されているように)、 attributes()と同じように、 children()メソッドを使用して特定の名前空間のすべての子要素をフェッチする必要があります。

例えば

<?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
于 2012-09-10T09:29:54.233 に答える
0

-> phpdomdocument-
>タグ名と名前空間で要素を取得

于 2012-09-10T09:12:33.230 に答える
0

はい、わかった。

答えは、名前空間を指定することです。

たとえば、前の質問を参照して使用します。

$im = $e->children('http://itunes.apple.com/rss');
于 2012-09-10T09:43:37.760 に答える