2017-04-18 13 views
1

私は以下の形式のxmlファイルを持っています。PHPでXMLファイルのデータを読み取る方法は?

<?xml version="1.0" encoding="utf-8" ?> 
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0"> 
    <channel> 
    <title>Data feed Title</title> 
    <link>http://www.amarchitrakatha.com/in/</link> 
    <description>Data feed description.</description> 
    <item>ACKKALID0577 
    <g:id><![CDATA[ACKKALID0577]]></g:id> 
    <title><![CDATA[Kalidasa]]></title> 
    <link><![CDATA[http://www.amarchitrakatha.com/in/kalidasa?fee=19&fep=1147]]></link> 
    <g:price>70.00</g:price> 
    <g:sale_price></g:sale_price> 
    <description><![CDATA[Kalidas Owes His Fame To His Sanksrit Play Abhijnana-Shakuntala (Shakuntala Recognised By The Token Ring), The Long Epic Poem Kumara-Sambhava (Birth Of Kumara) And The Lyric Poem Meghaduta (Cloud Messenger). Kalidasa, Who Lived Some Time In The Middle Of The 4Th And Early 5Th Centuries A.D., Has Left No Account Of His Life. According To Popular Legend, He Owed His Ingenuity As Much To Goddess Kali’S Blessings As To His Own Talents.]]></description> 
    <g:product_type><![CDATA[Amar Chitra Katha > Visionaries > Singles]]></g:product_type> 
    <g:google_product_category><![CDATA[]]></g:google_product_category> 
    <g:image_link><![CDATA[http://www.amarchitrakatha.com/media/catalog/product/K/A/KALIDASA_0__5.jpg]]></g:image_link> 
    <g:condition>new</g:condition> 
    <g:availability>in stock</g:availability> 
    <g:shipping_weight>0.09 kg</g:shipping_weight> 
    <g:brand><![CDATA[]]></g:brand> 
    <g:mpn><![CDATA[ACKKALID0577]]></g:mpn> 
    <g:gtin><![CDATA[]]></g:gtin> 
    </item> 
    <item>ACKPRITH1413 
    <g:id><![CDATA[ACKPRITH1413]]></g:id> 
    <title><![CDATA[Prithviraj Chauhan]]></title> 
    <link><![CDATA[http://www.amarchitrakatha.com/in/prithviraj-chauhan?fee=19&fep=1150]]></link> 
    <g:price>70.00</g:price> 
    <g:sale_price></g:sale_price> 
    <description><![CDATA[Jaichand Was Furious! His Daughter Samyogita Had Eloped With Prithviraj Chauhan, The Warrior King Of Delhi. She, Like Many Others, Was Smitten By Tales Of His Daring, His Nobility And His Sense Of Honour. Though The Young Couple’S Happiness Was Doomed, Even In His Dying Hour, It Was The Brave Prithviraj Who Chose How His Life Should End.]]></description> 
    <g:product_type><![CDATA[Amar Chitra Katha > Brave Hearts Story > Singles]]></g:product_type> 
    <g:google_product_category><![CDATA[]]></g:google_product_category> 
    <g:image_link><![CDATA[http://www.amarchitrakatha.com/media/catalog/product/P/R/PRITHVIRAJ_CHAUHAN_0__4.jpg]]></g:image_link> 
    <g:condition>new</g:condition> 
    <g:availability>in stock</g:availability> 
    <g:shipping_weight>0.09 kg</g:shipping_weight> 
    <g:brand><![CDATA[]]></g:brand> 
    <g:mpn><![CDATA[ACKPRITH1413]]></g:mpn> 
    <g:gtin><![CDATA[]]></g:gtin> 
    </item> 
</channel> 
</rss> 

simplexml_load_file私はアイテムのデータを取得していません。どのようにPHPでこの形式のXMLを解析するのですか?

if (file_exists('Sample.xml')) { 
$xml = simplexml_load_file('Sample.xml'); 
echo '<pre>'; 
print_r($xml->channel); 
echo '</pre>'; 
} else { 
exit('Failed to open Sample.xml.'); 
} 

などのID、タイトル、リンクと私は特定の変数内の項目のデータを格納する必要が
SimpleXMLElement Object 
(
[title] => Data feed Title 
[link] => http://www.amarchitrakatha.com/in/ 
[description] => Data feed description. 
[item] => Array 
    (
     [0] => ACKKALID0577 

     [1] => ACKPRITH1413 
    ) 
) 

として結果を得るメートル:私はグラムのようなすべての項目のデータを取得したいです。

+0

あなたのPHPのバージョンは何ですか? – kolunar

+1

XMLをインポートしましたが、インポートに使用しようとしているコードは含まれていません。結果をより正確に説明するとともに、コードを記入してください。 「アイテムのデータを取得していない」とは数多くのものがあります。エラーログを確認し、疑わしいものはすべて[あなたの質問に含めてください](http:// stackoverflow。co.jp/posts/43464051/edit)。あなたがそれをしている間、あなたはあなたの結果で何をしたいのかについてより完全な記述を含めることができます。最良の方向は通常あなたの目的地を知ることによって決定されます。 :) – ghoti

+0

私はコードを編集しました。 – Magecode

答えて

0

私は(http://ca2.php.net/manual/en/class.simplexmlelement.phpすなわち)このXMLスタイルで使用すると、標準のPHP XMLパーサーを使用することができると信じて、あなたの変数に ロード、それを(つまり、$ XML) そして、そのように、これらのノードをお読みください。

$xml=simplexml_load_string($yourXMLFile) 
$xml->children('g', true)->id; 

(上記のすべてのインスタンスは< g:id >です。

私の最後はテストされていません。

0

PHPDOMDocumentを入力すると利用できます。私が以下のコードで行ったように。

Try this code snippet here

<?php 
ini_set('display_errors', 1); 
$object= new DOMDocument(); 
$object->loadXML('<?xml version="1.0" encoding="utf-8" ?> 
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0"> 
    <channel> 
    <title>Data feed Title</title> 
    <link>http://www.amarchitrakatha.com/in/</link> 
    <description>Data feed description.</description> 
    <item>ACKKALID0577 
    <g:id><![CDATA[ACKKALID0577]]></g:id> 
    <title><![CDATA[Kalidasa]]></title> 
    <link><![CDATA[http://www.amarchitrakatha.com/in/kalidasa?fee=19&fep=1147]]></link> 
    <g:price>70.00</g:price> 
    <g:sale_price></g:sale_price> 
    <description><![CDATA[Kalidas Owes His Fame To His Sanksrit Play Abhijnana-Shakuntala (Shakuntala Recognised By The Token Ring), The Long Epic Poem Kumara-Sambhava (Birth Of Kumara) And The Lyric Poem Meghaduta (Cloud Messenger). Kalidasa, Who Lived Some Time In The Middle Of The 4Th And Early 5Th Centuries A.D., Has Left No Account Of His Life. According To Popular Legend, He Owed His Ingenuity As Much To Goddess Kali’S Blessings As To His Own Talents.]]></description> 
    <g:product_type><![CDATA[Amar Chitra Katha > Visionaries > Singles]]></g:product_type> 
    <g:google_product_category><![CDATA[]]></g:google_product_category> 
    <g:image_link><![CDATA[http://www.amarchitrakatha.com/media/catalog/product/K/A/KALIDASA_0__5.jpg]]></g:image_link> 
    <g:condition>new</g:condition> 
    <g:availability>in stock</g:availability> 
    <g:shipping_weight>0.09 kg</g:shipping_weight> 
    <g:brand><![CDATA[]]></g:brand> 
    <g:mpn><![CDATA[ACKKALID0577]]></g:mpn> 
    <g:gtin><![CDATA[]]></g:gtin> 
    </item> 
    <item>ACKPRITH1413 
    <g:id><![CDATA[ACKPRITH1413]]></g:id> 
    <title><![CDATA[Prithviraj Chauhan]]></title> 
    <link><![CDATA[http://www.amarchitrakatha.com/in/prithviraj-chauhan?fee=19&fep=1150]]></link> 
    <g:price>70.00</g:price> 
    <g:sale_price></g:sale_price> 
    <description><![CDATA[Jaichand Was Furious! His Daughter Samyogita Had Eloped With Prithviraj Chauhan, The Warrior King Of Delhi. She, Like Many Others, Was Smitten By Tales Of His Daring, His Nobility And His Sense Of Honour. Though The Young Couple’S Happiness Was Doomed, Even In His Dying Hour, It Was The Brave Prithviraj Who Chose How His Life Should End.]]></description> 
    <g:product_type><![CDATA[Amar Chitra Katha > Brave Hearts Story > Singles]]></g:product_type> 
    <g:google_product_category><![CDATA[]]></g:google_product_category> 
    <g:image_link><![CDATA[http://www.amarchitrakatha.com/media/catalog/product/P/R/PRITHVIRAJ_CHAUHAN_0__4.jpg]]></g:image_link> 
    <g:condition>new</g:condition> 
    <g:availability>in stock</g:availability> 
    <g:shipping_weight>0.09 kg</g:shipping_weight> 
    <g:brand><![CDATA[]]></g:brand> 
    <g:mpn><![CDATA[ACKPRITH1413]]></g:mpn> 
    <g:gtin><![CDATA[]]></g:gtin> 
    </item> 
</channel> 
</rss>'); 
$tagValues=array("g:id","title","link"); 
$result=array(); 
foreach($object->getElementsByTagName("item") as $item) 
{ 
    foreach($item->childNodes as $node) 
    { 
     if($node instanceof DOMElement) 
     { 
      if(in_array($node->tagName,$tagValues)) 
      { 
       $result[$node->tagName][]=$node->textContent; 
      } 
     } 
    } 
} 
print_r($result); 
+0

結果が得られましたが、データを特定の変数に格納する必要があります。 – Magecode

+0

@Cvince Okay no issues自分の投稿を数秒で更新しています。 –

+0

@Cvince今すぐ私の投稿をチェックしてみてください。このコードを試してください。この後、 'extract'も使うことができます。 –

0

テストし、これを試してみてください!

$xml = simplexml_load_file('http://example.com/rss.xml'); 
$namespaces = $xml->getNamespaces(true); 
var_dump($namespaces); 

出力:

array(1) { 
    ["g"]=> string(29) "http://base.google.com/ns/1.0" 
} 

そして:

foreach ($xml->channel->item as $item) 
{ 
    // for php >= 5.2.0 
    // $id = (string) $item->children('g', TRUE)->id; 
    // for php < 5.2.0 
    // $id = (string) $item->children('http://base.google.com/ns/1.0')->id; 
    $id = (string) $item->children($namespaces["g"])->id; // for php < 5.2.0 
    $title = (string) $item->title; 
    $link = (string) $item->link; 
    var_dump($id, $title, $link); 
    echo "--\n"; 
} 

出力:例えばための最後

string(12) "ACKKALID0577" 
string(8) "Kalidasa" 
string(58) "http://www.amarchitrakatha.com/in/kalidasa?fee=19&fep=1147" 
-- 
string(12) "ACKPRITH1413" 
string(18) "Prithviraj Chauhan" 
string(68) "http://www.amarchitrakatha.com/in/prithviraj-chauhan?fee=19&fep=1150" 
-- 

、オブジェクトの配列に結果をバインドするために:

$xml = simplexml_load_file('http://example.com/rss.xml'); 
$namespaces = $xml->getNamespaces(true); 
$itemArray = array(); 
foreach ($xml->channel->item as $item) 
{ 
    $obj = new stdClass(); 
    $obj->id = (string) $item->children($namespaces["g"])->id; // for php < 5.2.0 
    $obj->title = (string) $item->title; 
    $obj->link = (string) $item->link; 
    $itemArray[] = $obj; 
} 
var_dump($itemArray); 

出力:

array(2) { 
    [0]=> object(stdClass)#5 (3) { 
     ["id"]=> string(12) "ACKKALID0577" 
     ["title"]=> string(8) "Kalidasa" 
     ["link"]=> string(58) "http://www.amarchitrakatha.com/in/kalidasa?fee=19&fep=1147" 
    } 
    [1]=> object(stdClass)#3 (3) { 
     ["id"]=> string(12) "ACKPRITH1413" 
     ["title"]=> string(18) "Prithviraj Chauhan" 
     ["link"]=> string(68) "http://www.amarchitrakatha.com/in/prithviraj-chauhan?fee=19&fep=1150" 
    } 
} 

参考:http://php.net/manual/en/simplexmlelement.getnamespaces.php

参考:http://php.net/manual/en/simplexmlelement.children.php

参考:http://www.sitepoint.com/forums/showthread.php?606057-...

0

あなたはg内の値をループすることができます:この属性を使用する属性

$handle_object = $element->children('g', true); 
関連する問題