gpt4 book ai didi

php - 使用 php SimpleXML 解析 XML 命名空间

转载 作者:可可西里 更新时间:2023-10-31 22:43:16 25 4
gpt4 key购买 nike

我知道这个问题已经被问过很多次了,但我无法获得任何适合我的情况的建议,我已经在网上和这里进行了搜索,尝试了所有的方法,但没有任何效果。我只需要用命名空间 cap: 解析这个 XML,并且只需要其中的四个条目。

<?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>

我使用的是 simpleXML,我设置了一个简单的小测试脚本,它非常适合解析常规元素。我不能为我的狄更斯找到或得到一种方法来解析具有 namespace 的元素。

这是一个小示例测试脚本,其中包含我正在使用的代码,非常适合解析简单元素。我如何使用它来解析 namespace ?我尝试过的一切都不起作用。我需要它能够创建变量,以便能够将它们嵌入到 HTML 中以获得样式。

<?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;
?>

这对于解析常规元素来说效果很好,但是那些在条目父项下带有 cap: 命名空间的元素呢?

<?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;

?>

如有任何帮助,我们将不胜感激。

-谢谢

最佳答案

我给出了相同类型的答案 here - solution to your question

只需要注册Namespace就可以正常使用simplexml_load_file和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;

就是这样。

关于php - 使用 php SimpleXML 解析 XML 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16412047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com