我尝试使用JAXB(javax.xml.bind.JAXB;
)取消对文件XML文件(test.xml)的管理
然而它总是给我这个错误:
[org.xml.sax.SAXParseException; systemId: file:/C:/Users/EXAMPLE/AppData/Local/Eclipse/workspace_4.4.0/EXAMPLE/test.xml; lineNumber: 2; columnNumber: 42; Externe DTD: Lesen von externer DTD "example.dtd" nicht erfolgreich, da "file"-Zugriff wegen der von der Eigenschaft "accessExternalDTD" festgelegten Einschränkung nicht zulässig ist.]
英文翻译:
Reading from external DTD "example.dtd" not succesfull , couse "File"-Access is not allowed by the Restriction set by the Proppertie "accessExternalDTD"
已经尝试过解决方案:
accessExternalDTD
proppertie。注意事项:
<!DOCTYPE EXAMPLE SYSTEM
"example.dtd">
答案 0 :(得分:5)
可以使用系统属性accessExternalDTD
控制javax.xml.accessExternalDTD
属性,因此请使用-Djavax.xml.accessExternalDTD=true
启动您的程序,它应该可以正常运行。也应该可以在unmarshaller上设置属性,试试这个:
unmarshaller.setProperty(javax.xml.XMLConstants.ACCESS_EXTERNAL_DTD, Boolean.TRUE);
答案 1 :(得分:3)
import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Customer.class);
XMLInputFactory xif = XMLInputFactory.newFactory();
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource("input.xml"));
Unmarshaller unmarshaller = jc.createUnmarshaller();
Customer customer = (Customer) unmarshaller.unmarshal(xsr);
}
}