this.resultsElem = doc.getElementById("resultsId");
}
public void handleEvent(Event evt)
{
HTMLDocument doc = itsNatDoc.getHTMLDocument();
while (resultsElem.getFirstChild() != null)
resultsElem.removeChild(resultsElem.getFirstChild());
String expression = expressionInput.getText();
try
{
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
xpath.setNamespaceContext(new UniversalNamespaceResolver(doc));
XPathExpression exp = xpath.compile(expression); // "//*[name()='h1' or name()='h2']"
NodeList nodeList = (NodeList) exp.evaluate(doc, XPathConstants.NODESET);
for(int i = 0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
Element child = doc.createElement("div");
String value;
if (node.getNodeType() == Node.TEXT_NODE) value = "Text node: " + ((Text)node).getData();
else if (node.getNodeType() == Node.ELEMENT_NODE) value = "Element: " + node.getNodeName();
else
value = node.getNodeName();
child.appendChild(doc.createTextNode(value));
resultsElem.appendChild(child);
}
}
catch(XPathExpressionException ex)
{
//ex.printStackTrace();
String exMsg = ex.toString();
Throwable cause = ex.getCause(); // javax.xml.transform.TransformerException
if (cause != null) exMsg = exMsg + "\n" + cause.toString();
Text text = doc.createTextNode("ERROR: " + exMsg);
resultsElem.appendChild(text);
}
}