Parese XML without DTD validation

Posted on by Kim

Use the Saxparser...

import org.w3c.dom.Document;


private static Document getDocumentFromInputStreamTest(InputStream is, boolean validate) throws JDOMException, IOException {
SAXBuilder parser = new SAXBuilder();
parser.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) {
if (!validateUrl(systemId))
return new InputSource(new ByteArrayInputStream("".getBytes()));
return null;

return new org.jdom.output.DOMOutputter().output(;

public static final boolean validateUrl(String aURL) {
try { url = new URL(aURL);
if ("file".equals(url.getProtocol())) {
File f = new File(url.getFile());
if (f.isFile() && f.canRead())
return true;
} else if ("http".equals(url.getProtocol())) {
URL u = new URL(aURL);
return true;
} catch (Exception e) {
return false;

0 Responses to "Parese XML without DTD validation":