它是重複的,只是不適用於已關閉的問題。 終於找到了答案,即使在這裏。 實際重複:PHP SimpleXML Namespace ProblemSimpleXML PHP解析
編輯:如果你仔細閱讀這個問題,你會發現它不是PHP namespace simplexml problems的重複。 「可能重複」的答案不是我的問題的答案。
還是那句話:
我都沒有問題$value = $record->children('cap', true)->$title;
(這是所有「可能重複的答案)
我有當有與結腸,在標籤內的其它標籤的問題。
<tag:something>hello</tag:something> //I parse out hello (this is the 'duplicate questions' answer that I don't need answered)
<tag:something>
<stuff>hello</stuff> //I cannot grab this. Explanation below.
</tag:something>
編輯結束。
原題:
我不能得到數據的標記<value>
內部在位於http://alerts.weather.gov/cap/us.php?x=1(以下XML的樣本)的XML。
的問題是:
$array[] = $record->children($tag_cap, true)->$tag_geocode->$tag_value;
這是我不能搶,我已經驗證了比所有其他$array[4]
其他數據被抓住的唯一數據。
當父標籤的格式爲<cap:something>
時,從標籤獲取數據時出現問題。例如:
當它像<cap:something>100</cap:something>
時,我可以得到100。但是如果它像<cap:something><value>100</value></cap:something>
那樣我不能得到100。
一塊XML的:
<?xml version = '1.0' encoding = 'UTF-8' standalone = 'yes'?>
<feed
xmlns = 'http://www.w3.org/2005/Atom'
xmlns:cap = 'urn:oasis:names:tc:emergency:cap:1.1'
xmlns:ha = 'http://www.alerting.net/namespace/index_1.0'
>
<!-- http-date = Tue, 30 Oct 2012 06:34:00 GMT -->
<id>http://alerts.weather.gov/cap/us.atom</id>
<logo>http://alerts.weather.gov/images/xml_logo.gif</logo>
<generator>NWS CAP Server</generator>
<updated>2012-10-30T14:34:00-04:00</updated>
<author>
<name>[email protected]</name>
</author>
<title>Current Watches, Warnings and Advisories for the United States Issued by the National Weather Service</title>
<link href='http://alerts.weather.gov/cap/us.atom'/>
<entry>
<id>http://alerts.weather.gov/cap/wwacapget.php?x=AK124CCADA8120.BlizzardWarning.124CCAE7BFC0AK.AFGWSWNSB.d32adb45b5c82ec5e486c4cfb96d3fb6</id>
<updated>2012-10-30T05:20:00-08:00</updated>
<published>2012-10-30T05:20:00-08:00</published>
<author>
<name>[email protected]</name>
</author>
<title>Blizzard Warning issued October 30 at 5:20AM AKDT until October 31 at 6:00AM AKDT by NWS</title>
<link href='http://alerts.weather.gov/cap/wwacapget.php?x=AK124CCADA8120.BlizzardWarning.124CCAE7BFC0AK.AFGWSWNSB.d32adb45b5c82ec5e486c4cfb96d3fb6'/>
<summary>...BLIZZARD WARNING IN EFFECT UNTIL 6 AM AKDT WEDNESDAY... THE NATIONAL WEATHER SERVICE IN FAIRBANKS HAS ISSUED A BLIZZARD WARNING...WHICH IS IN EFFECT UNTIL 6 AM AKDT WEDNESDAY. * VISIBILITY...NEAR ZERO IN SNOW AND BLOWING SNOW. * WINDS...WEST 35 MPH GUSTING TO 50 MPH. * SNOW...ACCUMULATION 3 INCHES THROUGH TONIGHT.</summary>
<cap:event>Blizzard Warning</cap:event>
<cap:effective>2012-10-30T05:20:00-08:00</cap:effective>
<cap:expires>2012-10-30T16:00:00-08:00</cap:expires>
<cap:status>Actual</cap:status>
<cap:msgType>Alert</cap:msgType>
<cap:category>Met</cap:category>
<cap:urgency>Expected</cap:urgency>
<cap:severity>Severe</cap:severity>
<cap:certainty>Likely</cap:certainty>
<cap:areaDesc>Eastern Beaufort Sea Coast</cap:areaDesc>
<cap:polygon></cap:polygon>
<cap:geocode>
<valueName>FIPS6</valueName>
<value>002185</value>
<valueName>UGC</valueName>
<value>AKZ204</value>
</cap:geocode>
<cap:parameter>
<valueName>VTEC</valueName>
<value>/X.NEW.PAFG.BZ.W.0013.121030T1320Z-121031T1400Z/</value>
</cap:parameter>
</entry>
...//rest of XML...
PHP代碼:
ini_set('display_errors','1');
$alert_url = 'http://alerts.weather.gov/cap/us.php?x=1';
$alert_string_xml = file_get_contents($alert_url);
$alert_simple_xml_object = simplexml_load_string($alert_string_xml);
$count = 0;
$tag_entry = 'entry';
$tag_summary = 'summary';
$tag_cap = 'cap';
$tag_event = 'event';
$tag_certainty = 'certainty';
$tag_areaDesc = 'areaDesc';
$tag_geocode = 'geocode';
$tag_value = 'value';
foreach ($alert_simple_xml_object->$tag_entry as $record)
{
$count++;
$array = array();
$array[] = $record->$tag_summary;
$array[] = $record->children($tag_cap, true)->$tag_event;
$array[] = $record->children($tag_cap, true)->$tag_certainty;
$array[] = $record->children($tag_cap, true)->$tag_areaDesc;
$array[] = $record->children($tag_cap, true)->$tag_geocode->$tag_value;
//$array[] = $record->children($tag_cap, true)->$tag_geocode->$tag_value[0]; //doesnt work either
echo $array[4]; //nothing is echoed
}
MOST當前的嘗試:
我閱讀更多的命名空間,並更好地瞭解他們。我甚至嘗試了我認爲是一個更好的解決方案:
//inside the above foreach loop
$namespaces = $record->getNameSpaces(true);
$caap = $record->children($namespaces['cap']);
echo $caap->event; //works (but the first way works too)
echo $caap->geocode->value; //(STILL does not work. Nothing is echoed)
我不明白爲什麼我不能搶來自具有包括命名空間的父標籤孩子標籤的任何數據。
'children()'不是PHP函數。你不能調用未定義的函數。因此你的代碼被破壞了。請更正它,以便更清楚您嘗試過的內容。 – hakre
孩子是SimpleXML的功能正確嗎?我爲這個問題添加了一些信息。 – Ryan
是的,它是'SimpleXMLElement'的一種方法,參見http://php.net/simplexmlelement.children – hakre