The question is published on by Tutorial Guruji team.
Having a Document
creted from the below response string. I have tried:
XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); String authors = xpath.evaluate("//name)", doc);
I get no hits.
I have also tried:
Element root = doc.getDocumentElement(); root.getElementsByTagName("name");
And I get no hits. Is there something to do with the namespaces? Can you point me in the right direction?
<?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>
Answer
The <name>
element is actually bound to the Atom namespace.
It’s easy to miss, since there is no namespace-prefix, but pay attention to the <feed>
element, there is xmlns="http://www.w3.org/2005/Atom"
which means that the <feed>
element (and it’s descendants) will be bound to that namespace.
So, your XPath either needs to be adjusted to use a namespace-prefix and set a namespace context to configure the prefix and 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);
Or, you can make your XPath a bit more generic to match on any element with a predicate to evaluate it’s local-name()
and namespace-uri()
:
String authors = xpath.evaluate("//*[local-name()='name' and namespace-uri()='http://www.w3.org/2005/Atom'])", doc);