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

x
  • 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();
pDomWriter->setEncoding(X(m_refcXmlDocument.m_sEncoding));
if(pDomWriter->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true) == true)
{
   pDomWriter->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
}

pFormatTarget = new LocalFileFormatTarget(m_sFileName);

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

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

External DTD
http://www.xmlfiles.com/dtd/dtd_intro.asp

Then, you have to turn on validation:

How do I turn on validation?
http://xerces.apache.org/xerces-j/faq-general.html#faq-7
0
 
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?

tmprog
0
 
marklorenzCommented:
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.
0
 
tmprogAuthor Commented:
OK, I solved the problem. thanks for help anyway :).
0

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