JAXB-Java / XMLValue和XMLElement冲突

我有下一个html,我想解析:

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

我下一堂课:

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;

}

和:

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;

}

现在,我还需要span的值(“ bla bla”)。所以我添加到类中Span

String value;

public String getValue() {

return value;

}

@XmlValue

public void setValue(String value) {

this.value = value;

}

有点给我下一个错误:

 If a class has '@XmlElement' property, it cannot have '@XmlValue' property.

我尝试使用@XMLMixed,但没有成功。我会很高兴例如代码示例。谢谢。

回答:

可以同时具有文本和元素的子注释的任何元素都称为混合内容。在JAXB中,这对应于@XmlMixed注释。

@XmlMixed可以在自家的集合属性使用(见原来的答复),或组合@XmlAnyElement@XmlElementRef@XmlElementRefs。如果该元素可以是您将要使用的任何元素@XmlAnyElement,则它是您将要使用的一个已知元素,@XmlElementRef并且它是您使用的一个以上已知元素@XmlElementRefs

如果div在同一span元素中同时包含text和elements,则可以通过使用@XmlElementRef和注释属性来执行以下操作@XmlMixed。在@XmlElementRef注释上指定的元素名称必须直接对应于为目标类指定的根元素。

@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几乎与为指定的元数据相同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;

}

}

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("<span>Text<div>Text2</div>Text3</span>"));

System.out.println(span.getItems());

Marshaller marshaller = jc.createMarshaller();

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

marshaller.marshal(span, System.out);

}

}

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

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<span>Text<div>Text2</div>Text3</span>


您可以在类中添加带List<String>注释的属性:@XmlMixed``Span

import java.util.List;

import javax.xml.bind.annotation.*;

@XmlRootElement

public class Span {

List<Div> div;

List<String> mixed;

@XmlMixed

public List<String> getMixed() {

return mixed;

}

public void setMixed(List<String> mixed) {

this.mixed = mixed;

}

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;

}

}

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("<span>bla bla bla</span>"));

System.out.println(span1.getMixed());

Span span2 = (Span) unmarshaller.unmarshal(new StringReader("<span><div/><div/></span>"));

System.out.println(span2.getDiv());

}

}

[bla bla bla]

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

以上是 JAXB-Java / XMLValue和XMLElement冲突 的全部内容, 来源链接: utcz.com/qa/408991.html

回到顶部