1

XML 入力 : http://maps.googleapis.com/maps/api/geocode/xml?address=Connaught+Place,+New%20+Delhi,+India&sensor=true

JAXB を使用して、この XML (上記のリンク) からデータを取得しようとしています。私の目標は、特定の住所の緯度と経度を取得することです。

これらのクラスを作成しましたが、バインディング アノテーションがありません。これを入手するのを手伝ってください、事前に感謝します


編集:コードが機能するようになったので、回答に新しいコードを貼り付けます


public class JAXBExample {
    public static void main(String[] args) {

     try {

        File file = new File("GeoLocation.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(GeocodeResponse.class);//(GeocodeResponse.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        GeocodeResponse latLong = (GeocodeResponse) jaxbUnmarshaller.unmarshal(file);

        System.out.println(latLong);
//      System.out.println(latLong.getLatitude()+ ", "+latLong.getLongitude());

      } catch (JAXBException e) {
        e.printStackTrace();
      }

    }
}

   @XmlType(propOrder={"latitude","longitude"})
public class Location {

    double lat;
    double lng;


//  @XmlElement(name="lat")
    public double getLatitude() {
        return lat;
    }

//  @XmlElement
    @XmlElement(name="lat")
    public void setLatitude(double latitude) {
        this.lat = latitude;
    }

//  @XmlElement(name="lng")
    public double getLongitude() {
        return lng;
    }

//  @XmlAttribute
    @XmlElement(name="lng")
    public void setLongitude(double longitude) {
        this.lng = longitude;
    }

}

@XmlType(propOrder={"geometry"})
public class Geometry {
    Location aLocation;

//  @XmlElement(name="location")
    public Location getaLocation() {
        return aLocation;
    }

    @XmlElement(name="location")
    public void setaLocation(Location aLocation) {
        this.aLocation = aLocation;
    }


}

@XmlType(propOrder={"result"})
public class Result {
    Geometry aGeometry;

//  @XmlElement(name="geometry")
    public Geometry getaGeometry() {
        return aGeometry;
    }

    @XmlElement(name="geometry")
    public void setaGeometry(Geometry aGeometry) {
        this.aGeometry = aGeometry;
    }
}

@XmlRootElement
public class GeocodeResponse {
    Result aResult;

    public Result getaResult() {
        return aResult;
    }

    @XmlElement(name = "result")
    public void setaResult(Result aResult) {
        this.aResult = aResult;
    }
}
4

1 に答える 1

2

コードを修正したので、ここに貼り付けて、誰かが完全に使用できるようにします。

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class JAXBxmlToJavaCordinate {
    public static void main(String[] args) {

     try {

        File file = new File("resources/GeoLocation.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(GeocodeResponse.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        GeocodeResponse geoResponse = (GeocodeResponse) jaxbUnmarshaller.unmarshal(file);

        Location latLong = geoResponse.getaResult().getGeometry().getLocation();

        System.out.println(latLong.getLatitude()+ ", "+latLong.getLongitude());

      } catch (JAXBException e) {
        e.printStackTrace();
      }

    }
}

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="GeocodeResponse")
public class GeocodeResponse {
    Result result;

    public Result getaResult() {
        return result;
    }

    @XmlElement(name = "result")
    public void setResult(Result result) {
        this.result = result;
    }
}

import javax.xml.bind.annotation.*;

@XmlRootElement
public class Result {
    Geometry geometry;

    public Geometry getGeometry() {
        return geometry;
    }

    @XmlElement(name="geometry")
    public void setGeometry(Geometry geometry) {
        this.geometry = geometry;
    }
}

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="geometry")
public class Geometry {
    Location location;

    public Location getLocation() {
        return location;
    }

    @XmlElement(name="location")
    public void setaLocation(Location location) {
        this.location = location;
    }

}

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlType(propOrder={"latitude","longitude"})
public class Location {

    double lat;
    double lng;


    public double getLatitude() {
        return lat;
    }

    @XmlElement(name="lat")
    public void setLatitude(double latitude) {
        this.lat = latitude;
    }

    public double getLongitude() {
        return lng;
    }

    @XmlElement(name="lng")
    public void setLongitude(double longitude) {
        this.lng = longitude;
    }

}
于 2013-02-10T16:59:47.430 に答える