我正在尝试使用jaxb从java打印xml。
我的代码如下:
public class MyXMLGenerator {
public static void main(String[] args) throws Exception {
JAXBPojo jaxbPojo = new JAXBPojo();
jaxbPojo.setName("setName");
jaxbPojo.setId(345);
JAXBContext jc = JAXBContext.newInstance(JAXBPojo.class);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(jaxbPojo, System.out);
}
}
@XmlRootElement
public class JAXBPojo {
private int id;
private String name;
private Date dob;
@XmlElement(namespace="ddd")
private Address address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public Address getAddress() {
return address;
}
public void setAddress() {
address = new Address();
address.setHouseName("setHouseName");
address.setStateName("setStateName");
address.setLocalityName("setLocalityName");
address.setAreaName("setAreaName");
address.setCityName("setCityName");
}
}
@XmlElement
public class Address {
private String houseName;
private String streetName;
private String localityName;
private String areaName;
private String cityName;
private String districtName;
private String stateName;
private String countryName;
public String getHouseName() {
return houseName;
}
public void setHouseName(String houseName) {
this.houseName = houseName;
}
public String getStreetName() {
return streetName;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public String getLocalityName() {
return localityName;
}
public void setLocalityName(String localityName) {
this.localityName = localityName;
}
public String getAreaName() {
return areaName;
}
public void setAreaName(String areaName) {
this.areaName = areaName;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getDistrictName() {
return districtName;
}
public void setDistrictName(String districtName) {
this.districtName = districtName;
}
public String getStateName() {
return stateName;
}
public void setStateName(String stateName) {
this.stateName = stateName;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
}
我明白了:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxbPojo xmlns:ns2="ddd">
<id>345</id>
<name>setName</name>
</jaxbPojo>
但是当我在main方法中为地址类提供值时,我确实在输出中得到了这样的地址:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxbPojo>
<address>
<areaName>setAreaName</areaName>
<cityName>setCityName</cityName>
<houseName>setHouseName</houseName>
<localityName>setLocalityName</localityName>
<stateName>setStateName</stateName>
</address>
<id>345</id>
<name>setName</name>
</jaxbPojo>
我需要做些什么更改才能让它在当前的情况下工作,我在jaxbpojo中获取地址类的值?
提前致谢...
答案 0 :(得分:0)
我不确定你想要什么,但我会将setAddress更改为:
public void setAddress(Address address) {
this.address = address;
}
主要:
public static void main(String[] args) throws Exception {
JAXBPojo jaxbPojo = new JAXBPojo();
jaxbPojo.setName("setName");
jaxbPojo.setId(345);
Address address = new Address();
address.setHouseName("setHouseName");
address.setStateName("setStateName");
address.setLocalityName("setLocalityName");
address.setAreaName("setAreaName");
address.setCityName("setCityName");
jaxbPojo.setAddress(address);
JAXBContext jc = JAXBContext.newInstance(JAXBPojo.class);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(jaxbPojo, System.out);
}