how to remove empty lines from xml jFrame code example

Example: how to remove empty lines from xml jFrame

try {
	DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
	File xmlFile = new File("example.xml");
	Document doc = builder.parse(xmlFile);
    
    // Remove Code
	XPath xp = XPathFactory.newInstance().newXPath();
	NodeList nl = (NodeList) xp.evaluate("//text()[normalize-space(.)='']", doc, XPathConstants.NODESET);
	for (int i = 0; i < nl.getLength(); ++i) {
		Node node = nl.item(i);
		node.getParentNode().removeChild(node);
	}
    
	Transformer transformer = TransformerFactory.newInstance().newTransformer();
	transformer.setOutputProperty(OutputKeys.INDENT, "yes");
	transformer.transform(new DOMSource(doc), new StreamResult(xmlFile));
} catch (ParserConfigurationException e) {
	e.printStackTrace();
} catch (SAXException e) {
	e.printStackTrace();
} catch (IOException e) {
	e.printStackTrace();
} catch (XPathExpressionException e) {
	e.printStackTrace();
} catch (TransformerConfigurationException e) {
	e.printStackTrace();
} catch (TransformerException e) {
	e.printStackTrace();
}

Tags:

Misc Example