user2133558
user2133558

Reputation: 534

Can Jaxb automatically map objects?

I have an xml structure going like this.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE xml>
<nesProfiles>
    <group>
        <baseIpAddress>10.241.1.1</baseIpAddress>
        <endIpAddress>10.241.1.125</endIpAddress>
        <profileName>VCX</profileName>
    </group>
    <group>
        <baseIpAddress>10.241.1.126</baseIpAddress>
        <endIpAddress>10.241.2.250</endIpAddress>
        <profileName>GT</profileName>
    </group>
    <properties>
        <profileName>GT</profileName>
        <serialNumberPrefix>GtSN</serialNumberPrefix>
        <firmwareVersion>GtFW</firmwareVersion>
        <hardwareType>GtHW</hardwareType>
        <productName>GtPN</productName>
        <portNumber>6</portNumber>
        <portBaseName>Port</portBaseName>
        <assembly>GtA</assembly>
    </properties>
    <properties>
        <profileName>VCX</profileName>
        <serialNumberPrefix>VcxSN</serialNumberPrefix>
        <firmwareVersion>VcxFW</firmwareVersion>
        <hardwareType>VcxHW</hardwareType>
        <productName>VcxPN</productName>
        <portNumber>7</portNumber>
        <portBaseName>Port</portBaseName>
        <assembly>VcxA</assembly>
    </properties>
</nesProfiles>

And i have the three following classes

@XmlRootElement( name = "properties" )
@XmlAccessorType( XmlAccessType.FIELD )
    public class NesProperties {

        private String profileName;
        private String serialNumberPrefix;
        private String firmwareVersion;
        private String hardwareType;
        private String productName;
        private String portBaseName;
        private String assembly;
        private int portNumber;
    }

.

@XmlRootElement( name = "group" )
@XmlAccessorType( XmlAccessType.FIELD )
    public class NesGroup {
        private String baseIpAddress;
        private String endIpAddress;
        private String profileName;
    }

.

@XmlRootElement( name = "nesProfiles" )
@XmlAccessorType( XmlAccessType.FIELD )
public class NesProfiles {

    @XmlElement( name = "properties" )
    private List<NesProperties> profiles = new ArrayList<NesProperties>();

    @XmlElement( name = "group" )
    private List<NesGroup> groups = new ArrayList<NesGroup>();
}

So a NesGroup object must be mapped to NesProperties one, currently i am doing this association by Java code by checking the profile name in the group element and putting the entry in a hashmap of the NesProfiles object. I would like to know if it's possible to do that directly through JAXB with an xml structure like this.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE xml>
<nesProfiles>
    <group>
        <baseIpAddress>10.241.1.1</baseIpAddress>
        <endIpAddress>10.241.1.125</endIpAddress>
        <properties>VCX</properties>
    </group>
    <group>
        <baseIpAddress>10.241.1.126</baseIpAddress>
        <endIpAddress>10.241.2.250</endIpAddress>
        <properties>GT</properties>
    </group>
    <properties>
        <profileName>GT</profileName>
        <serialNumberPrefix>GtSN</serialNumberPrefix>
        <firmwareVersion>GtFW</firmwareVersion>
        <hardwareType>GtHW</hardwareType>
        <productName>GtPN</productName>
        <portNumber>6</portNumber>
        <portBaseName>Port</portBaseName>
        <assembly>GtA</assembly>
    </properties>
    <properties>
        <profileName>VCX</profileName>
        <serialNumberPrefix>VcxSN</serialNumberPrefix>
        <firmwareVersion>VcxFW</firmwareVersion>
        <hardwareType>VcxHW</hardwareType>
        <productName>VcxPN</productName>
        <portNumber>7</portNumber>
        <portBaseName>Port</portBaseName>
        <assembly>VcxA</assembly>
    </properties>
</nesProfiles>

And having the the group object directly having the NesProperties object as a class attribute.

@XmlRootElement( name = "group" )
@XmlAccessorType( XmlAccessType.FIELD )
public class NesGroup {
  private String baseIpAddress;
  private String endIpAddress;
  private String profileName;
  private NesProperties nesProperties;
}

Upvotes: 1

Views: 65

Answers (1)

lexicore
lexicore

Reputation: 43689

You can solve this with @XmlID/@XmlIDREF. Something like:

public class NesGroup {
  private String baseIpAddress;
  private String endIpAddress;
  private String profileName;
  @XmlIDREF
  private NesProperties nesProperties;
}

And:

   public class NesProperties {

        @XmlID
        private String profileName;
        private String serialNumberPrefix;
        private String firmwareVersion;
        private String hardwareType;
        private String productName;
        private String portBaseName;
        private String assembly;
        private int portNumber;
    }

See also id resolvers.

Upvotes: 1

Related Questions