【问题标题】:How to Prevent Html Formatted data parsing through JAXB如何防止通过 JAXB 解析 Html 格式的数据
【发布时间】:2012-03-26 10:49:52
【问题描述】:

我正在使用 JAXB 来解析 XML 流。 此流可能包含 HTML 格式的数据。 当我使用 jaxb 解组此 xml 以获取无效的 html 内容(如没有结束标记的

等)时,我收到以下错误:

javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 2987; The element type "BR" must be terminated by the matching end-tag </BR&gt.]

at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at arserImpl$JAXPSAXParser.parse(Unknown Source)

有什么办法可以阻止这种 HTML 格式的数据解析/验证或注释 XML 中的某些数据,这些数据将被视为纯字符串。
提前致谢。

【问题讨论】:

    标签: java html xml jaxb


    【解决方案1】:

    您可以先使用 JTidy 之类的东西将您的输入转换为有效的 XML。

    【讨论】:

      【解决方案2】:

      这是失败的,因为它是无效的 XML。您最好的解决方案是让生成此文件的任何内容都生成有效的 XML。

      如果您有能力预处理此文件,使其将部分数据视为纯文本的方法是将其放入CDATA 部分。

      【讨论】:

        猜你喜欢
        • 2010-09-21
        • 1970-01-01
        • 1970-01-01
        • 2011-01-24
        • 1970-01-01
        • 2021-07-11
        • 2013-06-21
        • 1970-01-01
        • 2013-07-17
        相关资源
        最近更新 更多