XML解析器java跳过dtd验证

时间:2014-03-11 19:03:34

标签: java xml-parsing dtd

我想跳过DTD验证,因为我没有互联网连接。是否可以在java中实现。

我的代码是:

  file = new File(filepath);  

            // Create instance of DocumentBuilderFactory  
            factory = DocumentBuilderFactory.newInstance();  

            // Get the DocumentBuilder  
            docBuilder = factory.newDocumentBuilder();  

            // Using existing XML Document  

            doc = docBuilder.parse(file);  

我的错误就是这个,我知道这个错误的原因。

a.net.UnknownHostException: www.jboss.org  
        at java.net.PlainSocketImpl.connect(Unknown Source)  
        at java.net.Socket.connect(Unknown Source)  
        at java.net.Socket.connect(Unknown Source)  
        at sun.net.NetworkClient.doConnect(Unknown Source)  
       at sun.net.www.http.HttpClient.openServer(Unknown Source)  
        at sun.net.www.http.HttpClient.openServer(Unknown Source)  
        at sun.net.www.http.HttpClient.<init>(Unknown Source)  
        at sun.net.www.http.HttpClient.New(Unknown Source)  
        at sun.net.www.http.HttpClient.New(Unknown Source)  
        at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(UnknownSource)  
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)  
        at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)  
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)  
        at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)  
        at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(Unknown Source)  
        at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startDTDEntity(Unknown Source)  
        at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)  
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)  
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)  

1 个答案:

答案 0 :(得分:0)

一个令人愤慨的黑客就是编写一个自定义java.io.InputStream扩展,它只是在解析器看到它之前删除了DTD引用。