JAXB - Java/ XMLValue & XMLElement conflict

前端 未结 2 1688
南方客
南方客 2020-12-17 02:45

I have the next html, which I want to parse:

My input: 
bla bla
2条回答
  •  暖寄归人
    2020-12-17 03:35

    UPDATE

    Any element that can have both child notes that are text and elements is said to have mixed content. In JAXB this corresponds to the @XmlMixed annotation. @XmlMixed can be used on its own on a collection property (see ORIGINAL ANSWER) or in combination with @XmlAnyElement, @XmlElementRef, or @XmlElementRefs. If the element can be anything you would use @XmlAnyElement, if it is one known element you would use @XmlElementRef and it is more than one known element you use @XmlElementRefs.

    Span

    If there will be both text and div elements within the same span element you could do the following by annotating a property with both @XmlElementRef and @XmlMixed. The element name specified on the @XmlElementRef annotation must correspond directly to the root element specified for the target class.

    @XmlRootElement
    public class Span {
    
        List items = new ArrayList();
    
        @XmlMixed
        @XmlElementRef(type=Div.class, name="div")
        public List getItems() {
            return items;
        }
    
        public void setItems(List mixed) {
            this.items = items;
        }
    
    
    }
    
    
    

    Div

    The metadata for Div is almost identical to the metadata specified for Span.

    @XmlRootElement
    public class Div {
    
        List items = new ArrayList();
    
        @XmlElementRef(name="span", type=Span.class)
        @XmlMixed
        public List getItems() {
            return items;
        }
    
        public void setItems(List items) {
            this.items = items;
        }
    
    }
    
    
    

    Demo

    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Span.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Span span = (Span) unmarshaller.unmarshal(new StringReader("Text
    Text2
    Text3
    ")); System.out.println(span.getItems()); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(span, System.out); } }

    Output

    [Text, forum15495156.Div@289f6ae, Text3]
    
    Text
    Text2
    Text3

    ORIGINAL ANSWER

    You could add a List property annotated with @XmlMixed to your Span class:

    Span

    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class Span {
        List
    div; List mixed; @XmlMixed public List getMixed() { return mixed; } public void setMixed(List mixed) { this.mixed = mixed; } public List
    getDiv() { return div; } @XmlElement public void setDiv(List
    div) { for (int i = 0; i < div.size(); i++) { System.out.print("element"); } this.div = div; } }

    Demo

    import java.io.StringReader;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Span.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Span span1 = (Span) unmarshaller.unmarshal(new StringReader("bla bla bla"));
            System.out.println(span1.getMixed());
    
            Span span2 = (Span) unmarshaller.unmarshal(new StringReader("
    ")); System.out.println(span2.getDiv()); } }

    Output

    [bla bla bla]
    elementelement[forum15495156.Div@1f80ce47, forum15495156.Div@4166a779]
    

    提交回复
    热议问题