2
當我運行JAXBxmlToJava時LocationType集爲空。任何機構都可以建議如何將xml的類型組件映射到Java的LocationType對象。 我有以下圓弧response.xml: -將Java對象映射到@XmlElement值
<address_component>
<long_name>BMC Colony</long_name>
<short_name>BMC Colony</short_name>
<type>neighborhood</type>
<type>political</type>
</address_component>
而且下面的代碼,AddressComponent: -
@XmlRootElement(name = "address_component")
public class AddressComponent {
@XmlElement(name = "long_name")
private String longName;
@XmlElement(name = "short_name")
private String shortName;
@XmlElement(name = "type")
private Set<LocationType> locationTypeSet;
//Setter Getter
}
的locationType: -
@XmlRootElement(name="type")
public class LocationType {
private Integer locationTypeId;
@XmlElement(name = "type")
private String type;
private String status;
//Setter Getter
}
JAXBxmlToJava.java:-
public class JAXBxmlToJava {
public static void main(String[] args) {
try {
File file = new File("arc-response.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(AddressComponent.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
AddressComponent geoResponse = (AddressComponent) jaxbUnmarshaller.unmarshal(file);
System.out.println(geoResponse);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
是的,你讓我對「他們的類型字段爲空」。我使用@ -XmlValue,但我得到以下異常。 com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:2個IllegalAnnotationExceptions的計數 如果某個類具有@ -XmlElement屬性,則它不能具有@ -XmlValue屬性。 \t這個問題涉及到以下位置: \t \t在私人java.lang.String中com.test.core.LocationType.type \t \t在com.test.core.LocationType \t \t在公共java.util中。設置com.leaks.impl.model.geo.location.AddressComponent.getLocationTypeSet() \t \t at com.leaks.impl.model.geo.location.AddressComponent –
忽略「 - 」作爲stackoverflow不允許我寫@串。 –
你有'AddressComponent'和'LocationType'單獨的類文件嗎?你是否從'LocationType'中刪除了其他'@ XmlElement'? – Glorfindel