tags:

views:

145

answers:

1

Hi given the following Code:

private void extractLink(ScriptFile file) throws SAXException, IOException,
   ParserConfigurationException, XPathExpressionException {
  Document d = this.parseFile(file);
  XPathFactory xpf = XPathFactory.newInstance();
  XPath xpath = xpf.newXPath();
  XPathExpression expr = xpath.compile("//link");
  Object result = expr.evaluate(d, XPathConstants.NODE);
  Node node = (Node) result;
  if(result!=null)
  {
   this.log.debug("Links found: "+node.toString());
  }
  else
  {
   this.log.debug("No link found!");
  }
 }

 private Document parseFile(ScriptFile file) throws SAXException, IOException, ParserConfigurationException
 {
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  dbf.setValidating(false);
  dbf.setNamespaceAware(true);
  dbf.setIgnoringComments(true);
  dbf.setIgnoringElementContentWhitespace(false);
  dbf.setExpandEntityReferences(false);
  dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
  DocumentBuilder db = dbf.newDocumentBuilder();
  return db.parse(new ByteArrayInputStream(file.getFile()));
 }

And an input like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case"&gt;
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="" />
<title>Default-Config-Accounts</title>
</head>
<body>
</body>
</html>

Why does my query return null?

+1  A: 

I'm not familiar with Java in general, but my XPath suspicions are aroused by the lack of (obvious to me) namespace handling in your code. From your input, the tag is in the default namespace "http://www.w3.org/1999/xhtml", so I'd expect you to have to write some code that tells the Java XPath apparatus about this namespace.

A little googling finds this useful blog entry XPath with namespaces in Java which looks to me like it will solve your problem.

Dan Blanchard
Thanks Dan! You were right. It was a namespace issue. The blog entry solved my problem.
er4z0r
+1. It always is a namespace issue… XML namespaces never fail to come as a surprise. Obviously they are done away with as "yet another attribute I don't need to care about".
Tomalak