1

can you suggest a simple example to load local XML data in List view.

2
  • Did you check the documentation before posting the question ? Commented Sep 15, 2012 at 6:48
  • @NishitPatel If my answer is helping you then please accept it. Commented Dec 6, 2012 at 8:25

1 Answer 1

3

First Put your XML file into raw folder and then Use below code for parse this XML file using Dom Parser.

public class XMLParsingDOMExample extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /** Create a new layout to display the view */
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(1);

        /** Create a new textview array to display the results */
        TextView id[];
        TextView imageurl[];

        try {

            InputStream is = res.openRawResource(R.raw.localxmlfileName);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(is));
            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("item");

            /** Assign textview array lenght by arraylist size */
            id = new TextView[nodeList.getLength()];
            imageurl = new TextView[nodeList.getLength()];

            for (int i = 0; i < nodeList.getLength(); i++) {

                Node node = nodeList.item(i);

                id[i] = new TextView(this);
                imageurl[i] = new TextView(this);

                Element fstElmnt = (Element) node;
                NodeList idList = fstElmnt.getElementsByTagName("item_id");
                Element idElement = (Element) idList.item(0);
                idList = idElement.getChildNodes();
                id[i].setText("id is = " + ((Node) idList.item(0)).getNodeValue());

                NodeList imageurlList = fstElmnt.getElementsByTagName("item_image");
                Element imageurlElement = (Element) imageurlList.item(0);
                imageurlList = imageurlElement.getChildNodes();
                imageurl[i].setText("imageurl is = " + ((Node) imageurlList.item(0)).getNodeValue());

                layout.addView(id[i]);
                layout.addView(imageurl[i]);
            }
        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }

        /** Set the layout view to display */
        setContentView(layout); 
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.