Dom4jUtil { public static String getTextOrNull(Node document, XPath xPath) { Node node = xPath.selectSingleNode(document); return node != null ? node.getStringValue() : null; }  static Document read(String xmlString); static String getTextOrNull(Node document, XPath xPath); static String getTextOrNull(Node document, String xPath); static String getTextOrEmpty(Node document, XPath xPath); static String getTextOrEmpty(Node document, String xPath); static String toText(Document document); static void serializeDom(Writer writer, Document document, Charset outCharset, boolean prettyPrint);  }
@Test public void getTextOrNull() throws SAXException, DocumentException { String xmlString = "<us-patent-grant><us-bibliographic-data-grant>" + "<invention-title id=\"d0e61\"><i>Streptococcus pneumoniae </i>proteins and nucleic acid molecules</invention-title>" + "</us-bibliographic-data-grant></us-patent-grant>"; Document doc = Dom4jUtil.read(xmlString); String actual = Dom4jUtil.getTextOrEmpty(doc, "/us-patent-grant/us-bibliographic-data-grant/invention-title"); assertEquals("Streptococcus pneumoniae proteins and nucleic acid molecules", actual); }