I'm trying to save a tree (extends JTree
) which holds an XML
document to a DOM Object
having changed it's structure.
I have created a new document object, traversed the tree to retrieve the contents successfully (including the original encoding of the XML
document), and now have a ByteArrayInputStream
which has the tree contents (XML
document) with the correct encoding.
The problem is when I parse the ByteArrayInputStream
the encoding is changed to UTF-8
(in the XML
document) automatically.
Is there a way to prevent this and use the correct encoding as provided in the ByteArrayInputStream
.
It's also worth adding that I have already used the
transformer.setOutputProperty(OutputKeys.ENCODING, encoding)
method to retrieve the right encoding.
Any help would be appreciated.
Here's an updated answer since OutputFormat is deprecated :
The second part will return the XML Document as String
This worked for me and is very simple. No need for a transformer or output formatter:
I solved it, given alot of trial and errors.
I was using
but changed it to
and this solved my problem.
encoding
is what I set it to betrue
refers to whether or not indent is set.Note to self - read more carefully - I had looked at the javadoc hours ago - if only I'd have read more carefully.