rss Xml 命名空间混淆 [英] rss Xml namespace confusion

查看:26
本文介绍了rss Xml 命名空间混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/"  xmlns:jskit="http://purl.org/dc/elements/1.1/" >
    <channel>
        <title>www.domain.com/page_735.htm comments</title>
        <link>http://www.domain.com/page_735.html</link>
        <atom:link rel="self" type="application/rss+xml" href="http://js-kit.com/rss/domain.com/page_735.html"></atom:link>
        <jskit:attribute key="md5path" value="eb7110ce84f5907c29f0717c171ad35e"></jskit:attribute>
        <jskit:attribute key="path" value="/page_735.html"></jskit:attribute>
        <description>RSS comments feed for www.domain.com/page_735.html</description>
        <generator>JS-Kit Bulk Site Exporter 0.8</generator>
        <lastBuildDate>Mon, 09 Nov 2009 10:35:47 +0000</lastBuildDate>
        <item>
            <guid>jsid-1259747304-188</guid>
            <pubDate>Wed, 02 Dec 2009 09:48:24 +0000</pubDate>
            <jskit:attribute key="IP" value="59.182.xxx.xxx"></jskit:attribute>
            <jskit:attribute key="permalink" value="http://www.domain.com/page_735.html"></jskit:attribute>
            <author>guest</author>
            <jskit:attribute key="share_facebook" value="off"></jskit:attribute>
            <jskit:attribute key="share_gfc" value="off"></jskit:attribute>
            <jskit:attribute key="share_twitter" value="off"></jskit:attribute>
            <jskit:attribute key="share_friendfeed" value="off"></jskit:attribute>
            <jskit:attribute key="share_yahoo" value="off"></jskit:attribute>
            <jskit:attribute key="Webpresence" value="[]"></jskit:attribute>
            <description>im a disco dancer</description>
            <jskit:parent-guid>jsid-1250154466-622</jskit:parent-guid>
        </item>
    </channel>
</rss>

我知道一点 xml 但这一种方式超出了我的想象:(

I know a bit of xml but this one way beyond my imagination :(

我如何提取永久链接或 IP 或 parent-guid 的值

How do i extract value of permalink or IP or parent-guid

我只能提取 guid,pubdate,author 和 description

I can only extract guid,pubdate ,author and description

我不知道命名空间

推荐答案

您必须使用 XPath 来找到正确的节点,然后从中获取值.xpath() 总是返回一个数组,因此您必须编写一个只返回该数组的第一个元素的小函数.

You will have to use XPath to find the right nodes, then get the value out of that. xpath() always returns an array, so you'll have to write a small function that returns only the first element of that array.

要访问命名空间元素,您可以使用 XPath 表达式或 SimpleXML 的 children() 方法.因为parent-guid"包含一个连字符,所以写属性名有点尴尬.

To access namespaced elements, you can either use an XPath expression or SimpleXML's children() method. Because "parent-guid" contains an hyphen, it makes writing the name of the property a bit awkward.

这是一个工作示例:

function attr(SimpleXMLElement $item, $key)
{
    $values = $item->xpath('./jskit:attribute[@key="' . $key . '"]/@value');
    return $values[0];
}

$rss = simplexml_load_string($xml);

foreach ($rss->channel->item as $item)
{
    $permalink   = attr($item, 'permalink');

    // either
    $parent_guid = $item->children('http://purl.org/dc/elements/1.1/')->{'parent-guid'};

    // or (PHP 5.2)
    $parent_guid = $item->children('jskit', true)->{'parent-guid'};

    // or
    $parent_guid = $item->xpath('./jskit:parent-guid');
    $parent_guid = $parent_guid[0];
}

这篇关于rss Xml 命名空间混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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