• ベストアンサー

PHP5でのXMLパーサーについて

PHP5で $xml = simplexml_load_file($url)を使って ヤフーオークションAPIから帰ってきたデータを処理したいのですが、 titleは {$xml->item[0]->title}で取得できることがわかったのですが、 totalAuctionsなど@attributesの中のデータを取得することができません。 どのようにすればよいかご教授願います。 よろしくお願いします。 SimpleXMLElement Object (  [@attributes] => Array  (   [totalAuctions] => 3677   [totalPage] => 74   [curPage] => 1  )  [item] => Array  (   [0] => SimpleXMLElement Object   (    [auctionID] => *****    [title] => ******    [seller] => SimpleXMLElement Object     (      [id] => ***      [itemlist] =>http://api.auctions.yahoo.co.jp/AuctionWebService/V1/SellingList?sellerID=***      [rating] =>ttp://rating2.auctions.yahoo.co.jp/jp/show/rating?userID=***     )      [url] => http://********************************      [img]=>http://**********************************.jpg      [price] => **,000 円      [bids] => **      [endtime] => *月 **日 **時 **分      [reserved] => SimpleXMLElement Object       (       )      [option] => SimpleXMLElement Object      (       [0] =>      )     )   ) )

  • PHP
  • 回答数2
  • ありがとう数2

質問者が選んだベストアンサー

  • ベストアンサー
  • racchoman
  • ベストアンサー率71% (20/28)
回答No.2

SimpleXMLElement->attributes() で属性が取得できます。 http://jp.php.net/manual/ja/function.simplexml-element-attributes.php DOMを使った方が楽かもしれませんね。

hola1973
質問者

お礼

回答ありがとうございます。 SimpleXMLElement->attributes()は、既に試していましたが全く取得できませんでした。 例文にあるように foreach($xml->attributes() as $a => $b) { echo $a,'="',$b,"\"<br>"; } のようにすると取れることがわかりました。 自分でやっていたのは、 $xml->AuctionSearchResult[0]->attributes()->totalAuctionとか やっていたのですが、$xml->attributesでいいのですね。 非常に勉強になりました。ありがとうございました。

その他の回答 (1)

noname#26650
noname#26650
回答No.1

参考URLの、「レスポンスフィールド」の説明が 参考になるかもしれません。

参考URL:
http://developer.yahoo.co.jp/auctions/rest/categoryleaf/V1/categoryleaf.html
hola1973
質問者

補足

このレスポンスフィールドの部分は見ています。 結果はこのように帰ってきます。 <AuctionCategoryLeaf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:jp:auc:leaf" xsi:schemaLocation="urn:yahoo:jp:auc:leaf http://api.auctions.yahoo.co.jp/AuctionWebService/V1/CategoryLeaf.xsd" totalFAuctions="69" totalAuctions="461" totalPage="11" curPage="1" categoryPath="オークション > この部分の属性のtotalFAuctions="69"とかがとりたいのです。 {$xml->attribute->totalFAuction}とやっても取得ができません。 よろしくお願いします。

関連するQ&A

  • PHP5でAPIの結果をXMLパースし参照する方法を教えて下さい。

    下記の様に、APIから情報をXMLパースすることは成功しましたが どうやって使用したらいいか分かりません。 例えば、Itemの0番のパソコンから簡単デジカメプリント注文【1枚15円】Lサイズ・DSCサイズを参照するには $xml->Body->ItemSearch->Items->Item[$i]->itemName だと思うのですが参照出来ません。 ご存じの方が」いらっしゃいましたら教えて下さい。 ------------------------------- SimpleXMLElement Object ( [Header] => SimpleXMLElement Object ( [Args] => SimpleXMLElement Object ( [Arg] => Array ( [0] => true ) ) [Status] => Success [StatusMsg] => SimpleXMLElement Object ( ) ) [Body] => SimpleXMLElement Object ( [ItemSearch] => SimpleXMLElement Object ( [count] => 23003 [page] => 1 [first] => 1 [last] => 30 [hits] => 30 [carrier] => 0 [pageCount] => 100 [Items] => SimpleXMLElement Object ( [Item] => Array ( [0] => SimpleXMLElement Object ( [itemName] => パソコンから簡単デジカメプリント注文【1枚15円】Lサイズ・DSCサイズ [itemCode] => gifucamera:843827 [itemPrice] => 15 [itemCaption] => DPEggと楽天からの二重請求が発生しないよう当店にて処理致します。 [itemUrl] => http:// [shopName] => カメラと写真の専門店ギフカメラ [shopCode] => gifucamera [shopUrl] => http://www.rakuten.co.jp/gifucamera/ [genreId] => 101750 ) [1] => SimpleXMLElement Object....続く (

    • 締切済み
    • PHP
  • Yahoo APIのXMLの取得について

    Yahoo APIのXMLで総数(totalResultsAvailable)を取得したいのですが、取得できません。 $url = "http://api.search.yahoo.co.jp/WebSearchService/xx/webSearch?appid=xxxxx&query=$query"; $xml= simplexml_load_file($url); $yahooxml=$xml->Result->Item ; $totalResults = $yahooxml->{totalResultsAvailable}; print_r ($totalResults); $urlは問題なく値が入ってるのですが、$totalResults ではSimpleXMLElement Object ( ) と返ってきてしまいます。どうすれば、$totalResults の値を取得できるのでしょうか?よろしくお願いいたします。

    • ベストアンサー
    • PHP
  • ★PHP★オブジェクトの並び替えを教えてください

    下記の[ss_category] の並びはそのままで、 [item] [0][1][2]のデータの並び替えの方法を教えてください。 ([2]のデータを一番上にしたいということです。) ご存知の方がおりましたらよろしくお願いします。 SimpleXMLElement Object ( [id] => 0.228142001317192226 [name] => aaa [ss_category] => Array ( [0] => SimpleXMLElement Object ( [id] => 0.697361001317192236 [name] => aaawww [description] => SimpleXMLElement Object ( ) [item] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => [name] => ええ ) [id] => 0.192165001317265048 [name] => ええ1 [description] => ええ1 [applyer] => ええ1 [ask_to] => SimpleXMLElement Object ( ) [upd_time] => 1317281103 [time_stamp] => 1317222000 [attach_file] => 0.192165001317265048 [attach_filename] => sql.txt ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => [name] => あああ ) [id] => 0.123983001317265270 [name] => あああ1 [description] => ああ1 [applyer] => ああ1 [ask_to] => SimpleXMLElement Object ( ) [upd_time] => 1317265632 [time_stamp] => 1317222000 [attach_file] => 0.123983001317265270 [attach_filename] => idm780.zip ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => [name] => www ) [id] => 0.955786001317265700 [name] => www [description] => www [applyer] => www [ask_to] => SimpleXMLElement Object ( ) [upd_time] => 1317265700 [time_stamp] => 1317222000 [attach_file] => 0.955786001317265700 [attach_filename] => idm780.zip ) ) ) [1] => SimpleXMLElement Object ( [id] => 0.706559001317265020 [name] => ああ [description] => ああ ) ) )

    • ベストアンサー
    • PHP
  • PHPで配列を使ってHTMLソースを取得したいです

    RSSで取得したXMLの中身からURLを取得したのですが、 それぞれのソースをループ構文を使って取得する方法はありますか? XMLの中身を配列として表示すると ソース↓ $RSS_data = simplexml_load_file($RSS_url); $i=0; foreach($RSS_data->URL as $value){ $result [$i] ['URL'] = $value->URL; $i++; } echo "<pre>" ; print_r($result) ; echo "</pre>" ; 結果↓ Array ( [0] => Array ( [URL] => SimpleXMLElement Object ( [0] => http://www.~ ) ) [1] => Array ( [URL] => SimpleXMLElement Object ( [0] => http://www~ ) ) [2] => Array ( [URL] => SimpleXMLElement Object ( [0] => http://www.~ ) ) [3] => Array ( [URL] => SimpleXMLElement Object ( [0] => http://www.~ ) ) こんな感じで続いていきます。 このURLの一つ一つのソースを取得したいのですがどうすればいいのでしょうか?

    • 締切済み
    • PHP
  • 配列のデータ抽出

    phpでgoogleニュースをxmlから抽出しようとしているのですが、 /*配列の中身↓*/ [entry] => Array ( [0] => SimpleXMLElement Object ( [title] => googleのニュースです ( [@attributes] => Array ( [href] => http://googlerss.com ) ) と、いつも使っているsimplexml_load_file関数から配列にいれて foreach ($xml->entry->@attributes as $item) { とした所、[@attributes]の@マークでエラーが出てしまいます。 このような場合、どのようにすれば、配列から情報をとれるのでしょうか。 宜しくお願いします。

    • 締切済み
    • PHP
  • $xml要素を階層指定して取得しようとすると、NULLになるのですが…

    $xml要素を階層指定して取得しようとすると、NULLになるのですが…。 ▽元のXMLファイル(楽天トラベルAPI) <Response> _<Body> __<KeywordHotelSearch:KeywordHotelSearch> ___<hotel> ____<hotelBasicInfo> _____<hotelNo>7531</hotelNo> _____<hotelName>パークハイアットホテル</hotelName> ▽$xml = simplexml_load_string($xml);した際の、var_dump($xml) object(SimpleXMLElement)#1 (2) { ["headerHeader"]=> object(SimpleXMLElement)#2 (3) { ["Args"]=> object(SimpleXMLElement)#4 (1) { ["Arg"]=> array(7) { [0]=> string(4) "true" [1]=> string(4) "true" [2]=> string(4) "true" [3]=> string(4) "true" [4]=> string(4) "true" [5]=> string(4) "true" [6]=> string(4) "true" } } ["Status"]=> string(7) "Success" ["StatusMsg"]=> object(SimpleXMLElement)#5 (0) { } } ["Body"]=> object(SimpleXMLElement)#3 (0) { } } object(SimpleXMLElement)#8 (0) { } この後、$items = $xml->Body->KeywordHotelSearchKeywordHotelSearch->hotel->hotelBasicInfo; とやると、$itemsが NULL になるのですが、何故でしょうか? (※KeywordHotelSearch:の:は、simplexml_load_stringする前に、str_replaceで処理済です) ちなみに、$items = $xml->Body->KeywordHotelSearchKeywordHotelSearch;では、$itemsは下記になります。 object(SimpleXMLElement)#4 (0) { }

    • ベストアンサー
    • PHP
  • PHPパーサー MagpieRSSについて

    PHPパーサーのMagpieRSSを使用しています。 読み込むxmlが更新されてもそれが反映されません。 また、htmlとして書き出す際に<p>タグにid名を付けたいのですが、idをつけるとエラーがでます。これはPHPでは文法的にまずいんでしょうか?下記が使用しているソースです。 重ねて質問なんですが、下記のソースでは1つのxmlしか読み込んでいませんが、複数のxmlを読み込むにはどうしたらいいのでしょうか。 よろしくお願いします。 <?php require_once 'rss_fetch.inc'; require_once 'rss_utils.inc'; $url = 'http://www.xxxxx/xxxxxx/index.xml'; $rss = fetch_rss($url); $title = $rss->channel['title']; $title = mb_convert_encoding($title, "UTF-8", "auto"); $rss->items = array_slice($rss->items, 0, 1); foreach ($rss->items as $item ) { $description = $item[description]; $description = mb_convert_encoding($description, "UTF-8", "auto"); $url = $item[link]; $date = date("Y.m.d", $item['date_timestamp']); echo "$description?n"; } ?>

    • ベストアンサー
    • PHP
  • PHPでのXMLの表示について

    PHPでのXMLの表示について PHPで下記のようなXMLファイルから <タイトル>改行 <コメント>改行 <日付> <名前> というように取り出したいのですが 私の書いたソースでは下記のXMLファイル から名前(1)、コメント(1)、日付(1)、件名(1)しか 取り出せません。。 名前(2)、コメント(2)、日付(2)、件名(2)を取り出す にはどのように記述をしたら良いか教えて頂けない でしょうか。。 #私が書いたソース <?php $xml = simplexml_load_file('http://api.tabelog.com/Ver1/ReviewSearch/?Rcd=<RestruantCode>&Key=<APIKey>&SortOrder=update&PageNum=1'); $NickName = $xml->Item->NickName; $VisitDate = $xml->Item->VisitDate; $Title = $xml->Item->Title; $Comment = $xml->Item->Comment; echo "<p><b>".$Title."</b><br>".$Comment."<br>".$VisitDate." ".$NickName."</p"; ?> #XML(元は食べログのAPIより取得) <?xml version="1.0" encoding="utf-8" ?> - <ReviewInfo> <NumOfResult>11</NumOfResult> - <Item> <NickName>名前(1)</NickName> <VisitDate>日付(1)</VisitDate> <ReviewDate>'10/04/29</ReviewDate> <UseType>夜のみ</UseType> <Situations>友人・同僚と</Situations> <TotalScore>3.0</TotalScore> <TasteScore>3.0</TasteScore> <ServiceScore>3.0</ServiceScore> <MoodScore>3.0</MoodScore> <PriceDinner>¥8,000~¥9,999</PriceDinner> <PriceLunch /> <Title>タイトル(1)</Title> <Comment>コメント(1)</Comment> <PcSiteUrl>URL(1)</PcSiteUrl> <MobileSiteUrl>URL(1)</MobileSiteUrl> </Item> - <Item> <NickName>名前(2)</NickName> <VisitDate>日付(2)</VisitDate> <ReviewDate>'10/04/17</ReviewDate> <UseType>昼のみ</UseType> <Situations>デート</Situations> <TotalScore>4.0</TotalScore> <TasteScore>4.0</TasteScore> <ServiceScore>4.0</ServiceScore> <MoodScore>4.0</MoodScore> <PriceDinner /> <PriceLunch>¥3,000~¥3,999</PriceLunch> <Title>タイトル(2)</Title> <Comment>コメント(2)</Comment> <PcSiteUrl>URL(2)</PcSiteUrl> <MobileSiteUrl>URL(2)</MobileSiteUrl> </Item> </ReviewInfo>

    • ベストアンサー
    • PHP
  • php rss取得方法

    こんにちは、rss取得方法を勉強中なのですが 下記phpでrssを取得しているのですが、$xml->entry[$i]['item']->titleなどで うまく表示されません。(外部phpからxoopsのpicoというモジュールのrssを取得しようとしています。) phpコード $rssurl = "url"; $xml = simplexml_load_file($rssurl); $num_of_data = 4; $outdata = ""; for ($i=0; $i<$num_of_data; $i++){ $outdata .= "<li>"; $outdata .= "<a href=\""; $outdata .= $xml->entry[$i]->link[href]; $outdata .= "\">"; $outdata .= $xml->entry[$i]['item']->title; $outdata .= "</a><br />\n"; $outdata .= "</li>"; } xmlの中身 SimpleXMLElement Object ( [@attributes] => Array ( [version] => 2.0 ) [channel] => SimpleXMLElement Object ( [title] => XOOPS Cube Site - pico [link] => http://localhost/xoops/modules/pico/ [description] => SimpleXMLElement Object ( ) [lastBuildDate] => Thu, 13 Dec 2012 02:52:29 +0100 [docs] => http://backend.userland.com/rss/ [generator] => XOOPS - pico [category] => TOP [webMaster] => admin@hotmail.co.jp [language] => ja [item] => Array ( [0] => SimpleXMLElement Object ( [title] => コンテンツ1 [link] => http://localhost/xoops/modules/pico/index.php/index.html [description] => 正式名称:NARUTO -ナルト概要:体内に九尾の妖狐を封印された落ちこぼれ忍者・うずまきナルトが、里一番の忍である火影を目指し、数々の試練を乗り越え成長していく物語。仲間との友... [guid] => http://localhost/xoops/modules/pico/index.php/index.html [pubDate] => Thu, 13 Dec 2012 02:52:29 +0100 [category] => アニメタイトル ) [1] => SimpleXMLElement Object ( [title] => ee [link] => http://localhost/xoops/modules/pico/index.php/content0003.html [description] => e [guid] => http://localhost/xoops/modules/pico/index.php/content0003.html [pubDate] => Thu, 13 Dec 2012 02:03:51 +0100 [category] => アニメタイトル ) [2] => SimpleXMLElement Object ( [title] => コンテンツ6 [link] => http://localhost/xoops/modules/pico/index.php/content0002.html [description] => コンテンツ2 [guid] => http://localhost/xoops/modules/pico/index.php/content0002.html [pubDate] => Thu, 13 Dec 2012 01:53:17 +0100 [category] => アニメタイトル ) ) ) )

    • 締切済み
    • PHP
  • file関数によるXMLの取得について

    XMLの取得をしようとしているのですが、PHPのバージョンが4.1のためSimpleXMLが使えずfile関数を使おうとしています。 例えば取得したいXMLの値が、 <Result>  <Title>タイトル</Title>  <Summary>  文章  </Summary>  <Url>  http://  </Url> </Result> だった場合、<Title>,<Summary>,<Url>はどのように取得するのでしょうか? 以下の方法で取得をしてみようとしたのですが、これだと<Summary>と<Url>は取得できるのですが<Title>が取得できません... ↓<Title>を取得する場合 $api_url = "http://"; $api_url2 = implode("",file("$api_url")); $title_exp = explode("<Title>",$api_url2); $title_count = count($title_exp); for($i=0;$i<$title_count;$i++){ $title = explode("</Title>",$title_exp[$i]); }; <Title>が他の項目と違うところは改行がない部分だけのようなのですが、どのようにすれば取得できるのでしょうか? 色々なサイトを見ながら手探りで学んでいますのでおかしなところがあるかもしれません。 その点もご指摘いただければ幸いです。 宜しくお願いします。

    • 締切済み
    • PHP

専門家に質問してみよう