I have xml as shown below (Google API) but cannot get the value of the gphoto:id element. How to do it? Note. When I use domFactory.setNamespaceAware(true); , /feed/entry xpath stops working.
<?xml version="1.0" encoding="UTF-8"?> <feed xmlns="http://www.w3.org/2005/Atom" xmlns:gphoto="http://schemas.google.com/photos/2007" xmlns:media="http://search.yahoo.com/mrss/" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/"> <entry> <title type="text">Test</title> <author> <name>username</name> <uri>https://picasaweb.google.com/113422203255202384532</uri> </author> <gphoto:id>57060151229174417</gphoto:id> </entry> </feed>
Java
NodeList nodes = (NodeList) path(body, "/feed/entry", XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node n = nodes.item(i); XPath xpath = XPathFactory.newInstance().newXPath(); // empty :( System.out.println( xpath.evaluate("id[namespace-uri()='http://schemas.google.com/photos/2007']",n) ); // empty too :( System.out.println( xpath.evaluate("gphoto:id",n) ); // ok System.out.println( xpath.evaluate("author",n) ); l.add(new Album("", "", "")); }
way method
private Object path(String content, String path, QName returnType) { try { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(content))); XPath xpath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xpath.compile(path); return expr.evaluate(doc, returnType); } catch (Exception e) { e.printStackTrace(); } return null; }
SOLVED according to @gioele path() answer now looks like this:
private Object path(String content, String path, QName returnType) { try { domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(content))); XPath xpath = XPathFactory.newInstance().newXPath(); NamespaceContext nsContext = new NamespaceContext() { @Override public Iterator getPrefixes(String namespaceURI) { return null; } @Override public String getPrefix(String namespaceURI) { return null; } @Override public String getNamespaceURI(String prefix) { if ("gphoto".equals(prefix)) return "http://schemas.google.com/photos/2007"; if ("media".equals(prefix)) return "http://search.yahoo.com/mrss/"; if("".equals(prefix)) return "http://www.w3.org/2005/Atom"; throw new IllegalArgumentException(prefix); } }; xpath.setNamespaceContext(nsContext); XPathExpression expr = xpath.compile(path); return expr.evaluate(doc, returnType); } catch (Exception e) { e.printStackTrace(); } return null; }
source share