解析具有多个名称空间的Youtube XML响应

时间:2018-11-18 16:57:12

标签: java xml dom xpath youtube

具有以下响应字符串中的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>

1 个答案:

答案 0 :(得分:1)

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

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

因此,您需要调整XPath以使用命名空间前缀,并设置命名空间上下文以配置前缀和namespace-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()namespace-uri()

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