我的输入XML字符串包含条目列表。当我使用jackson xmlmapper将其反序列化为对象时,我看到列表中只有一个项目即将到来。父元素已被定义为POJO中的通用对象。
xml string(ItemList包含3个项目):
<msg>
<head>
<Client>MyClient</Client>
<RoutingArea>Test</RoutingArea>
<ServerId>ABCXYZ123</ServerId>
</head>
<body>
<UserDetailResponse>
<UserDetail>
<Customer>
<CustomerId>1456342711975</CustomerId>
<BusinessUnit>TEST0000</BusinessUnit>
<Name>
<Salutation>U</Salutation>
<First>TROPICAL TAN</First>
</Name>
<Privacy>Y</Privacy>
</Customer>
<ItemList>
<Count>3</Count>
<Item>
<ServiceIdentifier>000001</ServiceIdentifier>
<BillingIdentifier>000001</BillingIdentifier>
</Item>
<Item>
<ServiceIdentifier>000002</ServiceIdentifier>
<BillingIdentifier>000002</BillingIdentifier>
</Item>
<Item>
<ServiceIdentifier>000003</ServiceIdentifier>
<BillingIdentifier>000003</BillingIdentifier>
</Item>
</ItemList>
</UserDetail>
</UserDetailResponse>
</body>
</msg>
Java代码:
private final static XmlMapper mapper = new XmlMapper();
public static <T> T getXmlObject(String xml, Class<T> cls) throws IOException {
return mapper.readValue(xml, cls);
}
public static void main(String[] args) throws Exception {
String xmlString = "<msg><head><Client>MyClient</Client><RoutingArea>Test</RoutingArea><ServerId>ABCXYZ123</ServerId></head><body><UserDetailResponse><UserDetail><Customer><CustomerId>1456342711975</CustomerId><BusinessUnit>TEST0000</BusinessUnit><Name><Salutation>U</Salutation><First>TROPICAL TAN</First></Name><Privacy>Y</Privacy></Customer><ItemList><Count>3</Count><Item><ServiceIdentifier>000001</ServiceIdentifier><BillingIdentifier>000001</BillingIdentifier></Item><Item><ServiceIdentifier>000002</ServiceIdentifier><BillingIdentifier>000002</BillingIdentifier></Item><Item><ServiceIdentifier>000003</ServiceIdentifier><BillingIdentifier>000003</BillingIdentifier></Item></ItemList></UserDetail></UserDetailResponse></body></msg>";
JacksonXmlModule jacksonXmlModule = new JacksonXmlModule();
jacksonXmlModule.setDefaultUseWrapper(false);
MyResponse myResponse = getXmlObject(xmlString, MyResponse.class);
System.out.println("XML Object: \n" + myResponse.toString());
ObjectMapper mapper = new ObjectMapper();
String str = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(myResponse);
System.out.println("JSON : \n" + str);
}
POJO:
public class MyResponse {
private Object head;
private Object body;
public Object getHead() {
return head;
}
public void setHead(Object head) {
this.head = head;
}
public Object getBody() {
return body;
}
public void setBody(Object body) {
this.body = body;
}
}
虽然输入xml字符串中的ItemList下有3个项目,但结果对象只包含第3个项目。
结果:
JSON :
{
"head" : {
"Client" : "MyClient",
"RoutingArea" : "Test",
"ServerId" : "ABCXYZ123"
},
"body" : {
"UserDetailResponse" : {
"UserDetail" : {
"Customer" : {
"CustomerId" : "1456342711975",
"BusinessUnit" : "TEST0000",
"Name" : {
"Salutation" : "U",
"First" : "TROPICAL TAN"
},
"Privacy" : "Y"
},
"ItemList" : {
"Count" : "3",
"Item" : {
"ServiceIdentifier" : "000003",
"BillingIdentifier" : "000003"
}
}
}
}
}
}
答案 0 :(得分:0)
如果没有声明head
和body
的实际类型,您的示例将无效。简单Object
会导致body
绑定为Map
,并且使用相同名称的重复元素,只有最后一个元素会被保留。
潜在的问题是XML在数组和对象之间没有区别(与JSON不同),解决差异的唯一方法是使用来自Java类的信息。因此,您需要使用List
或数组类型来将事物绑定为数组。