禁用DTD验证

时间:2016-05-09 11:23:46

标签: java xml-dtd

我使用下面的代码来解析xml并使用xsd文件进行验证。 Xml文件还包含对.dtd的引用,这里与此无关 如何禁用DTD处理?

DocumentBuilderFactory factory =  DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);     
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",  "http://www.w3.org/2001/XMLSchema" );
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", XmlSchemaUrl);

DocumentBuilder builder =factory.newDocumentBuilder();

Validator handler=new Validator();
builder.setErrorHandler(handler);

InputSource is =  new InputSource(new StringReader(xmlDoc));
Document doc = builder.parse(is);
if(handler.validationError==true)
{
    throw handler.saxParseException;
}
else
    return true;

下面是我试图解析的XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
<!DOCTYPE document SYSTEM "subjects.dtd">             
<Request>
    <Header>   
        <UserName>test</UserName>
        <ApplicationName>MyAccount</ApplicationName>
    </Header>
    <GetCustomerDetails>
        <AccountInternalId>12345678</AccountInternalId>
    </GetCustomerDetails>
</Request>

0 个答案:

没有答案