【问题标题】:SAXParseException XML document structures must start and end within the same entitySAXParseException XML 文档结构必须在同一个实体内开始和结束
【发布时间】:2017-04-02 13:34:32
【问题描述】:

所以我知道使用这个 API,我在服务器上生成的一些 xml 文件可能会生成错误或没有关闭某些标签,或者结构不正确。 现在我的代码工作正常,但是对于某些文件,其中一些 xml 文件会抛出这些错误,下面是错误块

org.xml.sax.SAXParseException; systemId: file:///E:/ARCHIVED_LOGS/BACKUP_LOG_190317_0000/trace_file.xml; lineNumber: 201; columnNumber: 105; XML document structures must start and end within the same entity.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.endEntity(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.endEntity(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.endEntity(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.peekChar(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)

那么有没有一种方法可以处理这些文件而不使用 SAX 解析器 API 解决问题?

我正在考虑逐行处理这些文件,但这很痛苦。

还有一种类似于跳过 dtd 验证的方法,如下所示

   SAXParserFactory factory = SAXParserFactory.newInstance();

   factory.setValidating(false);                
   factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
   SAXParser saxParser = factory.newSAXParser();

谢谢你:)

【问题讨论】:

    标签: java xml saxparser validating


    【解决方案1】:

    您不能使用符合标准的 XML 解析器来处理不符合标准的(非)XML 文档。

    您可以使用不符合标准的解析器 - 例如 HTML 解析器 - 它可能会提供 SAX 解析器 API - 但是您是否可以找到一个解析器来接受向您抛出的非 XML 的特定风格是一个悬而未决的问题,因为您没有给我们任何关于这种非 XML 语言的规范。

    【讨论】:

    • 感谢您的回复,我遇到的 XML 的问题是它们是从以 XML 形式写入日志的实时服务器生成的;因此,这些文件在写入过程中被占用,或者在下载(复制)这些文件时发生了重新启动。结果,其中一些最终以不正确的标签关闭而告终。我正在考虑添加另一种处理方法(逐行读取),以防异常写入同一个 HashMap
    猜你喜欢
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多