简体   繁体   English

如何将两个xml元素组合到一个使用xsd生成的JAXB类中

[英]How to combine two xml elements into one JAXB Class generated using xsd

I am using some xsd like this. 我正在使用这样的xsd。 This will generate a class with two fields name1 and name2. 这将生成一个具有两个字段name1和name2的类。 What I want is to generate a class with one field completeName with by combining both fields. 我想要的是通过组合两个字段来生成一个具有一个字段completeName的类。

<xs:element name="person">
    <xs:complexType>
         <xs:sequence>
             <xs:element name="firstName" type="xs:string"/>
             <xs:element name="lastName" type="xs:string"/>
         </xs:sequence> 
    </xs:complexType>   
</xs:element>

You may move your @XmlValue or @XmlAttribute Annoation to the setter methods and customize your setters there. 您可以将@XmlValue或@XmlAttribute Annoation移至setter方法,并在那里自定义setter。 In your case it probably makes more sense, however, to store the two values atomic in two separate fields and have a custom "getCombinedName" method in your jaxb class like this. 在您的情况下,将两个原子原子值存储在两个单独的字段中,并在jaxb类中具有一个自定义“ getCombinedName”方法,就像这样更有意义。

public String getCombinedName() {
  return firstName + " " + lastName;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM