一尘不染

使用php SimpleXML解析XML名称空间

php

我知道这个问题已经被问了很多遍了,但是我没有得到任何适合我情况的建议,因此我在网上和这里进行搜索,尝试了所有方法,但没有任何效果。我只需要用命名空间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,并且设置了一个小的简单测试脚本,它非常适合解析常规元素。我无法为自己的狄更斯找到或获得一种使用命名空间解析元素的方法。

这是一个小示例测试脚本,其中包含我正在使用的代码,非常适合解析简单元素。如何使用它来解析名称空间?我尝试过的所有方法均无效。我需要它能够创建变量,以便能够将它们嵌入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;

?>

任何帮助将不胜感激。

-谢谢


阅读 238

收藏
2020-05-29

共1个答案

一尘不染

您只需要注册命名空间,然后就可以正常使用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;

而已。

2020-05-29