Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One way to do this would be to read the document and then validate it with the transformer:</p> <pre><code>import java.net.URL; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; public class ValidateWithExternalDTD { private static final String URL = "http://127.0.0.1:8080/sample/subject.xml"; private static final String DTD = "http://127.0.0.1/YourDTD.dtd"; public static void main(String args[]) { try { DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); factory.setValidating(true); DocumentBuilder builder = factory.newDocumentBuilder(); // Set the error handler builder.setErrorHandler(new org.xml.sax.ErrorHandler() { public void fatalError(SAXParseException spex) throws SAXException { // output error and exit spex.printStackTrace(); System.exit(0); } public void error(SAXParseException spex) throws SAXParseException { // output error and continue spex.printStackTrace(); } public void warning(SAXParseException spex) throws SAXParseException { // output warning and continue spex.printStackTrace(); } }); // Read the document URL url = new URL(ValidateWithExternalDTD.URL); Document xmlDocument = builder.parse(url.openStream()); DOMSource source = new DOMSource(xmlDocument); // Use the tranformer to validate the document StreamResult result = new StreamResult(System.out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, ValidateWithExternalDTD.DTD); transformer.transform(source, result); // Process your document if everything is OK } catch (Exception ex) { ex.printStackTrace(); } } } </code></pre> <p>Another way would be to replace the XML title with the XML title plus the DTD reference</p> <p>Replace this:</p> <pre><code>&lt;?xml version = "1.0"?&gt; </code></pre> <p>with this:</p> <pre><code>&lt;?xml version = "1.0"?&gt;&lt;!DOCTYPE ...&gt; </code></pre> <p>Of course you would replace the first occurance only and not try to go through the whole xml document</p> <p>You have to instantiate the SAXBuilder by passing true(validate) to its constructor:</p> <pre><code>SAXBuilder builder = new SAXBuilder(true); </code></pre> <p>or call:</p> <pre><code>builder.setValidation(true) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload