XML adds data to ArrayList <String>

I am parsing some xml and now I am trying to get the text values ​​of some 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 it works except in the area of ​​getting text.

So in case of getting text, I have this:

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

      

So it adds Plain Bagel and Cheese Bagel elements fine, but then in the onProgressUpdate method, when I log the result, I see this:

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

      

I though it was only \ n characters, so I tried this, but I still have the same result.

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

      

So how can I get rid of those blank lines or whatever they are?

Thanks for the help in advance.

When I write this message

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

+3


source to share


2 answers


you have to crop the text. because the parser gives you a line with a space, as well as a line with \ n for each line between tags



case XmlResourceParser.TEXT:
if (xmlData.getText().trim().length() > 0) 
{
   itemsArray.add(xmlData.getText());
   Log.i(TAG, "a"+xmlData.getText()+"b");
}
break;

      

+1


source


You can do something like:



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

      

0


source







All Articles