Formatting XML file using StAX

后端 未结 3 482
闹比i
闹比i 2020-12-10 08:51

I am using StAX XML stream writer to write the XML file. It writes all the data in a single line. I want all the tags to be indented instead of a single line.

3条回答
  •  暖寄归人
    2020-12-10 09:18

    Answered here: StAX XML formatting in Java

    EDIT: A quick example (without resource cleaning) using stax-utils (https://stax-utils.dev.java.net/):

    XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
    FileOutputStream file = new FileOutputStream("d:/file.xml");
    XMLEventWriter writer = xmlOutputFactory.createXMLEventWriter(file);
    writer = new IndentingXMLEventWriter(writer);
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    writer.add(eventFactory.createStartDocument());
    writer.add(eventFactory.createStartElement("", "", "a"));
    writer.add(eventFactory.createStartElement("", "", "b"));
    writer.add(eventFactory.createEndElement("", "", "b"));
    writer.add(eventFactory.createEndElement("", "", "a"));
    writer.add(eventFactory.createEndDocument());
    

    This gives you:

    
    
      
    
    

提交回复
热议问题