简体   繁体   English

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

[英]Appending data to an XML file using OutputStreamWriter (StAX parser)

I want to append data to an XML file using OutputStreamWriter but ending up something like below 我想使用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>

How can I achieve the correct format 我怎样才能达到正确的格式

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

I have tried with below code 我试过下面的代码

 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();

Please give me suggestion. 请给我一些建议。 I'm new to StAX. 我是StAX的新手。

Your example is only appending a new xml document to the end of a file. 您的示例仅将新的xml文档附加到文件末尾。 If you want to append data to an existing xml doc you have to read the source xml, and write it to another file, inserting your new element as need.... see http://docs.oracle.com/javase/tutorial/jaxp/stax/example.html#bnbgq for an example using stax api. 如果要将数据附加到现有的xml doc,则必须读取源xml,并将其写入另一个文件,根据需要插入新元素....请参阅http://docs.oracle.com/javase/tutorial /jaxp/stax/example.html#bnbgq使用stax api的示例。

Use below code you get the output. 使用下面的代码获得输出。

XMLOutputFactory factory = XMLOutputFactory.newInstance(); 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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM