0

我正在获取数据,但我想以组的形式对一些特定数据进行分组,例如在机场列表中

JFK 机场的所有纬度、经度、名称、代码必须与其他机场类似地制作一个数组。请帮我解决这个问题。

主要的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 类中:

 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);
}

}

在 Airport 处理程序类中:

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

}
}
4

1 回答 1

1

执行 XML 解析后尝试这样

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

aList.add("your string");
于 2013-03-26T08:46:29.573 回答