0

我有一个名为data.xml的本地 xml 文件。我已经使用PullParser对其进行了解析。解析后logcat输出为:

12-06 10:51:33.761: I/End:(5026): START_TAG: quiz
12-06 10:51:33.761: I/End:(5026): START_TAG: mchoice
12-06 10:51:33.761: I/End:(5026): START_TAG: question
12-06 10:51:33.761: I/End:(5026): TEXT: Not a team sport for sure
12-06 10:51:33.761: I/End:(5026): END_TAG: question
12-06 10:51:33.761: I/End:(5026): START_TAG: id
12-06 10:51:33.761: I/End:(5026): TEXT: 1
12-06 10:51:33.761: I/End:(5026): END_TAG: id
12-06 10:51:33.761: I/End:(5026): START_TAG: option1
12-06 10:51:33.761: I/End:(5026): TEXT: Cricket
12-06 10:51:33.761: I/End:(5026): END_TAG: option1
12-06 10:51:33.761: I/End:(5026): START_TAG: option2
12-06 10:51:33.761: I/End:(5026): TEXT: Tennis
12-06 10:51:33.761: I/End:(5026): END_TAG: option2
12-06 10:51:33.761: I/End:(5026): START_TAG: option3
12-06 10:51:33.761: I/End:(5026): TEXT: Rugby
12-06 10:51:33.761: I/End:(5026): END_TAG: option3
12-06 10:51:33.761: I/End:(5026): START_TAG: option4
12-06 10:51:33.761: I/End:(5026): TEXT: Soccer
12-06 10:51:33.761: I/End:(5026): END_TAG: option4
12-06 10:51:33.761: I/End:(5026): START_TAG: answer
12-06 10:51:33.761: I/End:(5026): TEXT: Tennis
12-06 10:51:33.761: I/End:(5026): END_TAG: answer
12-06 10:51:33.761: I/End:(5026): END_TAG: mchoice
12-06 10:51:33.761: I/End:(5026): START_TAG: mchoice
12-06 10:51:33.761: I/End:(5026): START_TAG: question
12-06 10:51:33.761: I/End:(5026): TEXT: I am the biggest planet in the Solar System
12-06 10:51:33.761: I/End:(5026): END_TAG: question
12-06 10:51:33.761: I/End:(5026): START_TAG: id
12-06 10:51:33.761: I/End:(5026): TEXT: 2
12-06 10:51:33.761: I/End:(5026): END_TAG: id
12-06 10:51:33.761: I/End:(5026): START_TAG: option1
12-06 10:51:33.761: I/End:(5026): TEXT: Saturn
12-06 10:51:33.761: I/End:(5026): END_TAG: option1
12-06 10:51:33.761: I/End:(5026): START_TAG: option2
12-06 10:51:33.761: I/End:(5026): TEXT: Jupiter
12-06 10:51:33.761: I/End:(5026): END_TAG: option2
12-06 10:51:33.761: I/End:(5026): START_TAG: option3
12-06 10:51:33.761: I/End:(5026): TEXT: Neptune
12-06 10:51:33.761: I/End:(5026): END_TAG: option3
12-06 10:51:33.761: I/End:(5026): START_TAG: option4
12-06 10:51:33.761: I/End:(5026): TEXT: Pluto
12-06 10:51:33.761: I/End:(5026): END_TAG: option4
12-06 10:51:33.761: I/End:(5026): START_TAG: answer
12-06 10:51:33.761: I/End:(5026): TEXT: Jupiter
12-06 10:51:33.761: I/End:(5026): END_TAG: answer
12-06 10:51:33.761: I/End:(5026): END_TAG: mchoice
12-06 10:51:33.761: I/End:(5026): START_TAG: mchoice
12-06 10:51:33.761: I/End:(5026): START_TAG: question
12-06 10:51:33.761: I/End:(5026): TEXT: I am the closest star to the earth
12-06 10:51:33.761: I/End:(5026): END_TAG: question
12-06 10:51:33.761: I/End:(5026): START_TAG: id
12-06 10:51:33.761: I/End:(5026): TEXT: 3
12-06 10:51:33.761: I/End:(5026): END_TAG: id
12-06 10:51:33.761: I/End:(5026): START_TAG: option1
12-06 10:51:33.761: I/End:(5026): TEXT: Milky way
12-06 10:51:33.761: I/End:(5026): END_TAG: option1
12-06 10:51:33.761: I/End:(5026): START_TAG: option2
12-06 10:51:33.761: I/End:(5026): TEXT: Moon
12-06 10:51:33.761: I/End:(5026): END_TAG: option2
12-06 10:51:33.761: I/End:(5026): START_TAG: option3
12-06 10:51:33.761: I/End:(5026): TEXT: Sun
12-06 10:51:33.761: I/End:(5026): END_TAG: option3
12-06 10:51:33.761: I/End:(5026): START_TAG: option4
12-06 10:51:33.761: I/End:(5026): TEXT: North Star
12-06 10:51:33.761: I/End:(5026): END_TAG: option4
12-06 10:51:33.761: I/End:(5026): START_TAG: answer
12-06 10:51:33.761: I/End:(5026): TEXT: Sun
12-06 10:51:33.761: I/End:(5026): END_TAG: answer
12-06 10:51:33.761: I/End:(5026): END_TAG: mchoice
12-06 10:51:33.761: I/End:(5026): START_TAG: mchoice
12-06 10:51:33.761: I/End:(5026): START_TAG: question
12-06 10:51:33.761: I/End:(5026): TEXT: A number which is not prime
12-06 10:51:33.761: I/End:(5026): END_TAG: question
12-06 10:51:33.761: I/End:(5026): START_TAG: id
12-06 10:51:33.761: I/End:(5026): TEXT: 4
12-06 10:51:33.761: I/End:(5026): END_TAG: id
12-06 10:51:33.761: I/End:(5026): START_TAG: option1
12-06 10:51:33.761: I/End:(5026): TEXT: 31
12-06 10:51:33.761: I/End:(5026): END_TAG: option1
12-06 10:51:33.761: I/End:(5026): START_TAG: option2
12-06 10:51:33.761: I/End:(5026): TEXT: 61
12-06 10:51:33.761: I/End:(5026): END_TAG: option2
12-06 10:51:33.761: I/End:(5026): START_TAG: option3
12-06 10:51:33.761: I/End:(5026): TEXT: 71
12-06 10:51:33.761: I/End:(5026): END_TAG: option3
12-06 10:51:33.761: I/End:(5026): START_TAG: option4
12-06 10:51:33.761: I/End:(5026): TEXT: 91
12-06 10:51:33.761: I/End:(5026): END_TAG: option4
12-06 10:51:33.761: I/End:(5026): START_TAG: answer
12-06 10:51:33.761: I/End:(5026): TEXT: 91
12-06 10:51:33.761: I/End:(5026): END_TAG: answer
12-06 10:51:33.761: I/End:(5026): END_TAG: mchoice
12-06 10:51:33.761: I/End:(5026): START_TAG: mchoice
12-06 10:51:33.761: I/End:(5026): START_TAG: question
12-06 10:51:33.761: I/End:(5026): TEXT: Which is correct?
12-06 10:51:33.761: I/End:(5026): END_TAG: question
12-06 10:51:33.761: I/End:(5026): START_TAG: id
12-06 10:51:33.761: I/End:(5026): TEXT: 5
12-06 10:51:33.761: I/End:(5026): END_TAG: id
12-06 10:51:33.761: I/End:(5026): START_TAG: option1
12-06 10:51:33.761: I/End:(5026): TEXT: Foreine
12-06 10:51:33.761: I/End:(5026): END_TAG: option1
12-06 10:51:33.761: I/End:(5026): START_TAG: option2
12-06 10:51:33.761: I/End:(5026): TEXT: Fariegn
12-06 10:51:33.761: I/End:(5026): END_TAG: option2
12-06 10:51:33.761: I/End:(5026): START_TAG: option3
12-06 10:51:33.761: I/End:(5026): TEXT: Foreig
12-06 10:51:33.761: I/End:(5026): END_TAG: option3
12-06 10:51:33.761: I/End:(5026): START_TAG: option4
12-06 10:51:33.761: I/End:(5026): TEXT: Foreign
12-06 10:51:33.761: I/End:(5026): END_TAG: option4
12-06 10:51:33.761: I/End:(5026): START_TAG: answer
12-06 10:51:33.761: I/End:(5026): TEXT: Foreign
12-06 10:51:33.761: I/End:(5026): END_TAG: answer

