Getting xml string from Document in Java -
i have java program aiming consider xml dom , write string. using these packages: org.w3c.dom.*
, javax.xml.parsers.*;
so have documentbuilder
, document
, element
objects...
is there way string representing xml dom in 1 call????
its not 1 call but:
transformerfactory transfac = transformerfactory.newinstance(); transformer trans = transfac.newtransformer(); trans.setoutputproperty(outputkeys.method, "xml"); trans.setoutputproperty(outputkeys.indent, "yes"); trans.setoutputproperty("{http://xml.apache.org/xslt}indent-amount", integer.tostring(2)); stringwriter sw = new stringwriter(); streamresult result = new streamresult(sw); domsource source = new domsource(doc.getdocumentelement()); trans.transform(source, result); string xmlstring = sw.tostring();
the setoutputproperty method makes string output prettier can take out.
Comments
Post a Comment