1

I try to parse xml from str with this methods:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();    
DocumentBuilder db = dbf.newDocumentBuilder();    
InputSource is = new InputSource();    
is.setCharacterStream(new StringReader(str.toString()));    
Document doc = db.parse(is);

and in str i have :

<foo><bar>baz</bar></foo>

but always in db.parse(is); the app is crash. but if i put instead :

is.setCharacterStream(new StringReader(str.toString()));

this:

is.setCharacterStream(new StringReader("<foo><bar>baz</bar></foo>"));

it work perfect. any idea?

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
YosiFZ
  • 7,792
  • 21
  • 114
  • 221
  • 2
    What excatly is "the app is crash"? Any exception? – Bhesh Gurung Jan 12 '12 at 22:31
  • org.xml.sax.SAXParseException: Unexpected token (position:TEXT @1:2 in java.io.StringReader@41369600) – YosiFZ Jan 12 '12 at 22:44
  • are you sure Str has that xml string? Did you printout? If str is really there, I would change str.toString() to str.toString().trim(); and try. – kosa Jan 12 '12 at 22:59

2 Answers2

0

I did not know what is str.If str is already a string then not need to use str.toString.

or if str is any TextView or EditText then use str.getText().toString.Then it will work.And if you have third case in spite of these two then please specify this so that i could edit my answer

Tofeeq Ahmad
  • 11,935
  • 4
  • 61
  • 87
0


I suggest you to switch to SAX XML parser rather than DOM Parser . As its faster ,uses less memory and easier to implement (Dnt think of using advance parser like STAX as they are not supported in android ). Check this link out ANDROID: Parsing XML

Community
  • 1
  • 1
Code_Life
  • 5,742
  • 4
  • 29
  • 49