how to disable dtd at runtime in java's xpath?

后端 未结 3 1702
悲&欢浪女
悲&欢浪女 2020-12-01 16:30

I got dtd in file and I cant remove it. When i try to parse it in Java I get \"Caused by: java.net.SocketException: Network is unreachable: connect\", because its remote dtd

3条回答
  •  清歌不尽
    2020-12-01 17:03

    You should be able to specify your own EntityResolver, or use specific features of your parser? See here for some approaches.

    A more complete example:

    
    
    
        Value
    
    

    And xpath usage:

    import java.io.File;
    import java.io.IOException;
    import java.io.StringReader;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathFactory;
    
    import org.w3c.dom.Document;
    import org.xml.sax.EntityResolver;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    
    public class Main {
    
        public static void main(String[] args) throws Exception {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
    
            builder.setEntityResolver(new EntityResolver() {
    
                @Override
                public InputSource resolveEntity(String publicId, String systemId)
                        throws SAXException, IOException {
                    System.out.println("Ignoring " + publicId + ", " + systemId);
                    return new InputSource(new StringReader(""));
                }
            });
            Document document = builder.parse(new File("src/foo.xml"));
            XPathFactory xpathFactory = XPathFactory.newInstance();
            XPath xpath = xpathFactory.newXPath();
            String content = xpath.evaluate("/foo/bar/text()", document
                    .getDocumentElement());
            System.out.println(content);
        }
    }
    

    Hope this helps...

提交回复
热议问题