0

I checked SO for the xmlpullparser exception but it's giving me others questions with Android and SOUP. I am using J2me and normal HTTPrequest to get my XML and I am using kXMl to parser the xml text. Below is the code that I am working on. And above it is more parsing code and they work perfectly.

if (parser.getName().equals("comments")) {

                        event = parser.next();
                        boolean flag = false;
                        if (parser.getName().equals("comment")) {
                            flag = true;
                            System.out.println("Flag is true");
                        }
                        while (flag) {
                            event = parser.next();
                            Questioncomments.addComponent(new Label(parser.nextText()));
                            event = parser.next();
                            System.out.println("Inside the While");
                            if (!parser.getName().equals("comment")) {
                                flag = false;
                                System.out.println("Flag is false");
                            }
                        }
                        Questioncomments.repaint();
                    }

XML I am sending this side - <comments><comment>Awesome Question @dulitha<idComment></idComment></comment></comments>

The error is -

org.xmlpull.v1.XmlPullParserException: precondition: START_TAG (position:TEXT Awesome Question...@1:399 in java.io.InputStreamReader@f828ed68) at org.kxml2.io.KXmlParser.exception(+47) at org.kxml2.io.KXmlParser.nextText(+14) at com.petmill.mobile.view.qanda.QuestionCanvas.setData(QuestionCanvas.java:189) at com.petmill.mobile.view.qanda.QuestionsList$5$1$1.actionPerformed(QuestionsList.java:119)

The error comes up at the line where I am trying to get the text - parser.nextText(). How can I parse the xml to get the data required... Thanks in advance.

Chan
  • 2,601
  • 6
  • 28
  • 45
  • It is strange, logging parser.nextText() using Log.i("checks",parser.nextText()); nicely outputs the string we want. However, it is impossible to put the string obtained into a string variable. I have tried the posted solution but without luck! – Shailen Aug 28 '12 at 16:25

2 Answers2

1

It looks like you are not on the START_TAG event when you call parser.nextText(). Check that you are on a START_TAG event when you call parser.nextText() with the parser.getEventType(). I suspect that you have some whitespace between <comments> and <comment> tag and therefore your parser is not at the event that you expect it to be.

Perhaps you should also consider a safer approach for parsing this xml.

Gorkem Ercan
  • 3,220
  • 1
  • 18
  • 26
0
<comments>
          <comment>Awesome Question @dulitha
            <idComment></idComment>
           </comment>
</comments>

this is not valid xml

Nirmal- thInk beYond
  • 11,847
  • 8
  • 35
  • 46