首页 > 解决方案 > 使用多个命名空间解析 Youtube XML 响应

问题描述

从下面的Document响应字符串中创建一个。我努力了:

XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
String authors = xpath.evaluate("//name)", doc);

我没有命中。
我也试过:

 Element root = doc.getDocumentElement();
 root.getElementsByTagName("name");

而且我没有命中。与命名空间有什么关系吗?你能为我指出正确的方向吗?

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns:yt="http://www.youtube.com/xml/schemas/2015" xmlns:media="http://search.yahoo.com/mrss/" xmlns="http://www.w3.org/2005/Atom">
    <link rel="self" href="http://www.youtube.com/feeds/videos.xml?user=someuser"/>
    <id>yt:channel:UCUMC8pdifsdLRKjocJqQI9lLw</id>
    <yt:channelId>UCUMC8pdifLRKsdjocJqQI9lLw</yt:channelId>
    <title>SomeUser</title>
    <link rel="alternate" href="https://www.youtube.com/channel/UCUMCasd8pdifLRKjocJqQI9lLw"/>
    <author>
        <name>SomeUser</name>
        <uri>https://www.youtube.com/channel/UCUsdMC8pdifLRKjocJqQI9lLw</uri>
    </author>
    <published>2006-12-09T06:07:04+00:00</published>
    <entry>
        <id>yt:video:xePc_paasdT3sX30</id>
        <yt:videoId>xePc_pT3asdsX30</yt:videoId>
        <yt:channelId>ddsasd</yt:channelId>
        <title>someuser -  Call</title>
        <link rel="alternate" href="https://www.youtube.com/watch?v=xeasPc_pT3sdX30"/>
        <author>
            <name>someuser</name>
            <uri>https://www.youtube.com/channel/UCUMC8pdifLRKjocJqQI9lLw</uri>
        </author>
        <published>2018-09-27T15:52:42+00:00</published>
        <updated>2018-09-27T15:57:01+00:00</updated>
        <media:group>
            <media:title>someuser  Call</media:title>
            <media:content url="https://www.youtube.com/v/xePc_paT3X30?version=3" type="application/x-shockwave-flash" width="640" height="390"/>
            <media:thumbnail url="https://i1.ytimg.com/vi/xePc_pT3X30/hqdefault.jpg" width="480" height="360"/>
            <media:description>Nearly g call? ;)</media:description>
            <media:community>
                <media:starRating count="0" average="0.00" min="1" max="5"/>
                <media:statistics views="46"/>
            </media:community>
        </media:group>
    </entry>
</feed>

标签: javaxmldomxpathyoutube

解决方案


<name>元素实际上绑定到 Atom 命名空间。

很容易错过,因为没有命名空间前缀,但请注意<feed>元素,xmlns="http://www.w3.org/2005/Atom"这意味着<feed>元素(及其后代)将绑定到该命名空间。

因此,您的 XPath 要么需要调整为使用命名空间前缀并设置命名空间上下文来配置前缀和命名空间 uri:

XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
HashMap<String, String> prefMap = new HashMap<>() {{
  put("a", "http://www.w3.org/2005/Atom");
}};
SimpleNamespaceContext namespaces = new SimpleNamespaceContext(prefMap);
xpath.setNamespaceContext(namespaces);
String authors = xpath.evaluate("//a:name)", doc);

或者,您可以使您的 XPath 更通用一点,以匹配任何带有谓词的元素,以评估它的local-name()and namespace-uri()

String authors = xpath.evaluate("//*[local-name()='name' and 
                                     namespace-uri()='http://www.w3.org/2005/Atom'])", doc);

推荐阅读