将 Java 对象映射到 @XmlElement 值
Map Java Object to @XmlElement Value
当我 运行 JAXBxmlToJava 时,LocationType Set 变为空。任何人都可以建议如何将 xml 的类型组件映射到 Java 的 LocationType 对象。
我有以下 arc-response.xml:-
<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
}
位置类型:-
@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
将文本节点(例如 'neighborhood')映射到自定义 class 中的字段。
当我测试你的代码时,Set<LocationType>
不是 null
- 里面有两个 LocationType
,但是 它们的 type
字段 为 null
。我不确定这是否是您在问题中的意思。
当我将 LocationType
class 更改为
@XmlRootElement(name = "type")
public class LocationType {
private Integer locationTypeId;
@XmlValue
private String type;
private String status;
// Setter Getter
}
成功了。
当我 运行 JAXBxmlToJava 时,LocationType Set 变为空。任何人都可以建议如何将 xml 的类型组件映射到 Java 的 LocationType 对象。 我有以下 arc-response.xml:-
<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
}
位置类型:-
@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
将文本节点(例如 'neighborhood')映射到自定义 class 中的字段。
当我测试你的代码时,Set<LocationType>
不是 null
- 里面有两个 LocationType
,但是 它们的 type
字段 为 null
。我不确定这是否是您在问题中的意思。
当我将 LocationType
class 更改为
@XmlRootElement(name = "type")
public class LocationType {
private Integer locationTypeId;
@XmlValue
private String type;
private String status;
// Setter Getter
}
成功了。