Unmarshalling nested list of xml items using JAXB

前端 未结 2 1597
情书的邮戳
情书的邮戳 2020-12-10 09:05

I\'ve got such xml construction which I need to convert into java objects using JAXB:


    
        
          


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-10 09:39

    You will need to define a custom XmlAdapter. The complicated part in your case is that you want to map one XML element into multiple Java Element objects. This means that, in Java., your XmlAdapter needs to be configured for collection of Element objects. Assuming your example XML fragment is part of a document:

    
        
          
              ....
          
       
        
    

    Then you will need to configure the XmlAdapter for the List field in the Java Document class:

    class Document {
         @XmlJavaTypeAdapter(CustomAdapter.class)
         List elements;
    }
    

    Then you your CustomAdapter class can receive a list of Element objects (corresponding to the actual XML structure with the nested items) and produce a list of Element with the structure you want.

    For an example, check JAXB XmlAdapter – Customized Marshaling and Unmarshaling

提交回复
热议问题