Sam pafe
Sam pafe

Reputation: 11

create an exact copy of an XML file that is read with SAX?

In Java, is it possible to create an exact copy of an XML file that is read with SAX? Essentially using the methods startElement, characters, endElement to read the original file. While I appreciate all willingness to help, please do not deviate from this original question or answer by suggesting a different approach for reading the file; as for example I am not interested in DOM. Thanks!

Upvotes: 1

Views: 1861

Answers (2)

alexbrn
alexbrn

Reputation: 2155

You could use David Megginson's public domain XMLWriter (a SAX filter). It's available here, and the download has source code and examples.

(N.B. the output will be an exact logical match to the input, creating an exact lexical match is in the general case not possible as conformant XML processors lose some lexical features of the input e.g. whitespace within tags).

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500983

No, I don't believe SAX will necessarily allow you to create an eaxct copy of the file.

For example, I don't believe you'd be able to tell the difference between:

<foo>3 > 1</foo>

and

<foo>3 &gt; 1</foo>

In both cases, I believe you'd be given a string containing the characters "3 > 1" without any indication of whether the > was escaped.

You could probably create an XML-equivalent copy of the original file, but that isn't what your question asked, so I won't go into details.

Upvotes: 3

Related Questions