JAXB: How should I marshal complex nested data structures?

后端 未结 6 1206
无人及你
无人及你 2020-11-27 02:27

I have several complex data structures like

Map< A, Set< B > >
Set< Map< A, B > >
Set< Map< A, Set< B > > >
Map<         


        
6条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 02:58

    It looks like you're on the right track with XMLAdapter... the error message may be a clue:

    class java.util.Collections$UnmodifiableMap nor any of its super class is known to this context.

    are you wrapping a map using Collections.unmodifiableMap() anywhere? Where exactly does the error occur?


    (previous answer left as a stale record for the curious)

    You can create custom marshaller/unmarshaller logic that works a little more straighforward than the Adapters idea (I think; I haven't used that one before).

    Basically the idea is that you specify a static function to do the work, and you can also create a custom class. (I usually put the static function in the class in question, but you don't have to.) Then you put a line in your .XJB file to tell JAXB to use your static function.

    Now that I took a look at my existing code, I see that all I was doing was converting an attribute string to a custom Java object. Here's the code, for reference, but it's just for attributes.

    JAXB file:

    
     
        
            
                
                    
                        
                            
                        
                    
                
            
        
    
    

    (parseMethod and printMethod convert to/from attribute strings)

提交回复
热议问题