3

I am parsing some xml and now I am trying to get the text values of some of the nodes. Here is the xml

<menu>
<day name="monday">
    <meal name="BREAKFAST">
        <counter name="Bread">
           <dish>
               <name>Plain Bagel</name>
           </dish>
        <counter/>
    <meal/>
<day/>
<day name="tuesday">
    <meal name="LUNCH">
        <counter name="Other">
           <dish>
               <name>Cheese Bagel</name>
           </dish>
        <counter/>
    <meal/>
<day/>

And now I am using XMLPullParser and its working except in the get text area.

So in the case to get text I have this:

case XmlResourceParser.TEXT:
            itemsArray.add(xmlData.getText());
             Log.i(TAG, "a"+xmlData.getText()+"b");
      break;

So it is adding the items Plain Bagel and Cheese Bagel great, but then in the onProgressUpdate method, when I log the result I see this:

[
,
,
, Plain Bagel,
,
,
, Cheese Bagel]

I though these were just \n characters, So I tried this but I still got the same result.

if (!xmlData.getText().equals("\n")) {...

So how can I get rid of these empty lines or whatever they are?

Thanks for the help in advance.

When I Log this

Log.i(TAG, xmlData.getText().length() + "");
Log.i(TAG, xmlData.getText());

I Get this as a result

4
12-15 06:28:58.868    5849-5880/com.spencer.ueat I/DiningItemsActivity﹕ [ 12-15 06:28:58.868  5849: 5880 I/DiningItemsActivity ]
    5
12-15 06:28:58.868    5849-5880/com.spencer.ueat I/DiningItemsActivity﹕ [ 12-15 06:28:58.868  5849: 5880 I/DiningItemsActivity ]
    1
12-15 06:28:58.868    5849-5880/com.spencer.ueat I/DiningItemsActivity﹕ [ 12-15 06:28:58.868  5849: 5880 I/DiningItemsActivity ]
    34
12-15 06:28:58.869    5849-5880/com.spencer.ueat I/DiningItemsActivity﹕ Vegetable Samosa with Yogurt Sauce
12-15 06:28:58.869    5849-5880/com.spencer.ueat I/DiningItemsActivity﹕ 5

Confuses me??

2
  • why don't you write out all the data in hex and see what it is that you have there? Commented Dec 15, 2014 at 6:31
  • stackoverflow.com/questions/923863/… Commented Dec 15, 2014 at 6:32

2 Answers 2

1

you should trim the text. because parser gives you string with space and also string with \n for every string between tags

case XmlResourceParser.TEXT:
if (xmlData.getText().trim().length() > 0) 
{
   itemsArray.add(xmlData.getText());
   Log.i(TAG, "a"+xmlData.getText()+"b");
}
break;
Sign up to request clarification or add additional context in comments.

1 Comment

you are getting all text between tags. I mean, for instance dish, and counter tag. there is space also there is linefeed between these tags.
0

You can something like:

if (xmlData.getText().trim().length > 0) {...

4 Comments

did you try to get the length of each element and see what is it?
no trying that now would oh been a logical next stop sorry its getting late here
see addition for the lengths, could you explain the itemsArray.filter more
But any change you know why those spaces and \n are being added?

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.