簡體   English   中英

使用OutputStreamWriter(StAX解析器)將數據附加到XML文件

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

我想使用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的新手。

您的示例僅將新的xml文檔附加到文件末尾。 如果要將數據附加到現有的xml doc,則必須讀取源xml,並將其寫入另一個文件,根據需要插入新元素....請參閱http://docs.oracle.com/javase/tutorial /jaxp/stax/example.html#bnbgq使用stax api的示例。

使用下面的代碼獲得輸出。

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