Android XML Parse ReadCityCode(Context context, String cityname)

Here you can find the source of ReadCityCode(Context context, String cityname)

Description

Read City Code

Declaration

public static String ReadCityCode(Context context, String cityname) 

Method Source Code

//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;

    }
}

Related

  1. parse(String source)
  2. parse(String xml)
  3. parse(String xmlstr, Class clazz, List fields, List elements, String itemElement)
  4. parseResponse(HttpResponse response)
  5. parseTagValue(String xml, String tag, String endTag)
  6. XMLfromString(String xml)