• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 646
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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