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

Posted on 2007-07-25
Last Modified: 2010-05-18
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
Question by:tmprog
    LVL 12

    Accepted 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?

    Author Comment

    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?

    LVL 12

    Expert Comment

    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.

    Author Comment

    OK, I solved the problem. thanks for help anyway :).

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now