Solved

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

Posted on 2011-03-22
1
496 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

705 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