StAX XML formatting in Java

前端 未结 10 1409
-上瘾入骨i
-上瘾入骨i 2020-11-29 08:06

Is it possible using StAX (specifically woodstox) to format the output xml with newlines and tabs, i.e. in the form:


  
   someData
  &         


        
10条回答
  •  遥遥无期
    2020-11-29 08:48

    How about StaxMate:

    http://www.cowtowncoder.com/blog/archives/2006/09/entry_21.html

    Works well with Woodstox, fast, low-memory usage (no in-memory tree built), and indents like so:


    SMOutputFactory sf = new SMOutputFactory(XMLOutputFactory.newInstance());
    SMOutputDocument doc = sf.createOutputDocument(new FileOutputStream("output.xml"));
    doc.setIndentation("\n ", 1, 2); // for unix linefeed, 2 spaces per level    
    // write doc like:    
    SMOutputElement root = doc.addElement("element1");    
    root.addElement("element2").addCharacters("someData");    
    doc.closeRoot(); // important, flushes, closes output
    

提交回复
热议问题