使用Jackson XML Mapper将XML字符串的XML字符串在对象中进行挑选XML字符串后,项目的完整列表不会显



我的输入XML字符串包含条目列表。当我使用Jackson XMLMAPPER将其验证为对象时,我会看到列表中的一个项目即将到来。父元素已定义为POJO中的通用对象。

XML字符串(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字符串中的项目列表下有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"
          }
        }
      }
    }
  }
}

如果不声明headbody的实际类型,您的示例将无法使用。普通的Object将导致body被绑定为Map,并且具有相同名称的重复元素,只有最后一个最终被保留。潜在的问题是XML在数组和对象之间没有区别(与JSON不同),而解决差异的唯一方法是使用Java类中的信息。因此,您需要使用List或数组类型的东西以将事物绑定为数组。

最新更新