Yur*_*ish 8 java xml validation xsd dtd
有没有办法在不修改源代码(构造DocumentBuilder的库)的情况下基于外部DTD/XSD禁用XML验证?类似于为DocumentBuilderFactory功能设置JVM范围的默认值,对于SAX也是如此?
在IDE中编辑文件时验证很棒,但我不需要因为somelib.net发生故障而无法启动我的webapp.
我知道我可以指定本地DTD/XSD位置,但这是一个不方便的解决方法.
有什么选择?我能想到两个:
featuresHashtable(添加http://apache.org/xml/features/nonvalidating/load-external-dtd).禁用验证可能无法阻止处理器获取DTD,因为它仍然可以这样做以便使用DTD中存在的属性默认值等(它将放置在树中),即使它没有对DTD进行实际验证也是如此.语法.
处理XML文档时防止网络活动的一种技术是使用这样的"消隐解析器":
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class BlankingResolver implements EntityResolver
{
public InputSource resolveEntity( String arg0, String arg1 ) throws SAXException,
IOException
{
return new InputSource( new ByteArrayInputStream( "".getBytes() ) );
}
}
Run Code Online (Sandbox Code Playgroud)
然后在处理之前设置它,如下所示:
DocumentBuilderFactory factory = DocumentBuilderFactory.
factory.setNamespaceAware( true );
builder = factory.newDocumentBuilder();
builder.setEntityResolver( new BlankingResolver() );
myDoc = builder.parse( myDocUri );
// etc.
Run Code Online (Sandbox Code Playgroud)
然后,您还将确保正在处理的文档未被DTD.y中的任何信息更改
| 归档时间: |
|
| 查看次数: |
5980 次 |
| 最近记录: |