在Scala中将字符串加载为XML时出错

时间:2019-02-24 11:11:09

标签: xml scala encoding

我具有以下XML(作为def init = { servletContext -> def ctx = grailsApplication.mainContext ['TEST', 'TEST2'].each { String name -> HibernateDatastore ds = hibernateDatastore.getDatastoreForConnection(name) VehicleListenerService listener = new VehicleListenerService(ds) ctx.addApplicationListener(listener) } } 类型)。

String

当我执行以下操作

<article mdate="2017-06-06" key="journals/geb/SonmezU05">
<author>Tayfun S&ouml;nmez</author>
<author orcid="0000-0001-7693-1635">M. Utku &Uuml;nver</author>
<title>House allocation with existing tenants: an equivalence.</title>
<pages>153-185</pages>
<year>2005</year>
<volume>52</volume>
<journal>Games and Economic Behavior</journal>
<number>1</number>
<ee>https://doi.org/10.1016/j.geb.2004.04.008</ee>
<url>db/journals/geb/geb52.html#SonmezU05</url>
</article>

我收到以下错误:

XML.loadString()

由于行:

org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 23; The entity "ouml" was referenced, but not declared.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1472)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEntityReference(XMLDocumentFragmentScannerImpl.java:1902)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3061)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:327)
    at scala.xml.factory.XMLLoader.loadXML(XMLLoader.scala:41)
    at scala.xml.factory.XMLLoader.loadXML$(XMLLoader.scala:37)
    at scala.xml.XML$.loadXML(XML.scala:60)
    at scala.xml.factory.XMLLoader.loadString(XMLLoader.scala:60)
    at scala.xml.factory.XMLLoader.loadString$(XMLLoader.scala:60)
    at scala.xml.XML$.loadString(XML.scala:60)

尝试将字符串转换为<author>Tayfun S&ouml;nmez</author>

InputStream

但是问题仍然存在。已经为此苦苦挣扎了很长一段时间。 尝试了许多在线可用内容,并推荐了诸如this

的帖子

但是没有进展。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

我认为是因为&ouml;不是标准的xml实体。在HTML中,浏览器可以理解,但在XML中则可以。在文件中添加声明可能会有所帮助。

<!DOCTYPE article [
  <!ENTITY ouml "your redired value">
]>

与&Uuml;

相同

答案 1 :(得分:0)

如果while ($fetch = $query->fetch(PDO::FETCH_ASSOC)) { $array[$fetch['AVA_RoomId']][$fetch['AVA_Date']][] = [ 'AVA_Id' => $fetch['AVA_Id'], 'AVA_Status' => $fetch['AVA_Status'] ]; } 是唯一缺少的实体,则可以按照Kaustabh的建议以DOCTYPE内联定义它。

&ouml;

但是,如果您有很多这样的实体,最好创建一个单独的<!DOCTYPE article [ <!ENTITY ouml "your redired value"> ]> 文件(例如.dtd)并在XML中引用它。

"myxml.dtd"

现在,为了使解析器能够找到文件,应将其放置在项目路径中。如果您将DTD文件与应用程序捆绑在一起,则可以将该文件放在<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE myxml SYSTEM "myxml.dtd"> <myxml> // The rest of your XML </myxml> 文件夹中,找到该文件的路径,然后将其替换为XML字符串。

resources

使用val dtdFilePath = getClass.getClassLoader.getResource("myxml.dtd").toURI val xmlString = s""" <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE myxml SYSTEM "$dtdFilePath"> <myxml> // The rest of your XML </myxml> """ val xml = XML.loadString(xmlString) 加载文件可确保即使使用ClassLoader分发应用时也可以访问文件。