XML解析的问题,我尝试从str
这个方法解析XML:在Java的Android
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(str.toString()));
Document doc = db.parse(is);
和str
我有:
<foo><bar>baz</bar></foo>
但总是db.parse(is);
应用是崩溃。 但如果我把代替:
is.setCharacterStream(new StringReader(str.toString()));
这样的:
is.setCharacterStream(new StringReader("<foo><bar>baz</bar></foo>"));
它的工作完美。任何想法?
什么excatly是“应用程序是死机”?有什么异常? – 2012-01-12 22:31:48
org.xml.sax.SAXParseException:意外令牌(位置:TEXT @ 1:2 [email protected]) – MTA 2012-01-12 22:44:45
你确定乙方拥有该XML字符串?你打印了吗?如果str真的存在,我会将str.toString()更改为str.toString()。trim();并尝试。 – kosa 2012-01-12 22:59:03