Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java XML - after removing a node - white space line remains - how to remove?

I'm working with javax.xml & After removing some node:

docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilder = docBuilderFactory.newDocumentBuilder();
document = docBuilder.parse(new File("file_name"));
NodeList tagList = document.getElementsByTagName("tag_name");
tagList.item(0).getParentNode().removeChild(waysList.item(0));

Instead of the removed tag, I have an empty line.

How can avoid this or remove the empty line?

XML file is:

<root>
    <tag_name>text</tag_name>
    <tag_name>text</tag_name>
    <tag_name>text</tag_name>
</root>

EDIT: If I add tagList.item(0).getParentNode().setTextContent(""); before or after the removeChild I get a NullPointerException.

Thanks,

like image 919
michael Avatar asked Jan 08 '17 14:01

michael


1 Answers

Indent before the element and the Carriage return after it are text nodes. If you remove an element and there's a text node before or after it, you will see it as whitespace lines.

 public static void main(String[] args){
    File file=new File(XmlProcessing.class.getResource("InputXml.txt").getFile());
    // create a new DocumentBuilderFactory
      DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
      try{
    DocumentBuilder  docBuilder = docBuilderFactory.newDocumentBuilder();
    Document document = docBuilder.parse(file);
    System.out.println("Before removing the Node:-");
    printDocument(document, System.out);
    NodeList tagList = document.getElementsByTagName("tag_name");
        Element elemToBeRemoved = (Element)tagList.item( 0 );
        Node prevElem = elemToBeRemoved.getPreviousSibling();
        if (prevElem != null && 
            prevElem .getNodeType() == Node.TEXT_NODE &&
            prevElem .getNodeValue().trim().length() == 0) {
            elemToBeRemoved.getParentNode().removeChild(prevElem );
        }
        elemToBeRemoved.getParentNode().removeChild( elemToBeRemoved );  
       System.out.println("\nAfter removing the node:-");
        printDocument(document, System.out);


      }catch(Exception excep){
          excep.printStackTrace();
      }

}
public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    transformer.transform(new DOMSource(doc), 
         new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}


/** Output:-
Before removing the Node:-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
    <tag_name>text</tag_name>
    <tag_name>text</tag_name>
    <tag_name>text</tag_name>
</root>

After removing the node:-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
    <tag_name>text</tag_name>
    <tag_name>text</tag_name>
</root>

*/
like image 115
Amit Bhati Avatar answered Sep 28 '22 06:09

Amit Bhati