Parese XML without DTD validation

Posted on by Kim

Use the Saxparser...

import org.w3c.dom.Document;

JDom

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()));
else
return null;
}
});

return new org.jdom.output.DOMOutputter().output(parser.build(is));
}

public static final boolean validateUrl(String aURL) {
try {
java.net.URL 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);
u.openStream();
return true;
}
} catch (Exception e) {
}
return false;
}

0 Responses to "Parese XML without DTD validation":