gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:49:50 24 4
gpt4 key购买 nike

<分区>

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com