[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 634
  • Last Modified:

How to validate created xml file with external dtd file using Xerces

Dear Experts

Im writing application which generates xml files (using Xerces). The document is generated correctly (and written to file too). But I want to validate generated file with external dtd. And I have no Idea how to use document/writer properties to force Xerces to return errror if dtd validation fails. (I create xml using DOMDocument and write it to file with DOMWriter).

Part of code which writes xml doc into file:

DOMWriter* pDomWriter = NULL;
XMLFormatTarget* pFormatTarget = NULL;
pDomWriter = ((DOMImplementationLS*)m_refcXmlDocument.m_pDomImpl)->createDOMWriter();
if(pDomWriter->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true) == true)
   pDomWriter->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);

pFormatTarget = new LocalFileFormatTarget(m_sFileName);

CDMTXmlErrorReporter cErrorHandler;
pDomWriter->writeNode(pFormatTarget, *m_refcXmlDocument.m_pDomDoc);

Thanks for all help
  • 2
  • 2
1 Solution
You must refer to the DTD location from your document.  See:

External DTD

Then, you have to turn on validation:

How do I turn on validation?
tmprogAuthor Commented:
thanks for answer.

I add info about document type  to xml file (<!DOCTYPE msig SYSTEM "msig.dtd">)
I use following methods:

DOMDocumentType* pDomDocType = m_pDomImpl->createDocumentType(X(m_sDocumentName), NULL, X(m_sDocumentType));
DOMDocument* m_pDomDoc = m_pDomImpl->createDocument(0, X(m_sDocumentName), pDomDocType);

And document is generated, even if it has different structure than dtd.
Do I need to create parser after document is generated (and before saving document to file)? Or is and method to validate it on document level?

Sorry I wasn't clear - the validation takes place when you parse with a validating parser (either SAX, DOM, or StAX).  So, the parser will tell you when an invalid token is encountered.  You then write logic to do whatever you want to do in that case.
tmprogAuthor Commented:
OK, I solved the problem. thanks for help anyway :).

Featured Post

Independent Software Vendors: 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!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now