使用php SimpleXML解析XML名称空间 [英] Parse XML namespaces with php SimpleXML

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

问题描述

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

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>

我正在使用simpleXML,并且设置了一个小的简单测试脚本,它非常适合解析常规元素.我无法为自己的狄更斯找到或获得一种使用命名空间解析元素的方法.

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.

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

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

这对于解析常规元素很好用,但是在父项下具有cap:名称空间的元素呢?

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;

?>

任何帮助将不胜感激.

-谢谢

推荐答案

我给出了相同类型的答案

I have given same type of answer here - solution to your question

您只需要注册命名空间,然后就可以正常使用simplexml_load_file和XPath

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;

就是这样.

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

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