Java generates indentation and line feed for XML

Source: Internet
Author: User

Dom the following code uses Dom to generate an XML document

Package XML; </P> <p> Import Java. io. file; <br/> Import Java. io. filenotfoundexception; <br/> Import Java. io. fileoutputstream; </P> <p> Import javax. XML. parsers. documentbuilder; <br/> Import javax. XML. parsers. documentbuilderfactory; <br/> Import javax. XML. parsers. parserconfigurationexception; <br/> Import javax. XML. transform. transformer; <br/> Import javax. XML. transform. transformerconfigurationexception; <br/> Import javax. XML. transform. transformerexception; <br/> Import javax. XML. transform. transformerfactory; <br/> Import javax. XML. transform. dom. domsource; <br/> Import javax. XML. transform. stream. streamresult; </P> <p> Import Org. w3C. dom. document; <br/> Import Org. w3C. dom. element; </P> <p> public class xmlhandler {<br/> Public void createxml () throws filenotfoundexception {<br/> document DOC = NULL; <br/> documentbuilderfactory DBF = documentbuilderfactory. newinstance (); </P> <p> try {<br/> documentbuilder docbuilder = DBF. newdocumentbuilder (); <br/> Doc = docbuilder. newdocument (); <br/> element root = Doc. createelement ("root"); <br/> Doc. appendchild (Root); </P> <p> element Country = Doc. createelement ("contry"); <br/> country. appendchild (Doc. createtextnode ("China"); <br/> root. appendchild (country); </P> <p> element city = Doc. createelement ("city"); <br/> city. appendchild (Doc. createtextnode ("Beijing"); <br/> country. appendchild (city); </P> <p> city = Doc. createelement ("city"); <br/> city. appendchild (Doc. createtextnode ("Shanghai"); <br/> country. appendchild (city); </P> <p> transformerfactory TF = transformerfactory. newinstance (); <br/> transformer = TF. newtransformer (); <br/> file = new file ("E: // cities. XML "); <br/> fileoutputstream out = new fileoutputstream (File); <br/> streamresult xmlresult = new streamresult (out); <br/> transformer. transform (New domsource (DOC), xmlresult); <br/>} catch (parserconfigurationexception e) {<br/> E. printstacktrace (); <br/>} catch (transformerconfigurationexception e) {<br/> E. printstacktrace (); <br/>} catch (transformerexception e) {<br/> E. printstacktrace (); <br/>} catch (filenotfoundexception e) {<br/> E. printstacktrace (); <br/> throw E; <br/>}</P> <p> Public static void main (string ARGs []) {<br/> xmlhandler XH = new xmlhandler (); <br/> try {<br/> XH. createxml (); <br/>} catch (filenotfoundexception e) {<br/> E. printstacktrace (); <br/>}< br/>

 

The generated XML is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Country> <China> <city> Beijing </city> <city> Shanghai </city> </China> </country>

All XML content is in a row without line breaks or indentation.

Now in the code

Transformer transformer = TF. newtransformer ();

Add

Transformer. setoutputproperty (outputkeys. indent, "yes ");

 

Generate xml again

 <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <country> <br/> <China> <br/> <city> Beijing </city> <br/> <city> Shanghai </city> <br /> </China> <br/> </country>

This time there is a line break, but there is still no indentation. For this reason, I have searched Google and Baidu for many times and have not found a solution. It may be because I have or have no patience, so I tried to solve it. Add a space in front

Element. appendchild (Doc. createtextnode ("/N "));

The final code is

Package XML; </P> <p> Import Java. io. file; <br/> Import Java. io. filenotfoundexception; <br/> Import Java. io. fileoutputstream; </P> <p> Import javax. XML. parsers. documentbuilder; <br/> Import javax. XML. parsers. documentbuilderfactory; <br/> Import javax. XML. parsers. parserconfigurationexception; <br/> Import javax. XML. transform. outputkeys; <br/> Import javax. XML. transform. transformer; <br/> Import javax. XML. transform. transformerconfigurationexception; <br/> Import javax. XML. transform. transformerexception; <br/> Import javax. XML. transform. transformerfactory; <br/> Import javax. XML. transform. dom. domsource; <br/> Import javax. XML. transform. stream. streamresult; </P> <p> Import Org. w3C. dom. document; <br/> Import Org. w3C. dom. element; </P> <p> public class xmlhandler {<br/> Public void createxml () throws filenotfoundexception {<br/> document DOC = NULL; <br/> documentbuilderfactory DBF = documentbuilderfactory. newinstance (); </P> <p> try {<br/> documentbuilder docbuilder = DBF. newdocumentbuilder (); <br/> Doc = docbuilder. newdocument (); </P> <p> element Country = Doc. createelement ("country"); </P> <p> Doc. appendchild (country); </P> <p> country. appendchild (Doc. createtextnode ("/N"); </P> <p> element China = Doc. createelement ("China"); <br/> country. appendchild (China); </P> <p> China. appendchild (Doc. createtextnode ("/N"); </P> <p> element city = Doc. createelement ("city"); <br/> city. appendchild (Doc. createtextnode ("Beijing"); <br/> China. appendchild (city); </P> <p> China. appendchild (Doc. createtextnode ("/N"); </P> <p> city = Doc. createelement ("city"); <br/> city. appendchild (Doc. createtextnode ("Shanghai"); <br/> China. appendchild (city); </P> <p> China. appendchild (Doc. createtextnode ("/N"); </P> <p> transformerfactory TF = transformerfactory. newinstance (); <br/> transformer = TF. newtransformer (); <br/> file = new file ("E: // cities. XML "); <br/> fileoutputstream out = new fileoutputstream (File); <br/> streamresult xmlresult = new streamresult (out); <br/> transformer. setoutputproperty (outputkeys. indent, "yes"); <br/> transformer. transform (New domsource (DOC), xmlresult); <br/>} catch (parserconfigurationexception e) {<br/> E. printstacktrace (); <br/>} catch (transformerconfigurationexception e) {<br/> E. printstacktrace (); <br/>} catch (transformerexception e) {<br/> E. printstacktrace (); <br/>} catch (filenotfoundexception e) {<br/> E. printstacktrace (); <br/> throw E; <br/>}</P> <p> Public static void main (string ARGs []) {<br/> xmlhandler XH = new xmlhandler (); <br/> try {<br/> XH. createxml (); <br/>} catch (filenotfoundexception e) {<br/> E. printstacktrace (); <br/>}< br/>

The generated XML is

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <country> <br/> <China> <br/> <city> Beijing </city> <br/> <city> Shanghai </city> <br /> </China> <br/> </country>

 

Ii. generate xml with Sax and line feed indent

Package XML; </P> <p> Import Java. io. fileoutputstream; <br/> Import Java. io. stringwriter; </P> <p> Import javax. XML. transform. outputkeys; <br/> Import javax. XML. transform. result; <br/> Import javax. XML. transform. transformer; <br/> Import javax. XML. transform. transformerconfigurationexception; <br/> Import javax. XML. transform. sax. saxtransformerfactory; <br/> Import javax. XML. transform. sax. transformerhandler; <br/> Import javax. XML. transform. stream. streamresult; </P> <p> Import Org. XML. sax. saxexception; <br/> Import Org. XML. sax. helpers. attributesimpl; </P> <p> public class xmlhandler {</P> <p> Public String createxmlfile () {<br/> string xmlstr = NULL; <br/> try {<br/> result resultxml = new streamresult (New fileoutputstream ("E: // cities. XML "); <br/> stringwriter writerstr = new stringwriter (); <br/> saxtransformerfactory SFF = (saxtransformerfactory) saxtransformerfactory. newinstance (); <br/> transformerhandler th = SFF. newtransformerhandler (); <br/> transformer = th. gettransformer (); <br/> transformer. setoutputproperty (outputkeys. encoding, "UTF-8"); <br/> th. setresult (resultxml); <br/> th. startdocument (); <br/> attributesimpl ATTR = new attributesimpl (); <br/> th. startelement ("", "", "country", ATTR); <br/> th. startelement ("", "", "China", ATTR); </P> <p> th. startelement ("", "", "city", ATTR); <br/> string bj = "Beijing"; <br/> th. characters (BJ. tochararray (), 0, BJ. length (); <br/> th. endelement ("", "", "city"); </P> <p> th. startelement ("", "", "city", ATTR); <br/> string SH = "Shanghai"; <br/> th. characters (Sh. tochararray (), 0, Sh. length (); <br/> th. endelement ("", "", "city"); </P> <p> th. endelement ("", "", "China"); <br/> th. endelement ("", "", "country"); <br/> th. enddocument (); <br/> xmlstr = writerstr. getbuffer (). tostring (); <br/>}catch (transformerconfigurationexception e) {<br/> E. printstacktrace (); <br/>}catch (saxexception e) {<br/> E. printstacktrace (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> return xmlstr; <br/>}</P> <p> Public static void main (string ARGs []) {<br/> xmlhandler XH = new xmlhandler (); <br/> XH. createxmlfile (); <br/>}< br/>

The XML generated using Sax is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Country> <China> <city> Beijing </city> <city> Shanghai </city> </China> </country>

There are no line breaks or indentation.

Add

Transformer. setoutputproperty (outputkeys. indent, "yes ");

The generated XML has a line break but is not indented.

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <country> <br/> <China> <br/> <city> Beijing </city> <br/> <city> Shanghai </city> <br /> </China> <br/> </country> <br/>

Similarly, add a space according to the DOM method.

String four = "/N"; <br/> transformerhandler. characters (four. tochararray (), 0, four. Length ());

The final sax code is as follows:

Package XML; </P> <p> Import Java. io. fileoutputstream; <br/> Import Java. io. stringwriter; </P> <p> Import javax. XML. transform. outputkeys; <br/> Import javax. XML. transform. result; <br/> Import javax. XML. transform. transformer; <br/> Import javax. XML. transform. transformerconfigurationexception; <br/> Import javax. XML. transform. sax. saxtransformerfactory; <br/> Import javax. XML. transform. sax. transformerhandler; <br/> Import javax. XML. transform. stream. streamresult; </P> <p> Import Org. XML. sax. saxexception; <br/> Import Org. XML. sax. helpers. attributesimpl; </P> <p> public class xmlhandler {</P> <p> Public String createxmlfile () {<br/> string xmlstr = NULL; <br/> try {<br/> result resultxml = new streamresult (New fileoutputstream ("E: // cities. XML "); <br/> stringwriter writerstr = new stringwriter (); <br/> saxtransformerfactory SFF = (saxtransformerfactory) saxtransformerfactory. newinstance (); <br/> transformerhandler th = SFF. newtransformerhandler (); <br/> transformer = th. gettransformer (); <br/> transformer. setoutputproperty (outputkeys. indent, "yes"); <br/> transformer. setoutputproperty (outputkeys. encoding, "UTF-8"); <br/> th. setresult (resultxml); <br/> th. startdocument (); <br/> string four = "/N"; <br/> string eight = "/N"; <br/> attributesimpl ATTR = new attributesimpl (); </P> <p> th. startelement ("", "", "country", ATTR); </P> <p> th. characters (four. tochararray (), 0, four. length (); </P> <p> th. startelement ("", "", "China", ATTR); </P> <p> th. characters (eight. tochararray (), 0, eight. length (); </P> <p> th. startelement ("", "", "city", ATTR); <br/> string bj = "Beijing"; <br/> th. characters (BJ. tochararray (), 0, BJ. length (); <br/> th. endelement ("", "", "city"); </P> <p> th. characters (eight. tochararray (), 0, eight. length (); </P> <p> th. startelement ("", "", "city", ATTR); <br/> string SH = "Shanghai"; <br/> th. characters (Sh. tochararray (), 0, Sh. length (); <br/> th. endelement ("", "", "city"); </P> <p> th. characters (four. tochararray (), 0, four. length (); </P> <p> th. endelement ("", "", "China"); <br/> th. endelement ("", "", "country"); <br/> th. enddocument (); <br/> xmlstr = writerstr. getbuffer (). tostring (); <br/>}catch (transformerconfigurationexception e) {<br/> E. printstacktrace (); <br/>}catch (saxexception e) {<br/> E. printstacktrace (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> return xmlstr; <br/>}</P> <p> Public static void main (string ARGs []) {<br/> xmlhandler XH = new xmlhandler (); <br/> XH. createxmlfile (); <br/>}< br/>

Generated XML

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <country> <br/> <China> <br/> <city> Beijing </city> <br/> <city> Shanghai </city> <br /> </China> <br/> </country>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.