2014-11-01 29 views
0

上午使用StaX XMLEventReaderXMLEventWriter。 我需要修改保存在字節數組中的原始xml文件的臨時拷貝。如果我這樣做(調試,正在寫入文件):如何克隆xml文件(完全相同的副本)

public boolean isCrcCorrect(Path path) throws IOException, XPathExpressionException { 
    ByteArrayOutputStream output = new ByteArrayOutputStream(); 
    XMLEventFactory eventFactory = XMLEventFactory.newInstance(); 
    XMLEventReader reader = null; 
    XMLEventWriter writer = null; 

    StreamResult result; 
    String tagContent; 

    if (!fileData.currentFilePath.equals(path.toString())) { 
     parseFile(path); 
    } 

    try { 
     System.out.println(path.toString()); 
     reader = XMLInputFactory.newInstance().createXMLEventReader(new FileReader(path.toString())); 
     //writer = XMLOutputFactory.newInstance().createXMLEventWriter(output); 
     writer = XMLOutputFactory.newInstance().createXMLEventWriter(new FileWriter("f:\\Projects\\iqpdct\\iqpdct-domain\\src\\main\\java\\de\\iq2dev\\domain\\util\\debug.xml")); 

     writer.add(reader); 
     writer.close(); 
    } catch(XMLStreamException strEx) { 
     System.out.println(strEx.getMessage()); 
    } 

    crc.reset(); 
    crc.update(output.toByteArray()); 
    System.out.println(crc.getValue()); 
    //return fileData.file_crc == crc.getValue(); 
    return false; 
} 

克隆從產地不同 來源:

<VendorText textId="T_VendorText" /> 

克隆:

<VendorText textId="T_VendorText"></VendorText> 

他爲什麼把結束標籤? Source中沒有任何一個。

回答

2

如果您想要恰好是XML文檔的字節流的精確副本,則必須將其複製爲字節流。您不能通過爲XML解析器提供後端來複制它,因爲解析器前端的目標是,以便將您的代碼與可以變化但語義上相同的功能隔離。例如,在你的情況下,這兩種方法表示一個空元素。

+0

非常感謝,這爲我工作。 – Constantine 2014-11-01 09:59:28