问题
I try to write a simple XML file in Java using the DocumentBuilder. I expected the XML file to look like this:
<outer>
<inner>
<element name="WEB"/>
<element name="WEB"/>
<element name="WEB"/>
</inner>
</outer>
But it generates it like this:
<outer>
<inner>
<element name="WEB"/>
<element name="WEB"/>
<element name="WEB"/>
</inner>
</outer>
Why the third element does not have the same indentation as the other two elements? Note: I read the XML file again to simulate a method in a project, where I read an XML file, add one element and save it to the XML file. Here is my code:
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;
public class main {
private static String FILEPATH = "/tmp/xmltest.xml";
private static DocumentBuilderFactory docFactory;
private static DocumentBuilder docBuilder;
private static TransformerFactory transformerFactory;
private static Transformer transformer;
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, TransformerConfigurationException, TransformerException{
docFactory = DocumentBuilderFactory.newInstance();
docBuilder = docFactory.newDocumentBuilder();
transformerFactory= TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "1");
// Creating the XML file structure
Document document = docBuilder.newDocument();
Element rootElement = document.createElement("outer");
document.appendChild(rootElement);
Element inner = document.createElement("inner");
rootElement.appendChild(inner);
// Write XML file
write(document);
// Read XML file
document = docBuilder.parse(FILEPATH);
Element root = document.getDocumentElement();
Element innerElement = (Element)root.getElementsByTagName("inner").item(0);
// Add element
Element e = document.createElement("element");
e.setAttribute("name", "WEB");
innerElement.appendChild(e);
// Add element
e = document.createElement("element");
e.setAttribute("name", "WEB");
innerElement.appendChild(e);
// Write XML file
write(document);
// Read XML fil
document = docBuilder.parse(FILEPATH);
root = document.getDocumentElement();
innerElement = (Element)root.getElementsByTagName("inner").item(0);
// Add element
e = document.createElement("element");
e.setAttribute("name", "WEB");
innerElement.appendChild(e);
// Write XML file
write(document);
}
private static void write(Document document) throws TransformerException {
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(FILEPATH));
transformer.transform(source, result);
}
}
回答1:
the text nodes in xmlfile used for indententation are treated as data. because of this your indentation is going for toss. you can fix this as below:
private static void removeEmptyText(Node node){
Node child = node.getFirstChild();
while(child!=null){
Node sibling = child.getNextSibling();
if(child.getNodeType()==Node.TEXT_NODE){
if(child.getTextContent().trim().isEmpty())
node.removeChild(child);
}else
removeEmptyText(child);
child = sibling;
}
}
private static void write(Document document) throws TransformerException {
removeEmptyText(document.getDocumentElement());
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(FILEPATH));
transformer.transform(source, result);
}
here i am removing all indentation text nodes from dom before writing to file.
来源:https://stackoverflow.com/questions/31413413/java-documentbuilder-wrong-indentation-in-xml-file