使用JAXB将XML转换为Java对象时,删除多余的嵌套 [英] Remove extra nesting when converting XML to Java object using JAXB
本文介绍了使用JAXB将XML转换为Java对象时,删除多余的嵌套的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将以下XML(1)转换为Java对象(2).我希望输出为(2a).但我注意到还有更多有用的缩进.我的班级是(3),(4),(5).
I am trying to convert the following XML(1) to java object (2). I want the output to be (2a). But I am noticing that there is more indentation that useful. My classes are (3), (4), (5).
有没有一种方法可以不手动映射对象?
Is there a way without manually mapping the object ?
I am following this question but it didnt work for me (Answer 2): Unmarshalling nested list of xml items using JAXB
(1)
<CustomerObject
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<address>
<Address>
<addressLine1>1111</addressLine1>
<addressType>1111</addressType>
<city>1111</city>
<country>US</country>
<countryDescription>UNITED STATES OF AMERICA</countryDescription>
<state>1111</state>
<zipCode>1111</zipCode>
</Address>
<Address>
<addressLine1>1111</addressLine1>
<addressType>1111</addressType>
<city>1111</city>
<country>US</country>
<countryDescription>UNITED STATES OF AMERICA</countryDescription>
<state>1111</state>
<zipCode>1111</zipCode>
</Address>
</address>
<businessPhoneNumber>1111</businessPhoneNumber>
<contact i:nil="true"/>
<dateAccountOpened>1111</dateAccountOpened>
<dateOfBirth>11114</dateOfBirth>
<firstName>1111</firstName>
<homePhoneNumber>1111</homePhoneNumber>
<lastName>FIEDLER</lastName>
<namePrefix i:nil="true"/>
<nameSuffix i:nil="true"/>
<statusMessage/>
</CustomerObject>
(2)
{
"businessPhoneNumber": "1111",
"contact": "",
"dateAccountOpened": "1111",
"dateOfBirth": "1111",
"firstName": "1111",
"homePhoneNumber": "1111",
"lastName": "1111",
"namePrefix": "",
"nameSuffix": "",
"statusMessage": "",
"address": {
"address": [
{
"addressLine1": "1111",
"addressType": "1111",
"city": "1111",
"country": "1111",
"countryDescription": "UNITED STATES OF AMERICA",
"state": "1111",
"zipCode": "1111"
},
{
"addressLine1": "1111",
"addressType": "1111",
"city": "1111",
"country": "1111",
"countryDescription": "UNITED STATES OF AMERICA",
"state": "1111",
"zipCode": "1111"
}
]
}
}
(2a)
{
"businessPhoneNumber": "1111",
"contact": "",
"dateAccountOpened": "1111",
"dateOfBirth": "1111",
"firstName": "1111",
"homePhoneNumber": "1111",
"lastName": "1111",
"namePrefix": "",
"nameSuffix": "",
"statusMessage": "",
"address": {
{
"addressLine1": "1111",
"addressType": "1111",
"city": "1111",
"country": "1111",
"countryDescription": "UNITED STATES OF AMERICA",
"state": "1111",
"zipCode": "1111"
},
{
"addressLine1": "1111",
"addressType": "1111",
"city": "1111",
"country": "1111",
"countryDescription": "UNITED STATES OF AMERICA",
"state": "1111",
"zipCode": "1111"
}
}
}
(3)
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"businessPhoneNumber","contact", "dateAccountOpened", "dateOfBirth",
"firstName", "homePhoneNumber", "lastName", "namePrefix", "nameSuffix", "statusMessage","address"})
@XmlRootElement(name = "CustomerObject")
public class CustomerObject {
private String businessPhoneNumber;
private String contact;
private String dateAccountOpened;
private String dateOfBirth;
private String firstName;
private String homePhoneNumber;
private String lastName;
private String namePrefix;
private String nameSuffix;
private String statusMessage;
private CIFAddress address;
public CIFAddress getAddress() {
return address;
}
public void setAddress(CIFAddress address) {
this.address = address;
}
public String getBusinessPhoneNumber() {
return businessPhoneNumber;
}
public void setBusinessPhoneNumber(String businessPhoneNumber) {
this.businessPhoneNumber = businessPhoneNumber;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getDateAccountOpened() {
return dateAccountOpened;
}
public void setDateAccountOpened(String dateAccountOpened) {
this.dateAccountOpened = dateAccountOpened;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getHomePhoneNumber() {
return homePhoneNumber;
}
public void setHomePhoneNumber(String homePhoneNumber) {
this.homePhoneNumber = homePhoneNumber;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getNamePrefix() {
return namePrefix;
}
public void setNamePrefix(String namePrefix) {
this.namePrefix = namePrefix;
}
public String getNameSuffix() {
return nameSuffix;
}
public void setNameSuffix(String nameSuffix) {
this.nameSuffix = nameSuffix;
}
public String getStatusMessage() {
return statusMessage;
}
public void setStatusMessage(String statusMessage) {
this.statusMessage = statusMessage;
}
}
(4)
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
public class CIFAddress {
@XmlElement(name = "Address")
protected List<Address> Address;
public List<Address> getAddress() {
if (Address == null) {
Address = new ArrayList<Address>();
}
return this.Address;
}
}
(5)
package com.rbc.fraud.fid.RestService.Resources;
public class Address {
private String addressLine1 ;
private String addressType;
private String city;
private String country;
private String countryDescription;
private String state;
private String zipCode;
public String getAddressLine1() {
return addressLine1;
}
public void setAddressLine1(String addressLine1) {
this.addressLine1 = addressLine1;
}
public String getAddressType() {
return addressType;
}
public void setAddressType(String addressType) {
this.addressType = addressType;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCountryDescription() {
return countryDescription;
}
public void setCountryDescription(String countryDescription) {
this.countryDescription = countryDescription;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
}
推荐答案
关键是使用@XmlElementWrapper
.此处的更多信息: https://howtodoinjava.com/jaxb/xmlelementwrapper-annotation/
The key is to use @XmlElementWrapper
. More info here : https://howtodoinjava.com/jaxb/xmlelementwrapper-annotation/
这篇关于使用JAXB将XML转换为Java对象时,删除多余的嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文