关闭scala.xml.XML的DTD验证 [英] Turn off DTD validation for scala.xml.XML

查看:90
本文介绍了关闭scala.xml.XML的DTD验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Scala中的 InputStream 加载XML文档。因此,我正在使用 scala.xml.XML.load(is:InputStream):scala.xml.Elem



XML文档是一个来自Potrace的SVG文件,看起来像这样:

 <?xml version = 1.0 standalone = no?> 
<!DOCTYPE svg公用-// W3C // DTD SVG 20010904 // EN
http://www.w3.org/TR/2001/REC-SVG-20010904/DTD /svg10.dtd\">
< svg版本= 1.0 xmlns = http://www.w3.org/2000/svg
width = 10.000000pt height = 10.000000pt viewBox = 0 0 10.000000 10.000000
reserveAspectRatio = xMidYMid Meet>
<元数据>
由potrace 1.11创建,由Peter Selinger撰写,2001-2013年
< / metadata>
< g transform = translate(0.000000,10.000000)scale(0.100000,-0.100000)
fill =#000000 stroke = none>
< path d = M14 73 c2 -10 11 -20 20 -21 13 -3 17 2 14 14 -4 25 -37 31 -34 7z />
< path d = M67 83 c-4 -3 -7 -15 -7 -25 0 -13 -7 -18 -26 -18 -14 0 -23 -4 -19
- 10 17 -28 75 9 75 47 0 14 -12 17 -23 6z /
< path d = M74 21 c-5 -5 -22 -11 -39 -14 -27 -4 -27 -4 7 -6 20 0 40 4 43 10 9
14 1 21 -11 10z />
< / g>
< / svg>

我的问题是,解析InputStream大约需要15秒,这很烦人。



我发现,从文件中删除<!DOCTYPE> 并没有延迟。因此,底层的 SAXParser 可能会下载<!DOCTYPE> 中的DTD来检查文档是否有效。 / p>

如何配置Scala的 XML.load (可能使用 XML.withSAXParser )是否不验证文档?

解决方案

您需要为使用的解析器禁用DTD验证。操作方法如下:
忽略Scala中的DTD规范


I want to load an XML document from an InputStream in Scala. Therefore I am using scala.xml.XML.load(is: InputStream): scala.xml.Elem.

The XML document is a SVG file that comes from Potrace and looks like this:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
 "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
 width="10.000000pt" height="10.000000pt" viewBox="0 0 10.000000 10.000000"
 preserveAspectRatio="xMidYMid meet">
<metadata>
Created by potrace 1.11, written by Peter Selinger 2001-2013
</metadata>
<g transform="translate(0.000000,10.000000) scale(0.100000,-0.100000)"
fill="#000000" stroke="none">
<path d="M14 73 c2 -10 11 -20 20 -21 13 -3 17 2 14 14 -4 25 -37 31 -34 7z"/>
<path d="M67 83 c-4 -3 -7 -15 -7 -25 0 -13 -7 -18 -26 -18 -14 0 -23 -4 -19
-10 17 -28 75 9 75 47 0 14 -12 17 -23 6z"/>
<path d="M74 21 c-5 -5 -22 -11 -39 -14 -27 -4 -27 -4 7 -6 20 0 40 4 43 10 9
14 1 21 -11 10z"/>
</g>
</svg>

My problem is, that parsing the InputStream takes around 15 seconds, which is annoying.

I figured out, that there's no delay when I remove the <!DOCTYPE> from the file. So probably the underlying SAXParser downloads the DTD in the <!DOCTYPE> to check that the document is valid.

How can I configure Scala's XML.load (probably by using XML.withSAXParser) to not validate the document?

解决方案

You need to disable validation using DTD for the parser that you use. Here is how: Ignore DTD specification in scala

这篇关于关闭scala.xml.XML的DTD验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