Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

731 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