Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Using XML data to display in List Item

Hi Ben!!!

I need help!! as i want to display my XML data in new View using new Activity. SHould i need to convert XML to JSON then use JSON array and JSON object to parse it.. or any way to just use grab data from XML and just display item wise.

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

There's no need to convert to JSON first - there are some great XML parsers and example code you can use! Check out the information on the Android developer site about parsing XML data.

The documentation there is a little overwhelming, so this helpful answer on StackOverflow might help you, too.

Hope this helps! If you get stuck along the way, feel free to post more questions in the forum here!

Hi Ben!!

I adopted to use XMLPullparser. but its not displaying, my code http://jsfiddle.net/navdeep/U9VWt/

And XML is

<?xml version="1.0" encoding="UTF-8"?>
<products>
    <product>
        <productname>Jeans</productname>
        <productcolor>red</productcolor>
        <productquantity>5</productquantity>
    </product>
    <product>
        <productname>Tshirt</productname>
        <productcolor>blue</productcolor>
        <productquantity>3</productquantity>
    </product>
    <product>
        <productname>shorts</productname>
        <productcolor>green</productcolor>
        <productquantity>4</productquantity>
    </product>
</products>

Please help to go through it. It will really great help.

Thanks in Advance :) Cheers Navdeep S

Ben Jakuben
Ben Jakuben
Treehouse Teacher

I plugged your code into a temp project and I think all you need to do to get the parsing working is change == to .equals() for your String comparisons:

                case XmlPullParser.START_TAG:
                    name = parser.getName();
                    if (name.equals("product")){
                        currentProduct = new Product();
                    } else if (currentProduct != null){
                        if (name.equals("productname")){
                            currentProduct.name = parser.nextText();
                        } else if (name.equals("productcolor")){
                            currentProduct.color = parser.nextText();
                        } else if (name.equals("productquantity")){
                            currentProduct.quantity= parser.nextText();
                        }
                    }
                    Log.i(TAG, "Node parsing");
                    break;

This allows the data to get printed to the log. There is a separate issue at that point with the following lines, but I'm not sure what your intent (pardon the pun) is with these lines. I just commented it out to make sure the parsing worked and was logged.

Intent intent = getIntent();
//String data = intent.getExtras().getString("title");
//textView.setText(data);

Bingo!!

It works.. Can you help me to show correct way, how i can use ArrayList<Product> products to display in my ListView using ListAdapter.

Thanks!!