问题
Is there a way to configure features/properties on the SAX parser so that it matches the default Android one?
I have implemented a SAX parser for an Atom feed and I'd like to be able to unit test it without running it via an InstrumentationTestCase. The differences I see right away is that in startElement(), localName has the element name when running on Android, yet the "name" method parameter is populated when running under the Sun JVM. In addition, whitespace is ignored when running on the Sun JVM, but not on Android.
回答1:
For those looking, after some trial and error, these two features seemed to make it the most similar. In the end, we ended up running these types of unit tests as instrumentation tests due to the differences in the XML parsing.
XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
reader.setFeature("http://xml.org/sax/features/namespaces", false);
reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
reader.setContentHandler(handler);
reader.parse(source);
回答2:
probably need to configure the parser to be namespace aware, and set the "http://apache.org/xml/features/dom/include-ignorable-whitespace" feature to true.
来源:https://stackoverflow.com/questions/5481043/how-can-i-configure-a-sax-parser-in-the-sun-jvm-to-match-the-android-one