忽略Scala中的DTD规范 [英] Ignore DTD specification in scala

查看:59
本文介绍了忽略Scala中的DTD规范的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Scala解析xml文件时,我偶尔会忽略dtd规范。我知道可以通过

I'd like to occasionally ignore the dtd specification while parsing an xml file using Scala. I know that this can be done pretty easily with the java interface by doing

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

dbf.setValidating(false);
dbf.setFeature("http://xml.org/sax/features/namespaces", false);
dbf.setFeature("http://xml.org/sax/features/validation", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

但是,我不确定如何使用Scala的xml库轻松完成此操作。如果可能的话,我想继续使用scala xml库,因为它明显更好。

However, I'm not sure of how to do this easily with Scala's xml library. If possible i'd like to continue using the scala xml library as it's significantly better.

预先感谢!

推荐答案

这对我有用,但这取决于XML解析器的实现。

This works for me, but it depends on the implementation of the XML parser.

import scala.xml.Elem
import scala.xml.factory.XMLLoader
import javax.xml.parsers.SAXParser
object MyXML extends XMLLoader[Elem] {
  override def parser: SAXParser = {
    val f = javax.xml.parsers.SAXParserFactory.newInstance()
    f.setNamespaceAware(false)
    f.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
    f.newSAXParser()
  }
}

另请参见这个问题,这确实是您的问题,但措词带有敌意。

See also this question, which is really your question but worded in a hostile way.

这篇关于忽略Scala中的DTD规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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