2013-03-26 73 views
0

荫获取数据,但我想组一组的形式,一些特定的数据,如在机场列表即获取XML数据,并存储在数组列表的形式

肯尼迪在所有的纬度,经度,机场的名称,代码必须与其他机场类似。请帮我解决问题。

主要的Java类:

public String getAirportListHTTPURL(String mAirportLocation,Double airportLatitude, Double airportLongitude, String mAirlineCode, boolean mAirport2, String mAddress2, String mCity2, String mState2, String mCountry2) { 
     String url = CarmelURLConstants.LIVE_URL.toString(); 
     URL u = new URL(url); 
     URLConnection uc = u.openConnection(); 
     HttpURLConnection http = (HttpURLConnection) uc; 

     http.setDoOutput(true); 
     http.setDoInput(true); 
     http.setRequestMethod("POST"); 
     http.setRequestProperty("Content-type", "text/xml; charset=utf-8");  

     String xmldata = Here i used the xml format Syntax data 
     System.out.println(xmldata); 
     OutputStream out = http.getOutputStream(); 
     Writer wout = new OutputStreamWriter(out); 
     wout.write(xmldata); 
     wout.flush(); 
     wout.close(); 
     // Reading Data from the server using getInputStream 
     BufferedReader rd = new BufferedReader(new InputStreamReader(http.getInputStream())); 
     System.out.println("code..."+http.getResponseCode()); 

     String result, responsedata = " "; 
     RequestName = "AirportlistByPickUpAddress Request:-"; 
     ResponseName = "AirportlistByPickupAddress Response:-"; 
     while ((result=rd.readLine()) != null) { 
      System.out.println(result"); 
      responsedata = result; 

      // Method to parse in SAX Parser 
     mParsedValue = ParseAirportLocationResponse(result); 
     } 
     // updateTraceFile(xmldata, responsedata,RequestName,ResponseName); 
     }catch(Exception e){ 
      mParsedValue = e.getMessage(); 
      e.printStackTrace(); 
     } 
     return mParsedValue; 
    } 

    private String ParseAirportLocationResponse(String result) { 
     try { 

      // Create a XMLReader from SAXParser 
      XMLReader mXmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 
      AirportHandler Airportlist = new AirportHandler(); 

      // Apply Handler to XML Reader 
      mXmlReader.setContentHandler(Airportlist); 
      // Start the Process to Parse 
      InputSource is = new InputSource(new StringReader(result)); 
      mXmlReader.parse(is); 
     } catch (Exception e) { 
      System.out.println(e); 

     } 
      return result;// Get the Parsed Data 

    } 

在XMLSETTERS CLASS:

public class XMLGettersSetters { 
public static ArrayList<String> airportCode = new ArrayList<String>(); 
public static ArrayList<String> airportName = new ArrayList<String>(); 
public static ArrayList<String> latitude = new ArrayList<String>(); 
public static ArrayList<String> longitude = new ArrayList<String>(); 
public ArrayList<String> getlongitude() { 
    return longitude; 
} 
public void setlongitude(String longitude) { 
    this.longitude.add(longitude); 
    Log.i("This is the longitude:", longitude); 
} 
public ArrayList<String> getairportCode() { 
    return airportCode; 
} 

public void setairportCode(String airportCode) { 
    this.airportCode.add(airportCode); 
    Log.i("This is the airportCode:", airportCode); 
} 
public ArrayList<String> getairportName() { 
    return airportName; 
} 
public void setairportName(String airportName) { 
    this.airportName.add(airportName); 
    Log.i("This is the airportName:", airportName); 
} 
public ArrayList<String> getlatitude() { 
    return latitude; 
} 
public void setlatitude(String latitude) { 
    this.latitude.add(latitude); 
    Log.i("This is the latitude:", latitude); 
} 

}

在机场处理程序类:

public class AirportHandler extends DefaultHandler { 
String elementValue = null; 
Boolean elementOn = false; 
public static XMLGettersSetters data = null; 

public static XMLGettersSetters getXMLData() { 
    return data; 
} 

public static void setXMLData(XMLGettersSetters data) { 
    AirportHandler.data = data; 
} 
@Override 
public void startElement(String uri, String localName, String qName, 
     Attributes attributes) throws SAXException { 

    elementOn = true; 

    if (localName.equals("getAirportListByPickUpAddressResponse")) 
    { 
     data = new XMLGettersSetters(); 
    } 
    else if (localName.equals("airportList")) { 

    } 
} 

/** 
* This will be called when the tags of the XML end. 
**/ 
@Override 
public void endElement(String uri, String localName, String qName) 
     throws SAXException { 

    elementOn = false; 

    /** 
    * Sets the values after retrieving the values from the XML tags 
    * */ 
    if (localName.equalsIgnoreCase("airportCode")) 
     data.setairportCode(elementValue); 
    else if (localName.equalsIgnoreCase("airportName")) 
     data.setairportName(elementValue); 
    else if (localName.equalsIgnoreCase("latitude")) 
     data.setlatitude(elementValue); 
    else if (localName.equalsIgnoreCase("longitude")) 
     data.setlongitude(elementValue); 

} 

/** 
* This is called to get the tags value 
**/ 
@Override 
public void characters(char[] ch, int start, int length) 
     throws SAXException { 

    if (elementOn) { 
     elementValue = new String(ch, start, length); 
     elementOn = false; 
    } 

} 
} 
+0

需要将XML字符串转换为ArrayList的右后! – Janmejoy 2013-03-26 07:45:19

+0

是啊,我得到的XML数据,但我想这些数据在单独的阵列@Janmejoy。 – Danny 2013-03-26 08:37:45

+0

你可以使用字典,它会很容易! – Janmejoy 2013-03-26 08:38:58

回答

1

执行XML解析尝试这样

ArrayList<String> aList = new ArrayList<String>(); 

aList.add("your string"); 
相关问题