如何从XML读取数据并显示到ListView?

时间:2013-02-05 08:34:51

标签: android xml xml-parsing

我是XML新手我想从XML读取数据我有以下格式的XML文件。

<country>
<coordinates>
    <x-y>
        <x>150</x>
        <y>160</y>
        <cities>
            <city>Mumbai</city>
            <photos>
                <image>
                    <url>Mumbai 1</url>
                    <description>
                        <english>this 1</english>
                        <arabic>this arabic 1</arabic>
                    </description>
                </image>
                <image>
                    <url>Mumbai 2</url>
                    <description>
                        <english>this 2</english>
                        <arabic>this arabic 2</arabic>
                    </description>
                </image>
                <image>
                    <url>Mumbai 3</url>
                    <description>
                        <english>this 3</english>
                        <arabic>this arabic 3</arabic>
                    </description>
                </image>
                <image>
                    <url>Mumbai 4</url>
                    <description>
                        <english>this 4</english>
                        <arabic>this arabic 4</arabic>
                    </description>
                </image>
            </photos>
        </cities>
    </x-y>
    <x-y>
        <x>155</x>
        <y>165</y>
        <cities>
            <city>Delhi</city>
            <photos>
                <image>
                    <url>Delhi 1</url>
                    <description>
                        <english>this 1</english>
                        <arabic>this arabic 1</arabic>
                    </description>
                </image>
                <image>
                    <url>Delhi 2</url>
                    <description>
                        <english>this 2</english>
                        <arabic>this arabic 2</arabic>
                    </description>
                </image>
                <image>
                    <url>Delhi 3</url>
                    <description>
                        <english>this 3</english>
                        <arabic>this arabic 3</arabic>
                    </description>
                </image>
                <image>
                    <url>Delhi 4</url>
                    <description>
                        <english>this 4</english>
                        <arabic>this arabic 4</arabic>
                    </description>
                </image>
            </photos>
        </cities>
    </x-y>
    <x-y>
        <x>160</x>
        <y>175</y>
        <cities>
            <city>Jammu</city>
            <photos>
                <image>
                    <url>Jammu 1</url>
                    <description>
                        <english>this 1</english>
                        <arabic>this arabic 1</arabic>
                    </description>
                </image>
                <image>
                    <url>Jammu 2</url>
                    <description>
                        <english>this 2</english>
                        <arabic>this arabic 2</arabic>
                    </description>
                </image>
                <image>
                    <url>Jammu 3</url>
                    <description>
                        <english>this 3</english>
                        <arabic>this arabic 3</arabic>
                    </description>
                </image>

            </photos>
        </cities>
    </x-y>
</coordinates>

我可以使用以下代码阅读城市名称:

    public static Boolean getCityList(InputStream inputstream) {
    try {
        Log.e("getCityList", "Comming");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = null;
        try {
            db = dbf.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Document doc = null;
        try {
            doc = db.parse(new InputSource(inputstream));
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        doc.getDocumentElement().normalize();

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

        if (nodeList.getLength() > 0) {
            Cities.initilize();
            for (int i = 0; i < nodeList.getLength(); i++) {

                Node node = nodeList.item(i);
                Element fstElmnt = (Element) node;
                NodeList nameList = fstElmnt.getElementsByTagName("city");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();
                Log.e("City Name : ", ""+ ((Node) nameList.item(0)).getNodeValue().toString());
                Cities cities = new Cities(((Node) nameList.item(0)).getNodeValue().toString());
                Cities.sCityList.add(cities);
                }
            }
            return true;
        }
        return false;

    } catch (ClientProtocolException e) {
        return false;
    } catch (IOException e) {
        return false;
    }
} 

以上代码从列表中读取城市。 现在我想从图像中读取url以及仅在此函数中描述它。 请帮帮我。

2 个答案:

答案 0 :(得分:1)

我建议你使用SimpleXML,它是一个用于android的轻量级解析库,它可以让你很容易地解析XML数据。

答案 1 :(得分:1)

现在您必须选择<image>作为父节点,因为<url><image>标记的子级。像这样

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

    if (nodeList.getLength() > 0) {
        Cities.initilize();
            Node node = nodeList.item(0);
            Element fstElmnt = (Element) node;
            NodeList nameList = fstElmnt.getElementsByTagName("url");
            // Store it in your city's arraylist

        }

希望这会有所帮助