它是重复的,只是不适用于已关闭的问题。 终于找到了答案,即使在这里。 实际重复: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