In my code below, I am trying to access my 'handler' XML elements using XPath, but I am having no luck - the 'elemHandler' element is always null. Can anyone share with me the obvious solution? Thanks in advance.
import java.io.IOException;
import java.io.StringReader;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
public class XPathTest {
private static String jobString = "<job name=\"Workflow.JOB\">" +
" <handler name=\"xslt.converter\"/>" +
" <handler name=\"openoffice.renderer\">" +
" <opts input=\"ODS\" output=\"PDF\"/>" +
" </handler>" +
"</job>";
public static void main(String[] args) {
try {
Element elemJobInfo = new SAXBuilder().build(new StringReader(jobString)).detachRootElement();
XPath handlerExpression = XPath.newInstance("//stp:handler[2]");
handlerExpression.addNamespace("stp", "http://service.mine.org/dgs");
Element elemHandler = (Element) handlerExpression.selectSingleNode(elemJobInfo);
jobString = elemHandler.toString();
}
catch (IOException e) {
System.out.println("Failure: " + e);
}
catch (JDOMException e) {
System.out.println("Failure: " + e);
}
catch (Exception e) {
System.out.println("Failure: " + e);
}
}
}