JAXB XJC compiler disregarding mixed=true on XML Schema documents

后端 未结 3 1617
北荒
北荒 2021-02-03 10:57

XJC seems to be completely ignoring mixed=\"true\" on my XML Schema elements thereby not allowing me to extract text content. From the sample XML below, I need to b

3条回答
  •  情书的邮戳
    2021-02-03 11:54

    Years pass by, but this scheme keeps troubling people! I've been struggling with this same XSD (HL7) for the past few days, especially for handling the mixed content element types.

    My initial requirment was the opposite of the original poster, as I needed to write simple text inside a mixed element. But I must be able to also read such content, so I applied the same solution suggested in another reply, that is creating a custom binding file and using it in Eclipse's "JAXB Classes from Schema" wizard. In that case you can specifically choose which binding file(s) to use in one of the wizard's dialogs.

    As a result, classes are generated with a List content whenever a mixed element type is found. It gets a bit tricky to extract the single piece of information you actually need, but at least you're sure you can programatically access anything that's present in an XML file adhering that XSD.

    You can even navigate into more complex mixed contents such as:

    
        Sample Given Name
        Sample Family Name
    
    

    Where name is defined as such (I removed content not useful for this example from the original XSD):

    
        
            
                
                    
                    
                    
                    
                    
                
            
        
    
    

    Types en.family, en.given and such are mixed themselves (and for our purpose we don't need to know anything else).

    So, when you access name's content, its List will be composed of:

    1. a String, being the blanks between and
    2. an EnGiven element
    3. a String, being the blanks between and
    4. an EnFamily element
    5. a String, being the blanks between and

    EnGiven and EnFamily's content will be each made of a single String, respectively "Sample Given Name" and "Sample Family Name". Again, it is a bit tricky, and you have probably have to put some logic to handle the specific cases, but you can manage it this way.

    By the way, populating an element is trivial: just add a String to the desired List content.

提交回复
热议问题