iqueqiorio
iqueqiorio

Reputation: 1187

XML add data to ArrayList<String>

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??

Upvotes: 3

Views: 83

Answers (2)

nitishagar
nitishagar

Reputation: 9413

You can something like:

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

Upvotes: 0

Adem
Adem

Reputation: 9429

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;

Upvotes: 1

Related Questions