Я смотрю на два атрибута XML - XMLConstants.ACCESS_EXTERNAL_DTD
и XMLConstants.ACCESS_EXTERNAL_SCHEMA
.
Я могу подтвердить это при загрузке external_dtd. xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "http://www.example.com/test.dtd">
<root>
</root>
unit test завершается неудачно, как ожидалось
@Test
public void testExternalDTD() throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
documentBuilderFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
documentBuilder.parse(SchemaTest.class.getResourceAsStream("external_dtd.xml"));
// [Fatal Error] :2:57: External DTD: Failed to read external DTD 'test.dtd', because 'http' access is not allowed due to restriction set by the accessExternalDTD property.
}
Однако я не уверен, на каком этапе XMLConstants.ACCESS_EXTERNAL_SCHEMA используется
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.example.com/Report" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/Report http://maven.apache.org/maven-v4_0_0.xsd">
</root>
как загрузка external_dtd. xml успешно.
@Test
public void testExternalSchema() throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
documentBuilderFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
documentBuilder.parse(SchemaTest.class.getResourceAsStream("external_schema.xml"));
}
Как использовать XMLConstants.ACCESS_EXTERNAL_SCHEMA?