0

The XML

        <foo>text</foo>

Parser code copied from http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html

        XmlPullParserFactory pullParserFactory;
        try {    
            pullParserFactory = XmlPullParserFactory.newInstance();
            pullParserFactory.setNamespaceAware(true);
            mParser = pullParserFactory.newPullParser();

            InputStream inputStream = getResources().openRawResource(R.xml.foo);
            mParser.setInput(inputStream, null);
            //mParser.setInput(new StringReader("<foo>text</foo>"));
            int eventType = mParser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if(eventType == XmlPullParser.START_DOCUMENT) {
                    System.out.println("Start document");
                } else if(eventType == XmlPullParser.START_TAG) {
                    System.out.println("Start tag "+mParser.getName());
                } else if(eventType == XmlPullParser.END_TAG) {
                    System.out.println("End tag "+mParser.getName());
                } else if(eventType == XmlPullParser.TEXT) {
                    System.out.println("Text "+mParser.getText());
                }
                eventType = mParser.next();
            }
            System.out.println("End document");

        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

It generates the error below on the first call to next(), but only when using setInput(InputStream, encoding). The example uses setInput(StringReader) which works fine when you use that version of setInput;

06-07 12:35:30.992: W/System.err(30950): org.xmlpull.v1.XmlPullParserException: Unexpected token (position:TEXT ���������������4������...@1:149 in java.io.InputStreamReader@425ceab0)

3
  • Removed the "text" from the XML the error slightly changed to 06-07 12:39:54.623: W/System.err(31336): org.xmlpull.v1.XmlPullParserException: Unexpected token (position:TEXT ����l����������(������...@1:109 in java.io.InputStreamReader@425d4448) Commented Jun 7, 2014 at 16:41
  • Created a new blank file, typed in the xml, saved it, and no change. Commented Jun 7, 2014 at 16:42
  • I am not too familiar with xml namespace support on XmlPullParser but you could try removing namespace support where you configure XmlPullParserFactory. At least until your xml starts using namespaces. Commented Jun 7, 2014 at 17:11

2 Answers 2

1

Got the answer 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.

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

Comments

0

Instead of using the InputStream to set the input, what you need is to pass InputStreamReader to the setInput

sample:

xpp.setInput(new InputStreamReader(obinputStreamj));

is -> string

 BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
    total.append(line);
}

2 Comments

Ya I saw that in similar unanswered questions. It generates an identical error. Running Android 4.3 on a S3.
@Graham.Fraser the other way is to convert inputstream to string

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.