该XML
对象具有具有以下签名的方法:
def loadXML(source: InputSource, parser: SAXParser): Elem
这允许您提供一个SaxParser
. 你可以给它SaxParser
一个LexicalHandler
具有以下签名的方法:
void startDTD(String name, String publicId, String systemId)
你可以覆盖它。一个例子:
var r = ("", "", "")
val handler = new DefaultHandler2 {
override def startDTD(name:String, publicId:String, systemId:String) = {
r = (name, publicId, systemId)
}
}
val parser = SAXParserFactory.newInstance().newSAXParser()
parser.setProperty("http://xml.org/sax/properties/lexical-handler", handler)
val source = Source fromString """<?xml version="1.0"?>
<!DOCTYPE wikiarticle SYSTEM "wikiarticle.dtd">
<wikiarticle>
<art type="redirect">
<redirect>#REDIRECT[[page]]</redirect>
</art>
</wikiarticle>
"""
XML.loadXML(source, parser)
println(r) // (wikiarticle,null,wikiarticle.dtd)
为了运行你需要一个 dtd 的例子,你可以使用这个:
<!ELEMENT wikiarticle (art)>
<!ELEMENT art (redirect, redirect?, article?)>
<!ATTLIST art type CDATA #REQUIRED>
<!ELEMENT redirect (#PCDATA)>
<!ELEMENT article (#PCDATA)>