JAXB - Java/XMLValue &XMLElement 冲突 [英] JAXB - Java/ XMLValue & XMLElement conflict

查看:30
本文介绍了JAXB - Java/XMLValue &XMLElement 冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的输入:<div><span id="x1x1">bla bla </span>

<跨度><div>bla bla </div></span>我在java中的输出:jaxbContext = JAXBContext.newInstance(Div.class);解组器 jaxbUnmarshaller = jaxbContext.createUnmarshaller();jaxbUnmarshaller.unmarshal(文件);System.out.println("id " + div1.getSpan().get(0).get(id) + "value" + div1.getSpan().get(0).get(id))//应该打印:id = x1x1 value = bla bla

我有下一堂课:

公共类跨度列表
div;公共列表<Div>getDiv() {返回div;}@XmlElementpublic void setDiv(List
div) {for (int i = 0 ; i

和:

公共类Div列表<跨度>跨度 = div1.get@XmlElementpublic void setSpan(Listspan) {for (int i = 0 ; i获取跨度(){回程跨度;}

现在,我还想要跨度的值(bla bla").所以我添加到类 Span:

字符串值;公共字符串 getValue() {返回值;}@XmlValue公共无效集值(字符串值){this.value = 值;}

它给了我下一个错误:

 如果类具有@XmlElement"属性,则它不能具有@XmlValue"属性.

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

解决方案

UPDATE

可以同时具有文本和元素的子注释的任何元素都被称为具有混合内容.在 JAXB 中,这对应于 @XmlMixed 注释.@XmlMixed 可以单独用于集合属性(请参阅原始答案)或与 @XmlAnyElement@XmlElementRef代码>@XmlElementRefs.如果元素可以是任何元素,您将使用 @XmlAnyElement,如果它是一个已知元素,您将使用 @XmlElementRef 并且它是多个已知元素,您使用 @XmlElementRefs.

跨度

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

@XmlRootElement公共类跨度{列表<对象>items = new ArrayList();@XmlMixed@XmlElementRef(type=Div.class, name="div")公共列表<对象>获取项目(){退换货品;}public void setItems(List混合){this.items = 物品;}}

分区

Div 的元数据与为 Span 指定的元数据几乎相同.

@XmlRootElement公共类 Div {列表<对象>items = new ArrayList();@XmlElementRef(name="span", type=Span.class)@XmlMixed公共列表<对象>获取项目(){退换货品;}public void setItems(List items) {this.items = 物品;}}

演示

公共类演示{public static void main(String[] args) 抛出异常 {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,真);marshaller.marshal(span, System.out);}}

输出

[文字,forum15495156.Div@289f6ae,文字3]<?xml version="1.0" encoding="UTF-8" standalone="yes"?><span>Text<div>Text2</div>Text3</span>

<小时>

原答案

您可以在您的 Span 类中添加一个用 @XmlMixed 注释的 List 属性:

跨度

import java.util.List;导入 javax.xml.bind.annotation.*;@XmlRootElement公共类跨度{列表
div;列表<字符串>混合;@XmlMixed公共列表<字符串>获取混合(){返回混合;}public void setMixed(Listmixed) {this.mixed = 混合;}公共列表<Div>getDiv() {返回div;}@XmlElementpublic void setDiv(List
div) {for (int i = 0; i < div.size(); i++) {System.out.print("元素");}this.div = div;}}

演示

import java.io.StringReader;导入 javax.xml.bind.*;公开课演示{public static void main(String[] args) 抛出异常 {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]

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;
    }

}

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("<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);
    }

}

Output

[Text, forum15495156.Div@289f6ae, Text3]
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<span>Text<div>Text2</div>Text3</span>


ORIGINAL ANSWER

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

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;
    }
}

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("<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());
    }

}

Output

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

这篇关于JAXB - Java/XMLValue &amp;XMLElement 冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