SimpleXML PHP解析 [英] SimpleXML PHP Parsing

查看:73
本文介绍了SimpleXML PHP解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是重复的,只是不是针对已关闭的问题. 终于找到了答案,即使在SO上也是如此. 实际重复: PHP SimpleXML命名空间问题

It IS a duplicate, just not for the question that is was closed for. Finally found the answer, even here on SO. Actual Duplicate: PHP SimpleXML Namespace Problem

如果您仔细阅读问题,将会发现它不是 PHP名称空间simplexml的重复项问题. 可能重复"的答案不是我的问题的答案.

If you read the question closely, you will see it is NOT a duplicate of PHP namespace simplexml problems. The answer from the 'possible duplicate' is not the answer to my question.

再次:
我对$value = $record->children('cap', true)->$title;没问题.(所有可能重复"的答案)
当带有冒号的标记中还有其他标记时,我会遇到问题.

Again:
I have no problem with $value = $record->children('cap', true)->$title;.(which is all the 'possible duplicate' answers)
I have a problem when there are other tags inside the tag with the colon.

<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>内的数据:rel ="nofollow noreferrer"> http: //alerts.weather.gov/cap/us.php?x=1 (以下XML示例).

ORIGINAL question:
I cannot get the data inside the tag <value> in the XML located at http://alerts.weather.gov/cap/us.php?x=1 (sample of XML below).

问题出在:

$array[] = $record->children($tag_cap, true)->$tag_geocode->$tag_value;

这是我无法捕获的唯一数据,我已经验证了是否捕获了除$array[4]以外的所有其他数据.

This is the only data I cannot grab, I have verified that all the other data other than $array[4] is grabbed.

当父标签的格式为<cap:something>时,从标签获取数据只是一个问题.例如:

There is just a problem getting data from tags when the parent tag is in the form <cap:something>. For example:

当它像<cap:something>100</cap:something>时我可以得到100.但是,如果像<cap:something><value>100</value></cap:something>这样,我就得不到100.

I can get 100 when it is like <cap:something>100</cap:something>. But I cant get 100 if it was like <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

}

最大电流尝试:
我阅读了有关名称空间的更多信息,并更好地理解了它们.我什至尝试了我认为是更好的解决方案:

MOST CURRENT ATTEMPT:
I read more on namespaces and understand them better. I even tried what I thought was a better solution:

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

我不明白为什么我无法从具有包含名称空间的父标记的子标记中获取任何数据.

I don't understand why I cannot grab any data from children tags that have a parent tag that includes a namespace.

推荐答案

如果您禁用了错误和警告,则所面临的问题并不明显:

The problem you are facing is not that visible if you have errors and warnings disabled:

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

namespace error : Namespace prefix cap on stuff is not defined

如果启用错误,则会看到该消息.由于simplexml无法正确解析名称空间前缀cap,因此将其删除.

If you would have errors enable you would see that message. Because simplexml is not able to parse the namespace prefix cap properly, it will be dropped.

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

Therefore you access it directly:

$xml->stuff->value[1]

和类似.考虑以下代码示例(演示:

And similar. Consider the following code-example (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中的内容:

It demonstrates the error message as well what is in $xml after loading the XML string by outputting it:

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解析了什么,另一种选择是启用错误报告并查找警告和错误,它们通常包含更多信息.

If you smell that something should work but it isn't, it is always necessary to look closer. One option is to echo the string again as XML to see what simplexml has parsed, the other is to enable error reporting and looking for warnings and error, they often contain further information.

这篇关于SimpleXML PHP解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