开发者

How can I retain doctype info in xml documnet during read/write process?

开发者 https://www.devze.com 2023-01-12 05:21 出处:网络
I am reading an xml document, inserting an element and then wri开发者_Python百科ting it. In this process, the document loses doctype and dtd info. Through \"Transformer\" class I can set OutputPropert

I am reading an xml document, inserting an element and then wri开发者_Python百科ting 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?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消