我正在尝试使用JAXB从此XML(上面的链接)获取数据。我的gole是获取给定地址的纬度和经度。
我已经创建了这些类但缺少绑定注释。 帮助得到这个, 提前致谢
编辑:现在我的代码正常运行,所以我在这里粘贴了答案中的新代码
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;
}
}
答案 0 :(得分: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;
}
}