StAX XML formatting in Java

前端 未结 10 1401
-上瘾入骨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:41

    if you are using XMLEventWriter, then an easier way to do that is:

    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
            XMLEventWriter writer = outputFactory.createXMLEventWriter(w);
            XMLEventFactory eventFactory = XMLEventFactory.newInstance();
            Characters newLine = eventFactory.createCharacters("\n"); 
            writer.add(startRoot);
            writer.add(newLine);
    
    0 讨论(0)
  • 2020-11-29 08:47

    Via the JDK: transformer.setOutputProperty(OutputKeys.INDENT, "yes");.

    0 讨论(0)
  • 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
    

    0 讨论(0)
  • 2020-11-29 08:50

    With Spring Batch this requires a subclass since this JIRA BATCH-1867

    public class IndentingStaxEventItemWriter<T> extends StaxEventItemWriter<T> {
    
      @Setter
      @Getter
      private boolean indenting = true;
    
      @Override
      protected XMLEventWriter createXmlEventWriter( XMLOutputFactory outputFactory, Writer writer) throws XMLStreamException {
        if ( isIndenting() ) {
          return new IndentingXMLEventWriter( super.createXmlEventWriter( outputFactory, writer ) );
        }
        else {
          return super.createXmlEventWriter( outputFactory, writer );
        }
      }
    
    }
    

    But this requires an additionnal dependency because Spring Batch does not include the code to indent the StAX output:

    <dependency>
      <groupId>net.java.dev.stax-utils</groupId>
      <artifactId>stax-utils</artifactId>
      <version>20070216</version>
    </dependency>
    
    0 讨论(0)
  • 2020-11-29 08:51

    If you're using the StAX cursor API, you can indent the output by wrapping the XMLStreamWriter in an indenting proxy. I tried this in my own project and it worked nicely.

    0 讨论(0)
  • 2020-11-29 08:55

    Rather than relying on a com.sun...class that might go away (or get renamed com.oracle...class), I recommend downloading the StAX utility classes from java.net. This package contains a IndentingXMLStreamWriter class that works nicely. (Source and javadoc are included in the download.)

    0 讨论(0)
提交回复
热议问题