Here you can find the source of ReadCityCode(Context context, String cityname)
public static String ReadCityCode(Context context, String cityname)
//package com.java2s; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import android.content.Context; public class Main { public static String ReadCityCode(Context context, String cityname) { String returnString = ""; DocumentBuilderFactory docBuilderFactory = null; DocumentBuilder docBuilder = null; Document doc = null;/*from www .ja v a 2s.c o m*/ try { docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilder = docBuilderFactory.newDocumentBuilder(); doc = docBuilder.parse(context.getAssets().open("citys.xml")); Element root = doc.getDocumentElement(); NodeList nodeList = root.getElementsByTagName("city"); for (int i = 0, len = nodeList.getLength(); i < len; i++) { Element nd = (Element) nodeList.item(i); String temp = nd.getAttribute("name"); if (temp.equals(cityname)) { returnString = nd.getAttribute("code"); break; } } } catch (Exception e) { e.printStackTrace(); returnString = ""; } return returnString; } }