Java SAXParser parsing invalid xml [duplicate]

怎甘沉沦 提交于 2019-12-11 00:53:09

问题


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.

Thanks.


回答1:


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.



来源:https://stackoverflow.com/questions/14464356/java-saxparser-parsing-invalid-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!