Solved

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

Posted on 2011-03-22
1
455 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

760 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

18 Experts available now in Live!

Get 1:1 Help Now