jaxb解组自定义类

时间:2018-07-18 07:27:17

标签: java jaxb

我尝试通过jaxb将xml文件解组到自定义类。

(不可更改的)xml文件如下所示:

<demo>
  <customers>
    <customer usrid="1" name="jane" />
    <customer usrid="2" name="leia" />
    <customer usrid="3" name="tobi" />
  </customers>
  <phonenumbers>
    <phonenumber usrid="1">123-456-789</phonenumber>
    <phonenumber usrid="2">987-654-321</phonenumber>
    <phonenumber usrid="2">111-222-333</phonenumber>
  </phonenumbers>
  <mobilenumbers>
    <mobilenumber usrid="3">666-666-666</mobilenumber>
  </mobilenumbers>
</demo>

Java类看起来很特别:

public class Demo {
  public List<Customer> customers;
}

public class Customer {
  public String usrid;
  public String name;
  public List<Number> numbers;
}

public class Number {
  public NumberType type;
  public String value;
}

public enum NumberType {
  phone, mobile
}

使用jaxb(例如适配器)是否可行?还是我必须先进行xslt转换?

顺便说一句。使用jaxb简单易用(可以省略注释):

public class Demo {
  public List<Customer> customers;
  public List<Phonenumber> phonenumbers;
  public List<Mobilenumbers> mobilenumbers;
}

public class Customer {
  public String usrid;
  public String name;
}

public class Phonenumber {
  public String usrid;
  public String value;
}

public class Mobilenumber {
  public String usrid;
  public String value;
}

0 个答案:

没有答案