我已经使用这种方法来解析 XML 文件

//local XML
         private String getEventsFromAnXML(Activity activity)
                    throws XmlPullParserException, IOException
                    {
                     StringBuffer stringBuffer = new StringBuffer();
                     Resources res = activity.getResources();
                     XmlResourceParser xpp = res.getXml(R.xml.data);
                     xpp.next();

                     int eventType = xpp.getEventType();
                     while (eventType != XmlPullParser.END_DOCUMENT)
                     {
                      if(eventType == XmlPullParser.START_DOCUMENT)
                      {
                      // stringBuffer.append("--- Start XML ---");
                       Log.i("Start:", "--- Start XML ---");
                      }
                      else if(eventType == XmlPullParser.START_TAG)
                      {
                       stringBuffer.append("\nSTART_TAG: "+xpp.getName());

                       Log.i("Start:", stringBuffer.toString());
                      }
                      else if(eventType == XmlPullParser.END_TAG)
                      {
                       stringBuffer.append("\nEND_TAG: "+xpp.getName());
                       Log.i("End:", stringBuffer.toString());
                      }
                      else if(eventType == XmlPullParser.TEXT)
                      {
                       stringBuffer.append("\nTEXT: "+xpp.getText());
                       Log.i("Text:", stringBuffer.toString());
                      }
                      eventType = xpp.next();
                     }
                     stringBuffer.append("\n--- End XML ---");
                     return stringBuffer.toString();
                    }

