Java – How to generate an *exact* copy of an XML document with resolved entities

javaxmlxml-parsing

Given an XML document like this:

 <!DOCTYPE doc SYSTEM 'http://www.blabla.com/mydoc.dtd'>
 <author>john</author>
 <doc>
   <title>&title;</title>
 </doc>  

I wanted to parse the above XML document and generate a copy of it with all of its entities already resolved. So given the above XMl document, the parser should output:

 <!DOCTYPE doc SYSTEM 'http://www.blabla.com/mydoc.dtd'>
 <author>john</author>
 <doc>
   <title>Stack Overflow Madness</title>
 </doc>  

I know that you could implement an org.xml.sax.EntityResolver to resolve entities, but what I don't know is how to properly generate a copy of the XML document with everything still intact (except its entities). By everything, I mean the whitespaces, the dtd at the top of the document, the comments, and any other things except the entities that should have been resolved previously. If this is not possible, please suggest a way that at least can preserve most of the things (e.g. all but no comments).

Note also that I am restricted to the pure Java API provided by Sun, so no third party libraries can be used here.

Thanks very much!

EDIT: The above XML document is a much simplified version of its original document. The original one involves a very complex entity resolution using EntityResolver whose significance I have greatly reduced in this question. What I am really interested is how to produce an exact copy of the XML document with an XML parser that uses EntityResolver to resolve the entities.

Best Answer

Is it possible for you to read in the xml template as a string? And with the string do something like

string s = "<title>&title;</title>";
s = s.replace("&title;", "Stack Overflow Madness");
SaveXml(s);