Java SAXParser解析无效的xml [英] Java SAXParser parsing invalid xml

查看:125
本文介绍了Java SAXParser解析无效的xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析一些无效的xml,因为属性不在引号中,有什么办法可以解决此问题?下面是一个简单的示例以及Java代码.

I'm trying to parse some xml which is invalid as the attributes are not in quotes, is there any way of getting around this? A simple example of this below, as well as the java code.

XML

<car id=1>
.
.
</car>

Java

  SAXParserFactory factory = SAXParserFactory.newInstance();
  factory.setValidating(false);
  SAXParser saxParser = factory.newSAXParser();
  saxParser.parse(page, handler);  //page is an input stream where the xml is.

谢谢.

推荐答案

您遇到的是格式正确问题,而不是验证问题(您发布的代码是仅禁用验证). XML解析器要求xml的格式正确,并且主要是为了避免验证问题而编写的.也许如果您查看 JSoup 之类的html解析器,您就有更好的机会,因为他们也对格式正确的内容有所宽容当他们尝试自动更正它们时.

What you have is well-formedness issue and not a validation issue (the code you posted is only disabling the validation). XML Parsers require the xml to be wellformed and are mostly written to forgive only validation issues. May be if you look at html parsers like JSoup you have a better chance as they are forgiving about the well-formedness as well as they try to auto correct them.

阅读本文,以了解格式正确与有效性.

Read this article to understand the difference between well-formedness and Validity.

这篇关于Java SAXParser解析无效的xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