0

I try to parse simple xml file with XmlPullParser.

<?xml version="1.0" encoding="utf-8"?>

<tests>
    <test>
        <name>Jack</name>
    </test>
    <test>
        <name>Brad</name>
    </test>
    <test>
        <name>Tom</name>
    </test>
</tests>

This is the MainActivity code:

public class MainActivity extends ActionBarActivity {
    ViewPager viewPager;
    PagerAdapter pagerAdapter;
    ArrayList<Quote> quotes;
    ArrayList<Pers> persons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        InputStream inputStream = getResources().openRawResource(R.xml.test);

        Fetcher fetcher = new Fetcher();
        persons = fetcher.parse(inputStream);

        String str = new String();
        for(int i = 0; i < persons.size(); i++) {
            str += persons.get(i).getName();
        }
        Log.d("1", str);
    }
}

This is the Fetcher class code:

public class Fetcher {
    private ArrayList<Pers> persons;
    private Pers person;
    private String text;

    public Fetcher() {
        persons = new ArrayList<Pers>();
    }

    public ArrayList<Pers> parse(InputStream is) {
        XmlPullParserFactory factory = null;
        XmlPullParser parser = null;
        try {
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            parser = factory.newPullParser();
            parser.setInput(is, null);
            int eventType = parser.getEventType();
            while(eventType != XmlPullParser.END_DOCUMENT) {
                String tagname = parser.getName();
                switch(eventType) {
                    case XmlPullParser.START_TAG:
                        if(tagname.equalsIgnoreCase("test")) {
                            person = new Pers();
                        }
                        break;
                    case XmlPullParser.TEXT:
                        text = parser.getText();
                        break;
                    case XmlPullParser.END_TAG:
                        if(tagname.equalsIgnoreCase("test")) {
                            persons.add(person);
                        } else if(tagname.equalsIgnoreCase("name")) {
                            person.setName(text);
                        }
                        break;
                    default:
                        break;
                }
                eventType = parser.next();
            }
        } catch(XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return persons;
    }
}

Why does XmlPullParserException exception occurs?

org.xmlpull.v1.XmlPullParserException: unterminated entity ref (position:TEXT ��������������������...@1:49 in java.io.InputStreamReader@405534d8)
4
  • Because there's an unterminated entity ref in your XML. Commented Oct 21, 2013 at 17:54
  • @Dave Newton May be you have some ideas how to fix this? I will be grateful to you if you can help me. Commented Oct 21, 2013 at 18:09
  • How did you create the xml? Did you create a plain text file, or did you use a word processor, copy from a web page, etc? It's likely got some binary or other garbage in it. Commented Oct 21, 2013 at 18:24
  • @Dave Newton i created xml file using Android Studio into xml folder. New->Resource xml file Commented Oct 21, 2013 at 18:36

4 Answers 4

2

I changed code, now it works. Thank those who tried to help me.

public class Fetcher {
    private ArrayList<Pers> persons;
    private Pers person;
    private String text;

    public Fetcher() {
        persons = new ArrayList<Pers>();
    }

    public ArrayList<Quote> parse(Activity activity) throws XmlPullParserException, IOException {
        Resources resources = activity.getResources();
        XmlResourceParser xmlResourceParser = resources.getXml(R.xml.quotes);
        person = new Pers();
        xmlResourceParser.next();
        int eventType = xmlResourceParser.getEventType();
        while(eventType != XmlPullParser.END_DOCUMENT) {
            String tagName = xmlResourceParser.getName();
            switch(eventType) {
                case XmlPullParser.START_TAG: {
                    if(tagName.equalsIgnoreCase("test")) {
                        person = new Pers();
                    }
                    break;
                }

                case XmlPullParser.TEXT: {
                    text = xmlResourceParser.getText();
                    break;
                }

                case XmlPullParser.END_TAG: {
                    if(tagName.equalsIgnoreCase("test")) {
                        persons.add(person);
                    } else if(tagName.equalsIgnoreCase("name")) {
                        person.setId(Integer.parseInt(text));
                    }
                    break;
                }

                default:
                    break;
            }
            eventType = xmlResourceParser.next();
        }

        return persons;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I imagine it's this line: parser.setInput(is, null);

Because the xml encoding is UTF-8, I believe you have to change it to parser.setInput(is,"UTF-8");

1 Comment

I set utf-8 charset, but nothing changed.
0

I think that the problem is in different input to what you have shown us. Or maybe the source code is different.

  • The error message seems to refer to a position that does not exist - line 1, character position 49 (or vice versa).

  • The error message seems to indicate some garbled characters (or a string of NULs), but there is nothing problematic in the sample XML.

Comments

0

A bit late but, in case it helps anyone else, check:

  • There are no characters that need escaping in the XML file e.g. & which should be &#amp;
  • Make sure the attributes are correctly enclosed in quotes (in my case, eclipse had been helpful putting close quotes in, so when I added the close quotes at the end of my text, it gave an error

I checked through my XML by starting with the outermost tags, deleting the content and re-adding in bits, running each time, to find where the errors were. Debugging XML files is a real pain!

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.