Constantine
Constantine

Reputation: 2032

How to clone xml file (totaly identical copy)

Am using StaX XMLEventReader and XMLEventWriter. I need to make modified temporal copy of original xml file saved in byte array. If I do so (for debug, am writing to file):

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;
}

clone differs from origin Source:

<VendorText textId="T_VendorText" />

Clone:

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

Why he is putting the end tag? There is no either in Source.

Upvotes: 0

Views: 585

Answers (1)

Raedwald
Raedwald

Reputation: 48682

If you want a precise copy of a byte stream that happens to be an XML document, you must copy it as a byte stream. You can't copy it by providing a back-end to an XML parser because the purpose of the parser front-end to to isolate your code from features that can vary but which are semantically equivalent. Such as, in your case, the two means for indicating an empty element.

Upvotes: 2

Related Questions