Java,xml,XSLT:防止DTD验证

the*_*ega 5 java xml xslt

我使用Java(6)XML-Api对来自Web的html文档应用xslt转换.这个文件格式正确xhtml,因此包含有效的DTD-Spec(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">).现在出现问题:Uppon转换XSLT-Processor尝试下载DTD并且w3-server通过HTTP 503错误拒绝这一点(由于w3的Bandwith限制).

如何防止XSLT-Processor下载dtd?我不需要我的输入文档验证.

来源是:

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
Run Code Online (Sandbox Code Playgroud)

-

   String xslt = "<?xml version=\"1.0\"?>"+
   "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"+
   "    <xsl:output method=\"text\" />"+          
   "    <xsl:template match=\"//html/body//div[@id='bodyContent']/p[1]\"> "+
   "        <xsl:value-of select=\".\" />"+
   "     </xsl:template>"+
   "     <xsl:template match=\"text()\" />"+
   "</xsl:stylesheet>";

   try {
   Source xmlSource = new StreamSource("http://de.wikipedia.org/wiki/Right_Livelihood_Award");
   Source xsltSource = new StreamSource(new StringReader(xslt));
   TransformerFactory ft = TransformerFactory.newInstance();

   Transformer trans = ft.newTransformer(xsltSource);

   trans.transform(xmlSource, new StreamResult(System.out));
   }
   catch (Exception e) {
     e.printStackTrace();
   }
Run Code Online (Sandbox Code Playgroud)

我在这里阅读了以下问题,但它们都使用了另一个XML-Api:

谢谢!

jph*_*jph 5

我最近在使用JAXB解组XML时遇到了这个问题.答案是从XmlReader和InputSource创建一个SAXSource,然后将其传递给JAXB UnMarshaller的unmarshal()方法.为了避免加载外部DTD,我在XmlReader上设置了一个自定义EntityResolver.

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xmlr = sp.getXMLReader();
xmlr.setEntityResolver(new EntityResolver() {
    public InputSource resolveEntity(String pid, String sid) throws SAXException {
        if (sid.equals("your remote dtd url here"))
            return new InputSource(new StringReader("actual contents of remote dtd"));
        throw new SAXException("unable to resolve remote entity, sid = " + sid);
    } } );
SAXSource ss = new SAXSource(xmlr, myInputSource);
Run Code Online (Sandbox Code Playgroud)

如上所述,如果要求解析实体以外的其他实体,则该自定义实体解析程序将抛出异​​常,而不是您希望它解析的实体.如果您只是希望它继续并加载远程实体,请删除"throws"行.