问题
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