SAX解析器。如何阻止SAX解析器连接到Internet?

时间:2015-02-11 17:43:22

标签: java xml parsing sax dtd

我正在尝试将11384个XML文件解析为一个SQLite数据库。其中之一:

<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2009/2010/2011 Ulrich Apel.
This work is distributed under the conditions of the Creative Commons
Attribution-Share Alike 3.0 Licence. This means you are free:
* to Share - to copy, distribute and transmit the work
* to Remix - to adapt the work

Under the following conditions:
* Attribution. You must attribute the work by stating your use of KanjiVG in
  your own copyright header and linking to KanjiVG's website
  (http://kanjivg.tagaini.net)
* Share Alike. If you alter, transform, or build upon this work, you may
  distribute the resulting work only under the same or similar license to this
  one.

See http://creativecommons.org/licenses/by-sa/3.0/ for more details.
-->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" [
<!ATTLIST g
xmlns:kvg CDATA #FIXED "http://kanjivg.tagaini.net"
kvg:element CDATA #IMPLIED
kvg:variant CDATA #IMPLIED
kvg:partial CDATA #IMPLIED
kvg:original CDATA #IMPLIED
kvg:part CDATA #IMPLIED
kvg:number CDATA #IMPLIED
kvg:tradForm CDATA #IMPLIED
kvg:radicalForm CDATA #IMPLIED
kvg:position CDATA #IMPLIED
kvg:radical CDATA #IMPLIED
kvg:phon CDATA #IMPLIED >
<!ATTLIST path
xmlns:kvg CDATA #FIXED "http://kanjivg.tagaini.net"
kvg:type CDATA #IMPLIED >
]>
<svg xmlns="http://www.w3.org/2000/svg" width="109" height="109" viewBox="0 0 109 109">
<g id="kvg:StrokePaths_0ff01" style="fill:none;stroke:#000000;stroke-width:3;stroke-linecap:round;stroke-linejoin:round;">
<g id="kvg:0ff01">
    <path id="kvg:0ff01-s1" d="M54.5,15.79c0,6.07-0.29,55.49-0.29,60.55"/>
    <path id="kvg:0ff01-s2" d="M54.5,88 c -0.83,0 -1.5,0.67 -1.5,1.5 0,0.83 0.67,1.5 1.5,1.5 0.83,0 1.5,-0.67 1.5,-1.5 0,-0.83 -0.67,-1.5 -1.5,-1.5"/>
</g>
</g>
<g id="kvg:StrokeNumbers_0ff01" style="font-size:8;fill:#808080">
    <text transform="matrix(1 0 0 1 45 16)">1</text>
    <text transform="matrix(1 0 0 1 45 88)">2</text>
</g>
</svg>

我正在使用SAX解析器:

public class SaxKanjivgHandler extends DefaultHandler {
.....
        File folder = new File(KANJIVG_DIRECTORY);
        if (folder.isDirectory()) {
            File[] listOfFiles = folder.listFiles();

            for (File file : listOfFiles) {
                if (file.isFile()) {
                    currentFileName = file.getName();
                    readXmlFromFile(file);
                }
            }
        }
.....
    public void readXmlFromFile(File file) throws ParserConfigurationException,
            SAXException, IOException {

        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        parser.parse(file, this);

    }

当我解析文件时,我收到此错误:

  

线程中的异常&#34; main&#34; java.net.SocketException:连接重置     在java.net.SocketInputStream.read(未知来源)at   java.net.SocketInputStream.read(未知来源)at   java.io.BufferedInputStream.fill(未知来源)at   java.io.BufferedInputStream.read1(未知来源)at   java.io.BufferedInputStream.read(未知来源)at   sun.net.www.MeteredStream.read(未知来源)at   java.io.FilterInputStream.read(未知来源)at   sun.net.www.protocol.http.HttpURLConnection $ HttpInputStream.read(未知   来源)at   com.sun.org.apache.xerces.internal.impl.XMLEntityManager $ RewindableInputStream.read(未知   来源)at   com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(未知   来源)at   com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(未知   来源)at   com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipSpaces(未知   来源)at   com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.scanEntityDecl(未知   来源)at   com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.scanDecls(未知   来源)at   com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.scanDTDExternalSubset(未知   来源)at   com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl $ DTDDriver.dispatch(未知   来源)at   com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl $ DTDDriver.next(未知   来源)at   com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl $ PrologDriver.next(未知   来源)at   com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(未知   来源)at   com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(未知   来源)at   com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(未知   来源)at   com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(未知   来源)at   com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(未知   来源)at   com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(未知   来源)at   com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl $ JAXPSAXParser.parse(未知   来源)at   com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(未知   来自)javax.xml.parsers.SAXParser.parse(未知来源)at   SaxKanjivgHandler.readXmlFromFile(SaxKanjivgHandler.java:63)at   SaxKanjivgHandler。(SaxKanjivgHandler.java:44)at   Main.main(Main.java:28)

首先,我认为这个错误是因为一个确切的文件。但是不同时间的不同文件会发生错误。如何让SAX解析器停止连接到Internet?

1 个答案:

答案 0 :(得分:1)

您可以提供自己的EntityResolver

public class DummyEntityResolver implements EntityResolver {
    public InputSource resolveEntity(String publicID, String systemID)
        throws SAXException {

        return new InputSource(new StringReader(""));
    }
}

public void readXmlFromFile(File file) throws ParserConfigurationException,
        SAXException, IOException {

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = factory.newSAXParser();
    parser.getXMLReader().setEntityResolver(new DummyEntityResolver());
    parser.parse(file, this);

}

这会停止外部实体解析。如果您要提供某些外部实体,则可以执行此操作检查publicIDsystemID

HTH。