(saxon dtd)java.net.SocketException:来自服务器的文件意外结束

发布于 2024-12-09 22:09:18 字数 953 浏览 0 评论 0原文

我使用 Saxon(Java) 将 *.xhtml 转换为 *.xml

这是我的java代码:

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);

因为*.xhtml文件中有DTD,例如:

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

错误:

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

我想知道:

1)如何简单地禁用dtd?

2)如果没有,如何在Java程序中为saxon设置目录文件(将dtd映射到本地)?有什么例子吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘落散花 2024-12-16 22:09:18

终于知道如何实现了。

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文