gpt4 book ai didi

java - 使用 JAXB 将 XML 转换为 Java 对象时删除额外的嵌套

转载 作者:行者123 更新时间:2023-12-01 16:35:20 26 4
gpt4 key购买 nike

我正在尝试将以下 XML(1) 转换为 java 对象 (2)。我希望输出为(2a)。但我注意到还有更多有用的缩进。我的类(class)是(3)、(4)、(5)。

有没有办法不用手动映射对象?

我正在关注这个问题,但它对我不起作用(答案 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/

关于java - 使用 JAXB 将 XML 转换为 Java 对象时删除额外的嵌套,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61961856/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com