s.surya Prakash
s.surya Prakash

Reputation: 37

XML writing in java creating unnecessary new lines

I am using w3c DOM to write xml file. when i used to create first child node no trouble occurs. For the 2nd time if i'm appending a new node in pre existing file it creates unwanted new lines in previous nodes and the new lines kept increasing everytime when i used to insert new nodes. Here is my code...

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(new File("D:\\TestXml.xml"));
        Element rootElement = doc.getDocumentElement();
        Element supercar = doc.createElement("supercars");
        rootElement.appendChild(supercar);
        Element carname = doc.createElement("carname");
        carname.appendChild(doc.createTextNode("Ferrari 103"));
        supercar.appendChild(carname);
        Element carname1 = doc.createElement("carname");
        carname1.appendChild(doc.createTextNode("Ferrari 204"));
        supercar.appendChild(carname1);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("D:\\TestXml.xml"));
        transformer.transform(source, result);

And here is the Generated File.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<cars>
        
    <supercars>
                
        <carname>Ferrari 101</carname>
                
        <carname>Ferrari 202</carname>
            
    </supercars>
    
    <supercars>
        <carname>Ferrari 103</carname>
        <carname>Ferrari 204</carname>
    </supercars>
</cars>

The Code above is used to append the 2nd node for the 1'st time the generated file haves no extra new lines. And if add 10 new nodes the file haves so many unnecessary new lines resulting in more than 300 lines. Also the file size got increased. I cannot able to come to a conclusion that why this is occurring. The Problem occurring for every new node insertion. Any suggestions will be really helpful.

Upvotes: 1

Views: 986

Answers (1)

Parfait
Parfait

Reputation: 107767

Consider running the identity transform XSLT where its <xsl:strip-space> removes such line breaks and spaces between nodes. You can easily incorporate XSLT in your existing code:

XSLT (save below as .xsl file, copies entire document as is)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

Java

import javax.xml.transform.stream.StreamSource;
...

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new File("D:\\TestXml.xml"));

Element rootElement = doc.getDocumentElement();
Element supercar = doc.createElement("supercars");
rootElement.appendChild(supercar);
Element carname = doc.createElement("carname");
carname.appendChild(doc.createTextNode("Ferrari 103"));
supercar.appendChild(carname);
Element carname1 = doc.createElement("carname");
carname1.appendChild(doc.createTextNode("Ferrari 204"));
supercar.appendChild(carname1);

Source xslt = new StreamSource(new File("C:\\Path\\To\\Style.xsl"));       // LOAD STYLESHEET
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xslt);         // APPLY XSLT
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("D:\\TestXml.xml"));
transformer.transform(source, result);

Upvotes: 3

Related Questions