Jaxb可以自动映射对象吗?

时间:2015-04-15 19:19:08

标签: java xml jaxb

我有一个像这样的xml结构。

<?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>

我有以下三个课程

@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>();
}

所以NesGroup对象必须映射到NesProperties,目前我通过检查组元素中的配置文件名称并将条目放在NesProfiles对象的hashmap中,通过Java代码进行此关联。我想知道是否可以通过JAXB直接这样做,并使用像这样的xml结构。

<?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>

让组对象直接将NesProperties对象作为类属性。

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

1 个答案:

答案 0 :(得分:1)

您可以使用@XmlID / @XmlIDREF解决此问题。类似的东西:

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

   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;
    }

另见id resolvers