S
S
Sergey Khlopov2020-03-04 22:27:28
Java
Sergey Khlopov, 2020-03-04 22:27:28

Why do empty lines appear between tags in the xml file, and how can I remove them?

Hello, please tell me, there is an xml file:

<?xml version="1.0"?>
<countries>
    <country id="1">
        <continent>Северная Америка</continent>
        <name>Соединенные Штаты Америки</name>
        <area>9834000</area>
        <population>328915700</population>
        <minerals>Металлы,Алмазы,Бокситы,Барит,Золото</minerals>
    </country>
    <country id="2">
        <continent>Азия</continent>
        <name>Китай</name>
        <area>9597000</area>
        <population>1404328611</population>
        <minerals>Уголь,Железо,Медь,Алюминий</minerals>
    </country>
</countries>

Need to do adding new countries to this xml, implemented this:
try {
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   DocumentBuilder builder = factory.newDocumentBuilder();
   Document document = builder.parse(xml);
   document.getDocumentElement().normalize();
   Element root = document.getDocumentElement();
   root.appendChild(Elements.getCountry(document));
   document.getDocumentElement().normalize();
   TransformerFactory transformerFactory = TransformerFactory.newInstance();
   Transformer transformer = transformerFactory.newTransformer();
   transformer.setOutputProperty(OutputKeys.INDENT, "yes");
   DOMSource source = new DOMSource(document);
   StreamResult file = new StreamResult(xml);
   transformer.transform(source, file);
   System.out.println("Новая запись успешно создана");
} catch (Exception e) {
    e.printStackTrace();
}

As a result, I get the following xml output:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<countries>
        
    <country id="1">
                
        <continent>Северная Америка</continent>
                
        <name>Соединенные Штаты Америки</name>
                
        <area>9834000</area>
                
        <population>328915700</population>
                
        <minerals>Металлы,Алмазы,Бокситы,Барит,Золото</minerals>
            
    </country>
        
    <country id="2">
                
        <continent>Азия</continent>
                
        <name>Китай</name>
                
        <area>9597000</area>
                
        <population>1404328611</population>
                
        <minerals>Уголь,Железо,Медь,Алюминий</minerals>
            
    </country>
    <country>
        <continent>qwe</continent>
        <name>qwe</name>
        <area>qwe</area>
        <population>qwe</population>
        <minerals>qwe</minerals>
    </country>
</countries>

That is, those entries that were previously in xml, empty lines appear between their tags, and the new entry that was just added is fine, there are no empty lines between the tags. Please tell me how to remove empty lines, thanks in advance for the answer.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Khlopov, 2020-03-08
@Shlop

solved this problem, as I understood it was that this is the property that is set

transformer.setOutputProperty(OutputKeys.INDENT, "yes");
adds indents, and since I already had entries in the xml file, indents are added to them, in addition to those that were already in the file, so I got empty lines between the tags. Well, I solved the problem by simply building a new Document if I need to add something new to xml and overwriting the xml file with new data, and earlier I just added new data to the xml file and got empty lines between the tags.
Code example of how I create a new Document:
public static Document modelsToDocument(ArrayList<Country> countries) throws ParserConfigurationException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.newDocument();
        Element root = document.createElement("countries");
        document.appendChild(root);
        for(Country country : countries) {
            root.appendChild(Elements.getCountry(document,country));
        }
        return document;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question