Assuming the top element of the document is allows validation of the document(s) against a given DTD.
Libxml2 exports an API to handle DTDs and validation, check the associated description. So there may be a number of examples on-line, I will just list one for now, others pointers welcome: I suggest looking at the examples found under test/valid/dtd and any of the large number of books available on XML.
When validating documents with this method there are two issues I don't like about it.
First, it creates a bunch of warnings, which one would not expect, as the plot of calling this method is preventing any warnings that could occur when erroneously relying on the document's validity.
Stylus Studio® 5.1 fully supports XML document validation based on DTD (in addition to full support for W3C XML Schema) to ensure compatibility with legacy XML systems. To associate any open XML file with an externally defined DTD, click XML Associate XML with Schema from the Stylus Studio® menu. Validating an XML document against its associated DTD is easy simply click the Validate Document button (the tree diagram with the green checkmark) in Stylus Studio®'s XML Editor, then select the DTD validating parser you want to use.
Real-time XML syntax checking verifies that your XML code's syntax is well formed and checks it's validity (against its DTD) as you type.
Sense: X highlights XML syntax errors in real-time, and its auto-completion mechanism displays contextually valid prompts to help speed coding.
Second, it only returns a boolean with no chance of getting additional details about the reasons for rendering invalid.
That's the reason for me to use a little wrapper, which I post here in case anyone finds it useful.