Java DocumentBuilder - wrong indentation in XML file -
i try write simple xml file in java using documentbuilder. expected xml file this:
<outer> <inner> <element name="web"/> <element name="web"/> <element name="web"/> </inner> </outer>
but generates this:
<outer> <inner> <element name="web"/> <element name="web"/> <element name="web"/> </inner> </outer>
why third element not have same indentation other 2 elements? note: read xml file again simulate method in project, read xml file, add 1 element , save xml file. here 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 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); } }
the text nodes in xmlfile used indententation treated data. because of indentation going toss. can fix 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 removing indentation text nodes dom before writing file.
Comments
Post a Comment