Solved

Java XML updating xml doc with embedded dtd - dtd is lost when xml is updated

Posted on 2011-03-22
1
469 Views
Last Modified: 2012-06-27
I have an XML document that has an embedded DTD.  I have a method that will update the value of an element passed and save the doc. When the doc is saved the element is updated as expected, however the embedded DTD is wiped out.  How do I preserve the DTD?

Node resultNode = findSubNode(elementName, mDoc.getDocumentElement());
            if (resultNode != null) {
                  resultNode.setTextContent(value);
                  Transformer xformer;
                  try {
                        xformer = TransformerFactory.newInstance().newTransformer();
                        xformer.transform(new DOMSource(mDoc), new StreamResult(new File(mXmlPath)));
                  } catch (Exception e) {
                        XmlError = e.getMessage();
                        return false;
                  }
                  return true;
            } else
                  return false;
      }
0
Comment
Question by:JonMny
1 Comment
 
LVL 5

Accepted Solution

by:
aciuica earned 500 total points
ID: 35193572
You can set to the transformer doctype properties from document.
DocumentType docType = mDoc.getDoctype();
if (docType.getPublicId() != null) {
    xformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, docType.getPublicId());
}
if (docType.getSystemId() != null) {
    xformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, docType.getSystemId());
}

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now