[英]Unmarshaling xml with html entities using JAXB
我需要將Wikipedia修訂歷史記錄加載到POJO中,因此我正在使用JAXB解組Wikipeida數據轉儲(很好,它的各個頁面)。 問題在於文本節點有時包含在Wikipedia 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)
編輯:觸發該異常的輸入是Arctic Circle上Wikipedia文章的完整修訂歷史。 用於生成JAXB類的XSD在這里: http : //www.mediawiki.org/xml/export-0.3.xsd
編輯:這個問題的根源是我的一個錯誤-我使用的初始提取器無法正確維護編碼的實體。 但是,如果有人遇到我認為的問題,我確實找到了解決方法。 見下文。
解決實體不是JAXB的工作。 這是基礎XML解析器的工作。
您可以做的是:
這是一個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則需要:
我吸了下來,放在旁邊xhtml1-transitional.dtd
(所有文件位於: http : //www.w3.org/TR/xhtml1/DTD/ )
就像我說的那樣,丑陋如地獄,但它確實做到了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.