tags:

views:

18

answers:

1

I am reading an xml document, inserting an element and then writing it. In this process, the document loses doctype and dtd info. Through "Transformer" class I can set OutputProperties but my requirement is to insert or update some element and not to play with doctype at all. Could somebody give me a hint?

A: 

From a post at Example Depot:

By default, the DOCTYPE is not written when using a transformer to dump a DOM document to an XML file. This example demonstrates how to write a DOCTYPE with a public and system id. Unfortunately, it is not possible to write a DOCTYPE with an internal DTD.

The referenced article contains this example.

Here is another code snippet from someone with the same problem, which, unfortunately is not complete (and the forum is in german language, but the formum text is irrelevant here) but maybe you can complete the missing steps.

EDIT

A real ugly hack/workaround - but maybe the only solution to your problem:

  1. Copy the full doctype declaration from the input document to a String
  2. Add a 'marker comment' (like <!-- DOCTYPE -->) to the DOM at the very beginning of the document
  3. serialize the document to another String
  4. find and replace the 'marker comment' with the saved original doctype declaration
Andreas_D
Thank you Andreas for your help. I needed to write only public and system ids, not internal DTD. So referenced example well solved my problem
Leslie Norman