Java 标记必须格式良好

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2499373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:30:08  来源:igfitidea点击:

The markup must be well-formed

javaxmlsaxwell-formed

提问by Haythem

First off, let me say I am a new to SAX and Java.

首先,让我说我是 SAX 和 Java 的新手。

I am trying to read information from an XML file that is not well formed.

我正在尝试从格式不正确的 XML 文件中读取信息。

When I try to use the SAX or DOM Parser I get the following error in response:

当我尝试使用 SAX 或 DOM 解析器时,我收到以下错误响应:

The markup in the document following the root element must be well-formed.

This is how I set up my XML file:

这是我设置 XML 文件的方式:

<format type="filename" t="13241">0;W650;004;AG-Erzgeb</format>
<format type="driver" t="123412">001;023</format>
   ...

Can I force the SAX or DOM to parse XML files even if they are not well formed XML?

我是否可以强制 SAX 或 DOM 解析 XML 文件,即使它们不是格式正确的 XML?

Thank you for your help. Much appreciated. Haythem

感谢您的帮助。非常感激。海瑟姆

采纳答案by T.J. Crowder

Your best bet is to make the XML well-formed, probably by pre-processing it a bit. In this case, you can achieve that simply by putting an XML declaration on (and even that's optional) and providing a root element (which is not optional), like this:

最好的办法是使 XML 格式良好,可能是通过对其进行一些预处理。在这种情况下,您可以简单地通过放置一个 XML 声明(甚至这是可选的)并提供一个根元素(这不是可选的)来实现,如下所示:

<?xml version="1.0"?>
<wrapper>
    <format type="filename" t="13241">0;W650;004;AG-Erzgeb</format>
    <format type="driver" t="123412">001;023</format>
</wrapper>

There I've arbitrarily picked the name "wrapper" for the root element; it can be whatever you like.

在那里,我为根元素随意选择了名称“包装器”;它可以是任何你喜欢的。

回答by Yaneeve

Hint: using sax or stax you can successfully parse a not well formed xml document until the FIRST"well formed-ness" error is encountered.

提示:使用 sax 或 stax 您可以成功解析格式不正确的 xml 文档,直到遇到第一个“格式良好”错误。

(I know that this is not of too much help...)

(我知道这没有太大帮助......)

回答by jasonfungsing

As the DOM will scan you xml file then build a tree, the root node of the tree is like the as 1 Answer. However, if the Parser can't find the or even , it can even build the tree. So, its better to do some pre-processing the xml file before parser it by DOM or Sax.

由于 DOM 会扫描您的 xml 文件然后构建一棵树,因此树的根节点就像 1 个回答。但是,如果解析器找不到 甚至 ,它甚至可以构建树。因此,最好在通过 DOM 或 Sax 解析之前对 xml 文件进行一些预处理。