Extra empty lines appear when saving the XML file

miniWorld

When saving an XML file in Java, multiple runs show that after each run, a line of white space is added between the elements, and the most recently inserted data is normal. How to solve this problem?

public class OperateXml {
    public static void main(String[] args) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse("src/main/resources/chunkInfo/chunk.xml");
            add(document);
            saveDocument(document, new File("src/main/resources/chunkInfo/chunk.xml"));
        } catch (Exception e) {

        }
    }

    public static void saveDocument(Document document, File xmlFile) {
        TransformerFactory tff = TransformerFactory.newInstance();
        tff.setAttribute("indent-number", 2);
        try {
            Transformer tf = tff.newTransformer();
            tf.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "chunk.dtd");
            tf.setOutputProperty(OutputKeys.INDENT, "yes");
            tf.transform(new DOMSource(document), new StreamResult(xmlFile));
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }

    public static void add(Document document) {
        Element root = document.getDocumentElement();
        Element chunk = document.createElement("chunk");
        root.appendChild(chunk);
        chunk.setAttribute("id", "id_8");
        Element name = document.createElement("name");
        name.setTextContent("test_6");
        chunk.appendChild(name);
        Element type = document.createElement("type");
        type.setTextContent("log");
        chunk.appendChild(type);
        Element item = document.createElement("item");
        item.setTextContent("2,3,4,5");
        chunk.appendChild(item);
    }
}

The result of saving XML file is: enter image description here

Thilo Schwarz

May be it's a result of a known bug in Java9 or later.

For further details see: https://bugs.openjdk.org/browse/JDK-8262285?attachmentViewMode=list

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Empty lines appear after the element is removed from the xml file

How to remove extra empty lines from XML file?

When saving scraped item and file, Scrapy inserts empty lines in output csv file

c++ function saving extra empty entries to a file

How to remove the lines which appear on file 1 from another file 2 KEEPING empty lines?

Saving remaining lines to file

Perl Parse XML file over HTTP with a few extra lines

JavaFX - tableview shows data but when saving to file, the file is empty

Empty pdf file when saving figure to file using matplotlib

Black Image Appear when Saving?

How to add alternating blank lines when saving csv file in Pandas

Not saving lines when writing to CSV file using for-loop

DataGridView creates extra lines when I import a text file into it.

Papa Parse giving extra empty row when parsing CSV file

Python program prints an extra empty line when reading a text file

Getting an Extra Empty line when exporting Excel Range to .txt file

Updating XML file with TransformerFactory adds empty lines between nodes

Saving outputs (some lines in a file)

saving xml file and ConformanceLevel

Detect empty lines in a file

When I try to edit a file with .write() it adds a lot of empty lines

Extra empty elements when using XML::LibXML::Reader instead of XML::LibXML

Saving Number of Lines in File as a Variable in Batch File

Why does an array of text lines appear to have an extra level of container?

Prevent atom editor from deleting last blank lines when saving a file

Run into error “empty beginning of file” when read.table in R with 5 or more first empty lines

saving RDD[Elem] to an XML file

grep lines that appear partly in one file to another

SQL file contents appear empty in Azure Devops when reviewing PR or browsing repo even though they're not

TOP Ranking

HotTag

Archive