使用OutputStreamWriter(StAX解析器)将数据附加到XML文件

时间:2016-03-01 06:19:10

标签: java xml stax

我想使用OutputStreamWriter将数据附加到XML文件,但结果如下所示

<?xml version="1.0" encoding="utf-8"?>
<doc>
 <msg>
   <tag1>data1</tag1>
   ...
 </msg>
</doc>
<?xml version="1.0" encoding="utf-8"?>
<doc>
 <msg>
   <tag1>data2</tag1>
   ...
 </msg>
</doc>

如何实现正确的格式

<?xml version="1.0" encoding="utf-8"?>
<doc>
 <msg>
   <tag1>data1</tag1>
   ...
 </msg>
 <msg>
   <tag1>data2</tag1>
   ...
 </msg>
</doc>

我尝试使用以下代码

 XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    File file = new File(outputFile);
    Writer fw = null;
    XMLStreamWriter w = null;

    try {
        if(!file.isFile()) {
            file.createNewFile();
        }
        fw = new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8"); 
        w = outputFactory.createXMLStreamWriter(fw);
        w.writeStartDocument("utf-8", "1.0");
        w.writeStartElement("doc");

        createMessageElement(fingerPrint, w, data);//method to write data

        w.writeEndElement();
        w.writeEndDocument();

        w.flush();
        w.close();

        w.close();

        fw.flush();
        fw.close();

请给我一些建议。我是StAX的新手。

2 个答案:

答案 0 :(得分:1)

您的示例只是将新的xml文档附加到文件末尾。如果要将数据附加到现有的xml doc,则必须读取源xml,并将其写入另一个文件,根据需要插入新元素....有关使用stax api的示例,请参阅http://docs.oracle.com/javase/tutorial/jaxp/stax/example.html#bnbgq。 / p>

答案 1 :(得分:0)

使用下面的代码获得输出。

XMLOutputFactory factory = XMLOutputFactory.newInstance();

    try {

        XMLStreamWriter writer1 = factory.createXMLStreamWriter(new FileWriter("E:\\sampleXML.xml"));

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        XMLStreamWriter2 xtw = (XMLStreamWriter2) new WstxOutputFactory()
                .createXMLStreamWriter(byteArrayOutputStream, "UTF-8");

        xtw.writeStartDocument("UTF-8", "1.1");

        xtw.writeStartElement("doc");

        XMLStreamWriter2 writer2 = createMessageElement(xtw);

        writer2.writeStartElement("msg");
        writer2.writeStartElement("tag1");
        writer2.writeCharacters("data1");
        writer2.writeEndElement();
        writer2.writeStartElement("tag2");
        writer2.writeCharacters("data2");
        writer2.writeEndElement();


        writer2.writeEndDocument();
        writer2.close();
        xtw.flush();
        xtw.close();

        System.out.println("XML :" + new String(byteArrayOutputStream.toByteArray()));

    } catch (XMLStreamException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

private static XMLStreamWriter2 createMessageElement(XMLStreamWriter2 writer2)
        throws XMLStreamException, IOException {
        try {
        writer2.writeStartElement("msg");
        writer2.writeStartElement("tag1");
        writer2.writeCharacters("data1");
        writer2.writeEndElement();
        writer2.writeStartElement("tag2");
        writer2.writeCharacters("data2");
        writer2.writeEndElement();

        writer2.writeEndElement();

        writer2.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return writer2;

}