但是,我希望输出是这样的:

<?xml version="1.0" encoding="utf-8"?>
12-06 10:51:34.891: I/XML:(5026): <quiz>
12-06 10:51:34.891: I/XML:(5026):   <mchoice>
12-06 10:51:34.891: I/XML:(5026):     <question>Not a team sport for sure</question>
12-06 10:51:34.891: I/XML:(5026):   <id>1</id>
12-06 10:51:34.891: I/XML:(5026):     <option1>Cricket</option1>
12-06 10:51:34.891: I/XML:(5026):     <option2>Tennis</option2>
12-06 10:51:34.891: I/XML:(5026):     <option3>Rugby</option3>
12-06 10:51:34.891: I/XML:(5026):     <option4>Soccer</option4>
12-06 10:51:34.891: I/XML:(5026):   <answer>Tennis</answer>
12-06 10:51:34.891: I/XML:(5026):   </mchoice>
12-06 10:51:34.891: I/XML:(5026):   <mchoice>
12-06 10:51:34.891: I/XML:(5026):     <question>I am the biggest planet in the Solar System</question>
12-06 10:51:34.891: I/XML:(5026):   <id>2</id>
12-06 10:51:34.891: I/XML:(5026):     <option1>Saturn</option1>
12-06 10:51:34.891: I/XML:(5026):     <option2>Jupiter</option2>
12-06 10:51:34.891: I/XML:(5026):     <option3>Neptune</option3>
12-06 10:51:34.891: I/XML:(5026):     <option4>Pluto</option4>
12-06 10:51:34.891: I/XML:(5026):     <answer>Jupiter</answer>
12-06 10:51:34.891: I/XML:(5026):   </mchoice>
12-06 10:51:34.891: I/XML:(5026):   <mchoice>
12-06 10:51:34.891: I/XML:(5026):     <question>I am the closest star to the earth</question>
12-06 10:51:34.891: I/XML:(5026):   <id>3</id>
12-06 10:51:34.891: I/XML:(5026):     <option1>Milky way</option1>
12-06 10:51:34.891: I/XML:(5026):     <option2>Moon</option2>
12-06 10:51:34.891: I/XML:(5026):     <option3>Sun</option3>
12-06 10:51:34.891: I/XML:(5026):     <option4>North Star</option4>
12-06 10:51:34.891: I/XML:(5026):     <answer>Sun</answer>
12-06 10:51:34.891: I/XML:(5026):   </mchoice>
12-06 10:51:34.891: I/XML:(5026):   <mchoice>
12-06 10:51:34.891: I/XML:(5026):     <question>A number which is not prime</question>
12-06 10:51:34.891: I/XML:(5026):   <id>4</id>
12-06 10:51:34.891: I/XML:(5026):     <option1>31</option1>
12-06 10:51:34.891: I/XML:(5026):     <option2>61</option2>
12-06 10:51:34.891: I/XML:(5026):     <option3>71</option3>
12-06 10:51:34.891: I/XML:(5026):     <option4>91</option4>
12-06 10:51:34.891: I/XML:(5026):     <answer>91</answer>
12-06 10:51:34.891: I/XML:(5026):    </mchoice>
12-06 10:51:34.891: I/XML:(5026):   <mchoice>
12-06 10:51:34.891: I/XML:(5026):     <question>Which is correct?</question>
12-06 10:51:34.891: I/XML:(5026):   <id>5</id>
12-06 10:51:34.891: I/XML:(5026):     <option1>Foreine</option1>
12-06 10:51:34.891: I/XML:(5026):     <option2>Fariegn</option2>
12-06 10:51:34.891: I/XML:(5026):     <option3>Foreig</option3>
12-06 10:51:34.891: I/XML:(5026):     <option4>Foreign</option4>
12-06 10:51:34.891: I/XML:(5026):     <answer>Foreign</answer>
12-06 10:51:34.891: I/XML:(5026):      </mchoice>
12-06 10:51:34.891: I/XML:(5026): </quiz>

