Exemplo n.º 1
0
  /**
   * Removes empty #text nodes from a document. From James Murty on this StackOverflow post:
   * http://stackoverflow.com/questions/978810/how-to-strip-whitespace-only-text-nodes-from-a-dom-before-serialization
   *
   * @param doc The document to remove empty text nodes from.
   */
  private static void removeEmptyTextNodes(Document doc) {
    try {
      XPathFactory xpathFactory = XPathFactory.newInstance();
      // XPath to find empty text nodes.
      XPathExpression xpathExp =
          xpathFactory.newXPath().compile("//text()[normalize-space(.) = '']");
      NodeList emptyTextNodes = (NodeList) xpathExp.evaluate(doc, XPathConstants.NODESET);

      // Remove each empty text node from document.
      for (int i = 0; i < emptyTextNodes.getLength(); i++) {
        Node emptyTextNode = emptyTextNodes.item(i);
        emptyTextNode.getParentNode().removeChild(emptyTextNode);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
Exemplo n.º 2
0
 private NodeList findNodes(Document doc, Node operation) {
   List<Node> xpaths = getChildNodes(operation, "xpath");
   if (xpaths.isEmpty()) {
     return null;
   }
   String xpathExpression = xpaths.get(0).getTextContent();
   if (xpathExpression == null) {
     return null;
   }
   XPathFactory xPathfactory = XPathFactory.newInstance();
   XPath xpath = xPathfactory.newXPath();
   NodeList nl = null;
   try {
     XPathExpression expr = xpath.compile(xpathExpression);
     nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
   } catch (XPathExpressionException ex) {
     Utils.onError(new Error.WrongXpathExpression(xpathExpression));
   }
   return nl;
 }