-1

它是重复的,只是不适用于已关闭的问题。终于找到了答案,即使在这里也是如此。实际重复:PHP SimpleXML 命名空间问题

编辑:如果你仔细阅读这个问题,你会发现它不是PHP 命名空间 simplexml 问题的重复。“可能重复”的答案不是我问题的答案。

再说一遍:
我对$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>。例如:

我可以得到 100 当它喜欢<cap:something>100</cap:something>。但如果是这样的话,我不能得到 100 <cap:something><value>100</value></cap:something>

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>w-nws.webmaster@noaa.gov</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>w-nws.webmaster@noaa.gov</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

}

最新尝试:
我阅读了更多关于命名空间的内容并更好地理解它们。我什至尝试了我认为更好的解决方案:

//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)

我不明白为什么我不能从具有包含命名空间的父标签的子标签中获取任何数据。

4

3 回答 3

0

如果您禁用了错误和警告,您面临的问题就不会那么明显:

命名空间错误:未定义东西的命名空间前缀上限

如果您启用错误,您将看到该消息。因为 simplexml 无法cap正确解析命名空间前缀,所以它将被删除。

因此,您可以直接访问它:

$xml->stuff->value[1]

和类似的。考虑以下代码示例(demo

$xml = simplexml_load_string('<entry>
 <cap:stuff>
  <valueName>aaa</valueName>
  <value>000</value>
  <valueName>bbb</valueName>
  <value>111</value>
 </cap:stuff>
</entry>');

echo "\nResult:", $xml->stuff->value[1], "\n\n";
echo "XML:\n", $xml->asXML();

它通过输出它来演示错误消息以及$xml加载 XML 字符串后的内容:

Warning: simplexml_load_string(): namespace error : Namespace prefix cap on \ 
                                  stuff is not defined on line 10
Warning: simplexml_load_string():  <cap:stuff> on line 10
Warning: simplexml_load_string():            ^ on line 10

Result:111

XML:
<?xml version="1.0"?>
<entry>
 <stuff>
  <valueName>aaa</valueName>
  <value>000</value>
  <valueName>bbb</valueName>
  <value>111</value>
 </stuff>
</entry>

如果你闻到某些东西应该有效但实际上没有用,那么总是需要仔细观察。一种选择是将字符串作为 XML 再次回显以查看 simplexml 已解析的内容,另一种是启用错误报告并查找警告和错误,它们通常包含更多信息。

于 2012-11-01T14:32:45.703 回答
0

您可能正在寻找功能。有2个例子,应该足以解决你的问题

于 2012-11-01T14:16:11.833 回答
0

cap:stuff是根,因此您可以按以下方式访问元素:

$xml = simplexml_load_string($your_xml);
$value_name_0 = $xml->valueName[0];
$value_0 = $xml->value[0];
$value_name_1 = $xml->valueName[1];
$value_1 = $xml->value[1];
于 2012-11-01T14:15:56.667 回答