我哪里错了?如果我可以将 xml 数据存储在 String 变量中,那将是最好的。

4

1 回答 1

1

尝试这个 :

            int eventType = parser.getEventType();

            done = false;

            while (eventType != XmlPullParser.END_DOCUMENT && !done) {
                String name = null;
                switch (eventType) {
                case XmlPullParser.START_DOCUMENT:

                    break;

                case XmlPullParser.START_TAG:
                    name = parser.getName();


                    if (name.equalsIgnoreCase("mchoice")) {

                        // Initialize your list

                    } else if (list!= null) {

                        if (name.equals(question)) {

                            System.out.println("question : " + parser
                                    .nextText());

                        }

                        else if (name.equalsIgnoreCase("id")) {

                            System.out.println("id : " + parser
                                    .nextText());

                        }

                        else if (name.equalsIgnoreCase("option1")) {

                            System.out.println("option 1 : " + parser
                                    .nextText());

                        }

                        else if (name.equalsIgnoreCase("option2")) {

                            System.out.println("option 2 : " + parser
                                    .nextText());

                        }

                        else if (name.equalsIgnoreCase("option3")) {

                            System.out.println("option 3 : " + parser
                                    .nextText());

                        }

                        else if (name.equalsIgnoreCase("option4")) {

                            System.out.println("option 4 :" + parser
                                    .nextText());

                        }

                        else if (name.equalsIgnoreCase("answer")) {

                            System.out.println("answer :" + parser
                                    .nextText());

                        }                       
                    }

                    // -------------

                    break;

                case XmlPullParser.END_TAG:
                    name = parser.getName();
                    if (name.equalsIgnoreCase("mchoice")
                            && list != null) {  

                        // add elements in list

                    } else if (name.equalsIgnoreCase("quiz")) {


                        done = true;
                    }
                    break;
                }
                eventType = parser.next();
            }
于 2012-12-06T13:55:31.043 回答