[bla bla bla]elementelement[forum15495156.Div@1f80ce47, forum15495156.Div@4166a779]
I have the next html, which I want to parse:
My input:
<div>
<span id="x1x1"> bla bla </span>
</div>
<span>
<div> bla bla </div>
</span>
My output in java:
jaxbContext = JAXBContext.newInstance(Div.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
jaxbUnmarshaller.unmarshal(file);
System.out.println("id " + div1.getSpan().get(0).get(id) + "value " + div1.getSpan().get(0).get(id))
// should print: id = x1x1 value = bla bla
I have the next class:
public class Span
List<Div> div;
public List<Div> getDiv() {
return div;
}
@XmlElement
public void setDiv(List<Div> div) {
for (int i = 0 ; i<div.size(); i++){
System.out.print("element")}
this.div = div;
}
and:
public class Div
List<Span> span = div1.get
@XmlElement
public void setSpan(List<Span> span) {
for (int i = 0 ; i<span.size(); i++){
System.out.print("element")}
this.span = span;
}
public List<Button> getSpan() {
return span;
}
Now, I want also the value of the span ("bla bla"). so I add to the class Span:
String value;
public String getValue() {
return value;
}
@XmlValue
public void setValue(String value) {
this.value = value;
}
Bit it gives me the next error:
If a class has '@XmlElement' property, it cannot have '@XmlValue' property.
I try to use @XMLMixed, but without success. I would be happy for example with code example. Thanks.
解决方案
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<Object> items = new ArrayList<Object>();
@XmlMixed
@XmlElementRef(type=Div.class, name="div")
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> mixed) {
this.items = items;
}
}
Div
The metadata for Div is almost identical to the metadata specified for Span.
@XmlRootElement
public class Div {
List<Object> items = new ArrayList<Object>();
@XmlElementRef(name="span", type=Span.class)
@XmlMixed
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
}