iT邦幫忙

0

PHP從XML中找出值

  • 分享至 

  • xImage

我從災害中心接到資料可以存到mysql
可是從裡面要找出 areaDesc
卻不行
以下是我的程式碼:
(第一行收到以下XML資料)

以下是資料庫收到的東西(存$xmldata的值)

https://ithelp.ithome.com.tw/upload/images/20180423/20109267QbkFStFxbq.png

weiclin iT邦高手 4 級 ‧ 2018-04-23 20:18:40 檢舉
你知道 xml 可以貼在程式碼區塊嗎? 不然想幫你的人還得手動輸入 xml 也太辛苦了
liao86221 iT邦新手 5 級 ‧ 2018-04-23 21:36:13 檢舉
<?xml version="1.0" encoding="UTF-8"?><alert xmlns="urn:oasis:names:tc:emergency:cap:1.2"><identifier>CWB-Weather_extremely-rain_201804231850001</identifier><sender>weather@cwb.gov.tw</sender><sent>2018-04-23T18:56:57+08:00</sent><status>Actual</status><msgType>Update</msgType><scope>Public</scope><references>weather@cwb.gov.tw,CWB-Weather_extremely-rain_201804231815001,2018-04-23T18:19:54+08:00 weather@cwb.gov.tw,CWB-Weather_extremely-rain_201804231655001,2018-04-23T17:02:04+08:00 weather@cwb.gov.tw,CWB-Weather_extremely-rain_201804231610001,2018-04-23T16:23:28+08:00 weather@cwb.gov.tw,CWB-Weather_extremely-rain_201804231430001,2018-04-23T14:39:08+08:00 weather@cwb.gov.tw,CWB-Weather_extremely-rain_201804231350001,2018-04-23T14:02:19+08:00</references><info><language>zh-TW</language><category>Met</category><event>降雨</event><responseType>Monitor</responseType><urgency>Expected</urgency><severity>Severe</severity><certainty>Likely</certainty><eventCode><valueName>profile:CAP-TWP:Event:1.0</valueName><value>rainfall</value></eventCode><effective>2018-04-23T18:50:00+08:00</effective><onset>2018-04-23T18:52:00+08:00</onset><expires>2018-04-24T00:00:00+08:00</expires><senderName>中央氣象局</senderName><headline>豪雨特報</headline><description>
午後對流雲系發展旺盛,易有短時強降雨,今(23日)晚臺東地區有局部大雨或豪雨發生,宜蘭及花蓮地區仍有局部大雨發生的機率,請注意瞬間大雨、雷擊及強陣風坍方及落石。
</description><web>https://www.cwb.gov.tw/V7/prevent/warning.htm</web><parameter><valueName>alert_title</valueName><value>豪雨特報</value></parameter><parameter><valueName>severity_level</valueName><value>豪雨</value></parameter><parameter><valueName>alert_color</valueName><value>橙色</value></parameter><parameter><valueName>website_color</valueName><value>255,128,0</value></parameter><area><areaDesc>臺東縣綠島鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001411</value></geocode></area><area><areaDesc>臺東縣太麻里鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001409</value></geocode></area><area><areaDesc>臺東縣長濱鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001408</value></geocode></area><area><areaDesc>臺東縣鹿野鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001405</value></geocode></area><area><areaDesc>臺東縣卑南鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001404</value></geocode></area><area><areaDesc>臺東縣東河鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001407</value></geocode></area><area><areaDesc>臺東縣池上鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001406</value></geocode></area><area><areaDesc>臺東縣臺東市</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001401</value></geocode></area><area><areaDesc>臺東縣關山鎮</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001403</value></geocode></area><area><areaDesc>臺東縣成功鎮</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001402</value></geocode></area><area><areaDesc>臺東縣金峰鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001414</value></geocode></area><area><areaDesc>臺東縣蘭嶼鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001416</value></geocode></area><area><areaDesc>臺東縣達仁鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001415</value></geocode></area><area><areaDesc>臺東縣海端鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001412</value></geocode></area><area><areaDesc>臺東縣延平鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001413</value></geocode></area><area><areaDesc>臺東縣大武鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001410</value></geocode></area></info><info><language>zh-TW</language><category>Met</category><event>降雨</event><responseType>Monitor</responseType><urgency>Future</urgency><severity>Moderate</severity><certainty>Likely</certainty><eventCode><valueName>profile:CAP-TWP:Event:1.0</valueName><value>rainfall</value></eventCode><effective>2018-04-23T18:50:00+08:00</effective><onset>2018-04-23T18:52:00+08:00</onset><expires>2018-04-24T00:00:00+08:00</expires><senderName>中央氣象局</senderName><headline>豪雨特報</headline><description>
午後對流雲系發展旺盛,易有短時強降雨,今(23日)晚臺東地區有局部大雨或豪雨發生,宜蘭及花蓮地區仍有局部大雨發生的機率,請注意瞬間大雨、雷擊及強陣風坍方及落石。
</description><web>https://www.cwb.gov.tw/V7/prevent/warning.htm</web><parameter><valueName>alert_title</valueName><value>大雨特報</value></parameter><parameter><valueName>severity_level</valueName><value>大雨</value></parameter><parameter><valueName>alert_color</valueName><value>黃色</value></parameter><parameter><valueName>website_color</valueName><value>255,255,0</value></parameter><area><areaDesc>花蓮縣新城鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001504</value></geocode></area><area><areaDesc>花蓮縣吉安鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001505</value></geocode></area><area><areaDesc>花蓮縣壽豐鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001506</value></geocode></area><area><areaDesc>花蓮縣光復鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001507</value></geocode></area><area><areaDesc>宜蘭縣冬山鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1000208</value></geocode></area><area><areaDesc>花蓮縣秀林鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001511</value></geocode></area><area><areaDesc>花蓮縣玉里鎮</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001503</value></geocode></area><area><areaDesc>宜蘭縣頭城鎮</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1000204</value></geocode></area><area><areaDesc>宜蘭縣礁溪鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1000205</value></geocode></area><area><areaDesc>宜蘭縣壯圍鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1000206</value></geocode></area><area><areaDesc>宜蘭縣員山鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1000207</value></geocode></area><area><areaDesc>花蓮縣鳳林鎮</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001502</value></geocode></area><area><areaDesc>宜蘭縣宜蘭市</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1000201</value></geocode></area><area><areaDesc>宜蘭縣大同鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1000211</value></geocode></area><area><areaDesc>宜蘭縣三星鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1000210</value></geocode></area><area><areaDesc>花蓮縣萬榮鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001512</value></geocode></area><area><areaDesc>花蓮縣瑞穗鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001509</value></geocode></area><area><areaDesc>花蓮縣豐濱鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001508</value></geocode></area><area><areaDesc>宜蘭縣南澳鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1000212</value></geocode></area><area><areaDesc>花蓮縣花蓮市</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001501</value></geocode></area><area><areaDesc>宜蘭縣羅東鎮</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1000202</value></geocode></area><area><areaDesc>花蓮縣富里鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001510</value></geocode></area><area><areaDesc>花蓮縣卓溪鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1001513</value></geocode></area><area><areaDesc>宜蘭縣蘇澳鎮</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1000203</value></geocode></area><area><areaDesc>宜蘭縣五結鄉</areaDesc><geocode><valueName>Taiwan_Geocode_103</valueName><value>1000209</value></geocode></area></info></alert>
liao86221 iT邦新手 5 級 ‧ 2018-04-23 21:39:36 檢舉
我發現是因為中文無法被正確讀取,會出現亂碼,可是使用utf8還是有問題
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1

