Java XML dom: prevent collapsing empty element -


i use javax.xml.parsers.documentbuilder, , want write org.w3c.dom.document file.

if there empty element, default output collapsed:

<element/> 

can change behavior doesn't collapse element? i.e.:

<element></element> 

thanks help.

this actualy depends on way how you're writing document file , has nothing dom itself. following example uses popular transformer-based approach:

documentbuilderfactory factory = documentbuilderfactory.newinstance();       document document = factory.newdocumentbuilder().newdocument();              element element = document.createelement("tag");                             document.appendchild(element);                                               transformerfactory transformerfactory = transformerfactory.newinstance();    transformer transformer = transformerfactory.newtransformer();               transformer.setoutputproperty(outputkeys.method, "html");                    domsource source = new domsource(document);                                  streamresult result = new streamresult(system.out);                          transformer.transform(source, result);                                  

it outputs <tag></tag> you're expecting. please note, changing output method has other side effects, missing xml declaration.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -