I am having a problem of storing the string that I have build with DOM and queried with xpath. Here is some reference that I have used in my code Google and Sample
public static String getRoute() throws Exception {
String xPathString = "//text() ";
String nodeString = "";
String notags = null;
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
//URL and HTTP connection here
InputStream inputXml = connection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(inputXml);
NodeList nodes = (NodeList) xpath.evaluate(xPathString, doc, XPathConstants.NODESET);
for (int i = 0, n = nodes.getLength(); i < n; i++) {
nodeString = nodes.item(i).getTextContent();
notags = nodeString.replaceAll("<[^>]*>", "");
System.out.print(notags + "\n");
}
} catch (XPathExpressionException ex) {
System.out.print("XPath Error");
}
return notags;
The code seems to printed out with System.out.print(notags + "\n"); within the try and catch block, however when I try to get the method and do system printout with:
public static void main (String[] args) {
try {
System.out.println(getRoute());
} catch (Exception e) {
System.out.println(e);
}
}
I only able to get the last line of the output instead of the whole String.
notags? It looks like it's assigned to null at the top and stays that way. I also don't see whereinputXPathis defined orxPathStringis used, but that's probably a moot point.