0

我需要将维基百科修订历史加载到 POJO 中,所以我使用 JAXB 来解组 wikipeida 数据转储(嗯,它的各个页面)。问题是文本节点偶尔包含未在维基百科 xml 转储中定义的实体。例如:° (`°' 请记住,我不知道我需要能够读取的完整实体集。我的输入文件是 3tb,所以我们假设所有 html 可以呈现的内容都在其中。 )。

如何配置 JAXB 来处理无效 xml 的实体?

这是 JAXB 在遇到未定义实体时抛出的 SAX 异常:

Exception in thread "main" javax.xml.bind.UnmarshalException

 - with linked exception:

[org.xml.sax.SAXParseException: The entity "deg" was referenced, but not declared.]

    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)

    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:481)

    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:199)

    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:168)

    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)

    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:184)

    at com.stottlerhenke.tools.wikiparse.WikipediaIO.readPage(WikipediaIO.java:73)

    at com.stottlerhenke.tools.wikiparse.WikipediaIO.main(WikipediaIO.java:53)

Caused by: org.xml.sax.SAXParseException: The entity "deg" was referenced, but not declared.

    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)

    at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)

    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)

    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)

    at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)

    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEntityReference(Unknown Source)

    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)

    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)

    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)

    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)

    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)

    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)

    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:195)

编辑:触发该异常的输入是关于北极圈的维基百科文章的完整修订历史。用于生成 JAXB 类的 XSD 位于:http ://www.mediawiki.org/xml/export-0.3.xsd

编辑:这个问题的根源是我的错误——我使用的初始提取器没有正确维护编码实体。但是,如果有人遇到我认为我遇到的问题,我确实找到了解决方法。见下文。

4

2 回答 2

1

解析实体不是 JAXB 的工作。这是底层 XML 解析器的工作。

你可以做的是:

  • 使用 DOM 自己读取数据
  • 用你想要的东西替换所有未解决的实体
  • 然后,让 JAXB 处理结果
于 2009-06-22T23:09:57.853 回答
0

这是一个 hack,但它在紧要关头工作。

我从 w3.org 下载了 html 实体定义,并将输入 xml 文件的 doctype 设置为 xhtml-transitional,但将 doctype url 定向到本地 dtd:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "xhtml1-transitional.dtd">

反过来,xhtml1-transitional.dtd 需要:

  • xhtml-lat1.ent
  • xhtml-special.ent
  • xhtml-symbol.ent

我把它吸下来放在旁边 xhtml1-transitional.dtd

(所有文件都可以在:http ://www.w3.org/TR/xhtml1/DTD/ 获得)

就像我说的,丑得要命,但它似乎确实起到了作用

于 2009-06-23T16:14:28.387 回答