从javabean制作自定义xml

时间:2013-08-22 16:57:24

标签: java xml eclipselink javabeans moxy

我想从javabean制作xml,如下所示:

        <tag2>message</tag2>
        <tag3>message</tag3>
        <tag4 id='UNIQUE MT ID 1'>MOBILE No.</tag4>

我在javabean中尝试了以下代码:

 @XmlAccessorType(XmlAccessType.FIELD)
 @XmlType(name = "name", propOrder = {"tag2", "tag3", "tag4"})
 public class newBean {

@XmlElement(required = true)
    private List<String> tag2;

@XmlElement(required = true)
    private List<String> tag3;

@XmlElement(required = true)
    private List<String> tag4;

@XmlPath("tag4/@id")
    private List<String> id;

public  List<String> getTag2() {
    return tag2;
}

public void setTag2(List<String> tag2) {
    this.tag2 = tag2;
}

public List<String> gettag4() {
    return tag4;
}

public void settag4(List<String> tag4) {
    this.tag4 = tag4;
}

public List<String> getId() {
    return id;
}

public void setId(List<String> identifier) {
    this.id = identifier;
}

public  List<String> gettag3() {
    return tag3;
}

public void settag3(List<String> tag3) {
    this.tag3 = tag3;
}
}

我收到以下错误:

    Errorcom.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of           IllegalAnnotationExceptions
    Property id is present but not specified in @XmlType.propOrder
    this problem is related to the following location:
            at private java.util.List model.newBean.id
            at model.newBean

请帮帮我。我正在使用@XmlPath标签并生成错误。我搜索了很多,发现@XmlPath用法与上面使用的相同但仍然出错。

1 个答案:

答案 0 :(得分:1)

注意:我是EclipseLink JAXB (MOXy)主管,是JAXB (JSR-222)专家组的成员。

关于@XmlPath

@XmlPath是EclipseLink JAXB(MOXy)扩展,要求您使用MOXy作为JAXB提供程序:

有效用例#1

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "name", propOrder = { "tag4", "id" })
public class newBean {

    @XmlElement(required=true)
    String tag4;

    @XmlPath("tag4/@id")
    String id;

}

有效用例#2

import java.util.List;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "name", propOrder = { "tag4", "id" })
public class newBean {

    @XmlPath("tag4/@id")
    List<String> id;

}

无效的用例

import java.util.List;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "name", propOrder = { "tag4", "id" })
public class newBean {

    @XmlElement(required=true)
    List<String> tag4;

    @XmlPath("tag4/@id")
    List<String> id;

}

映射您的用例

您可以引入一个对应于tag4元素的对象,该元素具有与id属性和文本对应的两个属性。这适用于任何JAXB(JSR-222)实现。

<强> newBean

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "name", propOrder = { "tag4", "id" })
public class newBean {

    List<Tag4> tag4;

}

<强> TAG4

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Tag4 {

    @XmlAttribute
    private String id;

    @XmlValue
    private String value;

}

了解更多信息