Answer the question
In order to leave comments, you need to log in
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>
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();
}
<?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>
Answer the question
In order to leave comments, you need to log in
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. 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 questionAsk a Question
731 491 924 answers to any question