简体   繁体   中英

(saxon dtd)java.net.SocketException: Unexpected end of file fromserver

I use Saxon(Java) to convert *.xhtml to *.xml .

Here is my java code:

System.setProperty("javax.xml.transform.TransformerFactory","net.sf.saxon.TransformerFactoryImpl");
TransformerFactory tfactory = TransformerFactory.newInstance();
System.out.println("load xslt file");
Templates templates = tfactory.newTemplates(new StreamSource(xsltFile));

Transformer transformer = templates.newTransformer();
Result result = new StreamResult(new File(filtTempXml));
transformer.transform(new StreamSource(xmlFile), result);

Because there is DTD in the *.xhtml file, like:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

The error:

 java.net.SocketException: Unexpected end of file from server

I want to know:

1) How to simply disable the dtd?

2) If not, how to set catalog file(mapping dtd to local) for saxon in Java program? Any example?

Thanks.

Finally, I know how to implement it.

SAXParserFactory parserFactory = SAXParserFactory.newInstance();
SAXParser saxParser = parserFactory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();

EntityResolver entityReolver = new EntityResolver() {
    public InputSource resolveEntity(String publicId, String systemId) {
    try {
        System.out.println("Entity resolving systemID... " + publicId);
        if (systemId.indexOf((".dtd")) != -1) {
            System.out.println("Entity Resolved...");
            return new InputSource(new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes()));
        }
    } catch (Exception e) {
    }
    return null;
    }
};
xmlReader.setEntityResolver(entityReolver);

SAXSource saxSource = new SAXSource(xmlReader, SAXSource.sourceToInputSource(new   StreamSource(xmlFile)));
transformer.transform(saxSource, result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM