1

我想从 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 的用法与我使用的上面相同,但仍然出现错误。

4

1 回答 1

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) 实现。

新豆

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;

}

标签4

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Tag4 {

    @XmlAttribute
    private String id;

    @XmlValue
    private String value;

}

了解更多信息

于 2013-08-22T18:10:32.923 回答