使用 php SimpleXML 解析 XML 命名空间 [英] Parse XML namespaces with php SimpleXML
问题描述
我知道这已经被问过很多次了,但我没有得到任何建议来解决我的情况,我已经在网上和这里搜索过,尝试了所有的东西,但没有任何效果.我只需要使用命名空间上限解析这个 XML:并且只需要从中获取四个条目.
<name>w-nws.webmaster@noaa.gov</name>作者><title>NWS 于 5 月 6 日晚上 8:08 发布的特殊天气声明</title><link href="http://alerts.weather.gov/cap/wwacapget.php?x=TX124EFFB832F0.SpecialWeatherStatement.124EFFB84164TX.LUBSPSLUB.ac20a1425c958f66dc159gtbaea2f9e672"/<摘要>...针对科克伦和贝利县的重要天气建议...在 CDT 晚上 808 点...国家气象局多普勒雷达显示在莫顿西北 30 英里处有强雷暴...在 25 度向东南移动.镍大小的冰雹……风速高达 40 英里/小时……持续的云对地闪电……和短暂的中度倾盆大雨是可能的</summary><cap:event>特殊天气声明</cap:event><上限:有效>2013-05-06T20:08:00-05:00</上限:有效><cap:expires>2013-05-06T20:45:00-05:00</cap:expires><cap:status>实际</cap:status><cap:msgType>警报</cap:msgType><cap:category>符合</cap:category><cap:urgency>预期</cap:urgency><cap:severity>轻微</cap:severity><cap:certainty>观察到的</cap:certainty><cap:areaDesc>贝利;Cochran</cap:areaDesc><cap:polygon>34.19,-103.04 34.19,-103.03 33.98,-102.61 33.71,-102.61 33.63,-102.75 33.64,-103.05,-102.61>poly>poly>33.64,-103.05,-102.61<cap:geocode><valueName>FIPS6</valueName><值>048017 048079</值><valueName>UGC</valueName><值>TXZ027 TXZ033</值></cap:geocode><cap:参数><valueName>VTEC</valueName><价值></cap:参数></条目>
我正在使用 simpleXML 并且我设置了一个小的简单测试脚本,它非常适合解析常规元素.我不能为我的狄更斯找到或获得解析具有命名空间的元素的方法.
这是一个包含我正在使用的代码的小示例测试脚本,非常适合解析简单元素.我如何使用它来解析命名空间?我试过的一切都不起作用.我需要它能够创建变量,以便我能够将它们嵌入到 HTML 中以显示样式.
entry[$i]->title;$summary = $xml->entry[$i]->summary;$html .= "<p><strong>$title</strong></p><p>$summary</p><hr/>";}回声 $html;?>
这对于解析常规元素很有效,但是对于条目父项下带有 cap: 命名空间的元素呢?
registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');$result = $entries->xpath("//prefix:entry");//回声计数($asin);//echo "";print_r($asin);foreach ($result 作为 $entry):$title = $entry->title;$summary = $entry->summary;$html .= "<p><strong>$title</strong></p><p>$summary</p>$event<hr/>";Endforeach;万一;回声 $html;?>
任何帮助将不胜感激.
-谢谢
我已经给出了相同类型的答案 这里 - 解决您的问题
你只需要注册Namespace就可以正常使用simplexml_load_file和XPath
";print_r($entries);die;//除了注册名称空间之外的其他方式//$asin = $asins->xpath("///*[local-name() = 'ASIN']");$entries->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');$result = $entries->xpath("//prefix:entry");//回声计数($asin);//echo "";print_r($result);die;foreach ($result 作为 $entry)://echo "";print_r($entry);die;$dc = $entry->children('urn:oasis:names:tc:emergency:cap:1.1');echo $dc->event."<br/>";echo $dc-> Effective."<br/>";echo "
";Endforeach;万一;
就是这样.
I know this has been asked many many times but I haven't been able to get any of the suggestions to work with my situation and I have searched the web and here and tried everything and anything and nothing works. I just need to parse this XML with the namespace cap: and just need four entries from it.
<?xml version="1.0" encoding="UTF-8"?>
<entry>
<id>http://alerts.weather.gov/cap/wwacapget.php?x=TX124EFFB832F0.SpecialWeatherStatement.124EFFB84164TX.LUBSPSLUB.ac20a1425c958f66dc159baea2f9e672</id>
<updated>2013-05-06T20:08:00-05:00</updated>
<published>2013-05-06T20:08:00-05:00</published>
<author>
<name>w-nws.webmaster@noaa.gov</name>
</author>
<title>Special Weather Statement issued May 06 at 8:08PM CDT by NWS</title>
<link href="http://alerts.weather.gov/cap/wwacapget.php?x=TX124EFFB832F0.SpecialWeatherStatement.124EFFB84164TX.LUBSPSLUB.ac20a1425c958f66dc159baea2f9e672"/>
<summary>...SIGNIFICANT WEATHER ADVISORY FOR COCHRAN AND BAILEY COUNTIES... AT 808 PM CDT...NATIONAL WEATHER SERVICE DOPPLER RADAR INDICATED A STRONG THUNDERSTORM 30 MILES NORTHWEST OF MORTON...MOVING SOUTHEAST AT 25 MPH. NICKEL SIZE HAIL...WINDS SPEEDS UP TO 40 MPH...CONTINUOUS CLOUD TO GROUND LIGHTNING...AND BRIEF MODERATE DOWNPOURS ARE POSSIBLE WITH</summary>
<cap:event>Special Weather Statement</cap:event>
<cap:effective>2013-05-06T20:08:00-05:00</cap:effective>
<cap:expires>2013-05-06T20:45:00-05: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>Minor</cap:severity>
<cap:certainty>Observed</cap:certainty>
<cap:areaDesc>Bailey; Cochran</cap:areaDesc>
<cap:polygon>34.19,-103.04 34.19,-103.03 33.98,-102.61 33.71,-102.61 33.63,-102.75 33.64,-103.05 34.19,-103.04</cap:polygon>
<cap:geocode>
<valueName>FIPS6</valueName>
<value>048017 048079</value>
<valueName>UGC</valueName>
<value>TXZ027 TXZ033</value>
</cap:geocode>
<cap:parameter>
<valueName>VTEC</valueName>
<value>
</value>
</cap:parameter>
</entry>
I am using simpleXML and I have a small simple test script set up and it works great for parsing regular elements. I can't for the dickens of me find or get a way to parse the elements with the namespaces.
Here is a small sample test script with code I am using and works great for parsing simple elements. How do I use this to parse namespaces? Everything I've tried doesn't work. I need it to be able to create variables so I can be able to embed them in HTML for style.
<?php
$html = "";
// Get the XML Feed
$data = "http://alerts.weather.gov/cap/tx.php?x=1";
// load the xml into the object
$xml = simplexml_load_file($data);
for ($i = 0; $i < 10; $i++){
$title = $xml->entry[$i]->title;
$summary = $xml->entry[$i]->summary;
$html .= "<p><strong>$title</strong></p><p>$summary</p><hr/>";
}
echo $html;
?>
This works fine for parsing regular elements but what about the ones with the cap: namespace under the entry parent?
<?php
ini_set('display_errors','1');
$html = "";
$data = "http://alerts.weather.gov/cap/tx.php?x=1";
$entries = simplexml_load_file($data);
if(count($entries)):
//Registering NameSpace
$entries->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');
$result = $entries->xpath("//prefix:entry");
//echo count($asin);
//echo "<pre>";print_r($asin);
foreach ($result as $entry):
$title = $entry->title;
$summary = $entry->summary;
$html .= "<p><strong>$title</strong></p><p>$summary</p>$event<hr/>";
endforeach;
endif;
echo $html;
?>
Any help would be greatly appreciated.
-Thanks
I have given same type of answer here - solution to your question
You just need to register Namespace and then you can work normally with simplexml_load_file and XPath
<?php
$data = "http://alerts.weather.gov/cap/tx.php?x=1";
$entries = file_get_contents($data);
$entries = new SimpleXmlElement($entries);
if(count($entries)):
//echo "<pre>";print_r($entries);die;
//alternate way other than registring NameSpace
//$asin = $asins->xpath("//*[local-name() = 'ASIN']");
$entries->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');
$result = $entries->xpath("//prefix:entry");
//echo count($asin);
//echo "<pre>";print_r($result);die;
foreach ($result as $entry):
//echo "<pre>";print_r($entry);die;
$dc = $entry->children('urn:oasis:names:tc:emergency:cap:1.1');
echo $dc->event."<br/>";
echo $dc->effective."<br/>";
echo "<hr>";
endforeach;
endif;
That's it.
这篇关于使用 php SimpleXML 解析 XML 命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!