Jaxb unmarshal定制xml很难

时间:2016-09-03 22:53:29

标签: xml jaxb unmarshalling

我有以下XML:

<books>
    <book>
        <id>......<id>
        <author>....<author>
        <set01_start>.......</set01_end>
        <set01_summary>.....</set01_summary>
        <set01_end>.........<set01_end>
        <set02_start>.......</set02_end>
        <set02_summary>.....</set02_summary>
        <set02_end>.........<set02_end>
        <set03_start>.......</set03_end>
        <set03_summary>.....</set03_summary>
        <set03_end>.........<set03_end>
    </book>
    <book>
        <id>......<id>
        <author>....<author>
        <set01_start>.......</set01_end>
        <set01_summary>.....</set01_summary>
        <set01_end>.........<set01_end>
        <set02_start>.......</set02_end>
        <set02_summary>.....</set02_summary>
        <set02_end>.........<set02_end>
    </book>
</books>

我正在尝试将其映射到java类 但是我对列表集有疑问 我试图在这种情况下使用@XmlAnyElement(lax = true)注释 但是集合中的所有数据都是空的

@XmlAnyElement(lax = true)
public List<Object> set;

和Set Claas

@XmlAccessorType(XmlAccessType.FIELD)
public class Set {
@XmlElement
private String start;
@XmlElement
private String end;
@XmlElement
private String summary;

1 个答案:

答案 0 :(得分:0)

我有一个想法,即编写用于更改xml结构的conventer。
在这种形式下,我可以轻松解析它。

<books>
<book>
    <id>......<id>
    <author>....<author>
    <set>
      <start>.......</_start>
      <summary>.....</summary>
      <end>.........<end>
    </set>
    <set>
      <start>.......</end>
      <summary>.....</summary>
      <end>.........<end>
    </set>
    <set>
     <start>.......</end>
     <summary>.....</summary>
     <end>.........<end>
   </set>
</book>
<book>
    <id>......<id>
    <author>....<author>
    <set>
      <start>.......</end>
      <summary>.....</summary>
      <end>.........<end>
    </set>
    <set>
      <start>.......</end>
      <summary>.....</summary>
      <end>.........<end>
    </set>
</book>

但在maping到object之后我需要以旧格式xml保存新对象。

也许某人有更好的主意?