JAXB- @XmlMixed usage for reading @XmlValue and @XmlElement

后端 未结 1 391
谎友^
谎友^ 2020-12-01 17:41

I saw a similar question being posted here, yet it did not help me solve the problem so I am posting my question here to see if someone can modify my code to make it work.

相关标签:
1条回答
  • 2020-12-01 18:00

    I'll try to answer your question with an example:

    input.xml

    We will use the following XML document for this example. The root element has mixed content. Having mixed conent means that text nodes can appear mixed in with the elements. Since more than one text node can appear a unary property isn't a good fit.

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
        <root/>
        Hello
        <root/>
        World
        <root/>
    </root>
    

    Demo

    The following code will be used in to read in the XML to object form and then write it back to XML.

    package forum10940267;
    
    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Root.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum10940267/input.xml");
            Root root = (Root) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(root, System.out);
        }
    
    }
    

    USE CASE #1 - One List to Hold Mixed Content

    @XmlMixed is most often used to with another annotation, so that the resulting List contains both element and text content. One advantage of this is that order is maintained so that the document can be round tripped.

    package forum10940267;
    
    import java.util.*;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class Root {
    
        private List<Object> mixedContent = new ArrayList<Object>();
    
        @XmlElementRef(name="root", type=Root.class)
        @XmlMixed
        public List<Object> getMixedContent() {
            return mixedContent;
        }
    
        public void setMixedContent(List<Object> mixedContent) {
            this.mixedContent = mixedContent;
        }
    
    }
    

    Output

    The output matches the input.

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <root>
        <root/>
        Hello
        <root/>
        World
        <root/>
    </root>
    

    USE CASE #2 - Separate List for Mixed Content

    You can can also introduce a separate list property for the text content.

    package forum10940267;
    
    import java.util.*;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class Root {
    
        private List<Object> mixedContent = new ArrayList<Object>();
        private List<String> text;
    
        @XmlElementRef(name="root", type=Root.class)
        public List<Object> getMixedContent() {
            return mixedContent;
        }
    
        public void setMixedContent(List<Object> mixedContent) {
            this.mixedContent = mixedContent;
        }
    
        @XmlMixed
        public List<String> getText() {
            return text;
        }
    
        public void setText(List<String> text) {
            this.text = text;
        }
    
    }
    

    Output

    The output no longer matches the input.

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <root>
        <root/>
        <root/>
        <root/>
    
        Hello
    
        World
    
    </root>
    

    USE CASE #3 - String Property for Text Content

    Since text nodes can occur multiple times in mixed content, a non-List property isn't a good fit and it appears as though the @XmlMixed annotation is being ignored.

    package forum10940267;
    
    import java.util.*;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class Root {
    
        private List<Object> mixedContent = new ArrayList<Object>();
        private String text;
    
        @XmlElementRef(name="root", type=Root.class)
        public List<Object> getMixedContent() {
            return mixedContent;
        }
    
        public void setMixedContent(List<Object> mixedContent) {
            this.mixedContent = mixedContent;
        }
    
        @XmlMixed
        public String getText() {
            return text;
        }
    
        public void setText(String text) {
            this.text = text;
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <root>
        <root/>
        <root/>
        <root/>
    </root>
    
    0 讨论(0)
提交回复
热议问题