当使用JAXB将XML转换为Java对象时,删除额外的嵌套。

问题描述 投票:0回答:1

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

有没有一种方法可以不手动映射对象?

我正在关注这个问题,但它对我来说没有用(答案2)。使用JAXB对嵌套的xml项进行解嵌套处理

(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;
    }
}
java xml jaxb
1个回答
0
投票

关键是使用 @XmlElementWrapper. 更多信息在这里。https:/howtodoinjava.comjaxbxmlelementwrapper-annotation。

© www.soinside.com 2019 - 2024. All rights reserved.