1

我是 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 以及它在这个函数中的描述。请帮帮我。

4

2 回答 2

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

        }

希望这可以帮助

于 2013-02-05T08:45:07.143 回答
1

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

于 2013-02-05T08:39:42.137 回答