6

I am using XmlPullParser for xml parsing in my android app but when I set input as InputStream it not works while I set input as Reader it starts working

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(obj,null);//obj is the object of InputStream
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
                 logger.println("eventType.."+eventType);
              if(eventType == XmlPullParser.START_DOCUMENT) {

                     // control goes here only

              } else if(eventType == XmlPullParser.START_TAG) {
                  //This block never executed
                  }

              } else if(eventType == XmlPullParser.END_TAG) {
                 //This block never executed
              } else if(eventType == XmlPullParser.TEXT) {

              }
              eventType = xpp.next();
             }

Even if I store data from InputStream object in a string and set that String as input then this code also works fine.

xpp.setInput(new StringReader(str));//str contains the data from InputStream
6
  • what does the xml declaration (i.e. the "<?xml version="1.0" encoding="UTF-8" ?>" part) in your document look like? Commented Jun 25, 2012 at 13:53
  • <?xml version="1.0" encoding="UTF-8" standalone="no"?> <res_transfer ip="" uuid="" type="2" cons_tf_id="" prod_tf_id=""> <file_data type="0" name="" chunkno="" totalchunks="" md5="" bytes="" /> </res_transfer> this is my xml structure Commented Jun 25, 2012 at 14:09
  • 1
    Can you post the code where you create obj? Commented Jun 25, 2012 at 14:15
  • InputStram obj=socket.getInputStream(); Commented Jun 25, 2012 at 14:21
  • Do you perform any action in the loop (apart from checking the eventType)? Maybe it is related to the namespace awareness, try to remove the line factory.setNamespaceAware(true); and see if that helps... Commented Jun 25, 2012 at 14:48

2 Answers 2

4

The same problem: passing InputStream directly works fine on Android 2.3.3 but doesn't work on 4.1. You can use xpp.setInput(new InputStreamReader(obj));

Sign up to request clarification or add additional context in comments.

1 Comment

Running Android 4.3 and InputStreamReader generates the same error.
0

Got the answer for a similar problem from yano on this thread: XmlPullParser - unexpected token (android)

You need to move from file from res/xml to assets and get the file with the code:

InputStream in = this.getAssets().open("sample.xml");

Apparently getRawResource() does not read the encoding properly and if you just dump the contents of the inputstream there are plenty of garbage characters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.