标记必须格式正确
首先,我想说我是 SAX 和 Java 的新手。
我正在尝试从格式不正确的 XML 文件中读取信息。
当我尝试使用 SAX 或 DOM 解析器时,我收到以下错误响应:
The markup in the document following the root element must be well-formed.
这是我设置 XML 文件的方式:
<format type="filename" t="13241">0;W650;004;AG-Erzgeb</format>
<format type="driver" t="123412">001;023</format>
...
我可以强制 SAX 或 DOM 解析 XML 文件,即使它们不是格式良好的 XML?
感谢您的帮助。非常感谢。 海瑟姆
First off, let me say I am a new to SAX and Java.
I am trying to read information from an XML file that is not well formed.
When I try to use the SAX or DOM Parser I get the following error in response:
The markup in the document following the root element must be well-formed.
This is how I set up my XML file:
<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?
Thank you for your help. Much appreciated.
Haythem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于 DOM 会扫描您的 xml 文件,然后构建一棵树,因此树的根节点就像 1 个答案一样。但是,如果解析器找不到 或 ,它甚至可以构建树。因此,最好在使用 DOM 或 Sax 解析 xml 文件之前对它进行一些预处理。
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.
最好的办法是使 XML 格式良好,可能需要对其进行一些预处理。在这种情况下,您可以简单地通过放置 XML 声明(甚至是可选的)并提供根元素(不是可选的)来实现这一点,如下所示:
我任意为根选择了名称“wrapper”元素;它可以是任何你喜欢的。
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:
There I've arbitrarily picked the name "wrapper" for the root element; it can be whatever you like.
提示:使用 sax 或 stax,您可以成功解析格式不正确的 xml 文档,直到遇到FIRST“格式正确”错误。
(我知道这没有太大帮助......)
Hint: using sax or stax you can successfully parse a not well formed xml document until the FIRST "well formed-ness" error is encountered.
(I know that this is not of too much help...)