javaandroid中的Xml解析问题

javaandroid中的Xml解析问题,java,android,Java,Android,我尝试使用以下方法从str解析xml: DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(str.toString())); Documen

我尝试使用以下方法从
str
解析xml:

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>
这:

is.setCharacterStream(新的StringReader(“baz”);

它工作得很好。有什么想法吗?

我不知道什么是
str
。如果str已经是一个字符串,那么就不需要使用
str.toString

或者,如果str是任何TextView或EditText,则使用str.getText().toString。然后它将工作。如果您有第三种情况,尽管有这两种情况,请指定它,以便我可以编辑我的答案
我建议您切换到SAXXML解析器,而不是DOM解析器。由于它的速度更快,使用更少的内存和更容易实现(Dnt考虑使用像STAX这样的高级解析器,因为它们在android中不受支持)。查看此链接

什么是“应用程序崩溃”?任何异常?org.xml.sax.saxpasseeption:意外标记(位置:TEXT@1:2 in java.io)。StringReader@41369600)你确定Str有那个xml字符串吗?你打印出来了吗?如果str确实存在,我会将str.toString()更改为str.toString().trim();试试看。
is.setCharacterStream(new StringReader(str.toString()));
is.setCharacterStream(new StringReader("<foo><bar>baz</bar></foo>"));