简体   繁体   English

为什么会出现XmlPullParserException异常?

[英]Why does XmlPullParserException exception occurs?

I try to parse simple xml file with XmlPullParser. 我尝试使用XmlPullParser解析简单的xml文件。

<?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: 这是MainActivity代码:

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: 这是Fetcher类代码:

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? 为什么会出现XmlPullParserException异常?

org.xmlpull.v1.XmlPullParserException: unterminated entity ref (position:TEXT ��������������������...@1:49 in java.io.InputStreamReader@405534d8)

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;
    }
}

I imagine it's this line: parser.setInput(is, null); 我想这就是这一行: parser.setInput(is, null);

Because the xml encoding is UTF-8, I believe you have to change it to parser.setInput(is,"UTF-8"); 因为xml编码是UTF-8,我相信你必须把它改成parser.setInput(is,"UTF-8");

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). 错误消息似乎指的是不存在的位置 - 第1行,第49个字符(反之亦然)。

  • The error message seems to indicate some garbled characters (or a string of NULs), but there is nothing problematic in the sample XML. 错误消息似乎表示一些乱码(或一串NUL),但样本XML中没有任何问题。

A bit late but, in case it helps anyone else, check: 有点晚了,但如果它有助于其他人,请检查:

  • There are no characters that need escaping in the XML file eg & which should be &#amp; 在XML文件中没有需要转义的字符,例如&应该是&#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 确保属性正确用引号括起来(在我的例子中,eclipse有助于提供密切的引号,所以当我在文本末尾添加了引号时,它给出了一个错误

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. 我通过从最外层标签开始检查XML,删除内容并重新添加位,每次运行,以查找错误的位置。 Debugging XML files is a real pain! 调试XML文件真是太痛苦了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM