porting to Android: why am I getting “Can't create default XMLReader; is system property org.xml.sax.driver set?”?

…衆ロ難τιáo~ 提交于 2019-12-19 07:51:10

问题


I am porting some Java code that worked fine on my desktop to Android. I have the following code segment:

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
// ...
XMLReader p = XMLReaderFactory.createXMLReader();

At the last line I get the following exception:

Can't create default XMLReader; is system property org.xml.sax.driver set?

When I was testing the code on desktop, it was working fine. Why am I getting this exception on Android and how can I fix it? Thx!


回答1:


I just ran into the same issue while porting some code to Android. I found that creating my own factory and parser seems to work.

I'd try replacing this:

XMLReader p = XMLReaderFactory.createXMLReader();

With this:

SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser newSAXParser = saxParserFactory.newSAXParser();
XMLReader p = newSAXParser.getXMLReader();

Hope this helps :)




回答2:


My solution was including the following code in my MainActivity:

System.setProperty("org.xml.sax.driver","org.xmlpull.v1.sax2.Driver");



回答3:


Resolved by following Parsing an XML File Using SAX (The Java™ Tutorials > Java API for XML Processing (JAXP) > Simple API for XML)



来源:https://stackoverflow.com/questions/10165477/porting-to-android-why-am-i-getting-cant-create-default-xmlreader-is-system

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