Removes whitespace nodes. Those whitespace nodes are required to reconstruct the original XML's spacing and indentation. If you call this and use saveXML() your original spacing will be gone. @nowebref @brief Removes whitespace nodes
()
| 604 | * @brief Removes whitespace nodes |
| 605 | */ |
| 606 | public void trim() { |
| 607 | try { |
| 608 | XPathFactory xpathFactory = XPathFactory.newInstance(); |
| 609 | XPathExpression xpathExp = |
| 610 | xpathFactory.newXPath().compile("//text()[normalize-space(.) = '']"); |
| 611 | NodeList emptyTextNodes = (NodeList) |
| 612 | xpathExp.evaluate(node, XPathConstants.NODESET); |
| 613 | |
| 614 | // Remove each empty text node from document. |
| 615 | for (int i = 0; i < emptyTextNodes.getLength(); i++) { |
| 616 | Node emptyTextNode = emptyTextNodes.item(i); |
| 617 | emptyTextNode.getParentNode().removeChild(emptyTextNode); |
| 618 | } |
| 619 | } catch (Exception e) { |
| 620 | throw new RuntimeException(e); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | |
| 625 | // /** Remove whitespace nodes. */ |
no test coverage detected