找錯層了…
其他的以此類推…

 ["info"]=>
  array(2) {
    [0]=>
    object(SimpleXMLElement)#2 (17) {
      ["language"]=>
      string(5) "zh-TW"
      ["area"]=>
      array(16) {
        [0]=>
        object(SimpleXMLElement)#9 (2) {
          ["areaDesc"]=>
          string(18) "臺東縣綠島鄉"
          ["geocode"]=>
          object(SimpleXMLElement)#25 (2) {
            ["valueName"]=>
            string(18) "Taiwan_Geocode_103"
            ["value"]=>
            string(7) "1001411"
          }
        }
        [1]=>
        object(SimpleXMLElement)#10 (2) {
          ["areaDesc"]=>
          string(21) "臺東縣太麻里鄉"
          ["geocode"]=>
          object(SimpleXMLElement)#25 (2) {
            ["valueName"]=>
            string(18) "Taiwan_Geocode_103"
            ["value"]=>
            string(7) "1001409"
          }
        }

PHP:

$data->info[0]->area;

結果:

object(SimpleXMLElement)#2 (2) {
  ["areaDesc"]=>
  string(18) "臺東縣綠島鄉"
  ["geocode"]=>
  object(SimpleXMLElement)#3 (2) {
    ["valueName"]=>
    string(18) "Taiwan_Geocode_103"
    ["value"]=>
    string(7) "1001411"
  }
}

我要發表回答

立即登入回答