java - 从 javabean 制作自定义 xml

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

最佳答案

注意:我是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;

}

标签4

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Tag4 {

    @XmlAttribute
    private String id;

    @XmlValue
    private String value;

}

了解更多信息

关于java - 从 javabean 制作自定义 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18386591/

相关文章:

java - 优化插入多个具有嵌入式主键的实体时的 JPA 性能

java - 客户端日期时间与服务器日期时间不匹配

java - exec-maven-plugin 找不到或加载主类但输出在命令行上运行正常

java - 有没有办法可以重用 pom.xml 中定义的属性,作为另一个 *.xml 文件中的整数类型?

java - EclipseLink 如何检测首次访问以进行延迟抓取?

java - 在jpa多对多中合并连接表中的额外列

mysql - EclipseLink JPA 辅助服务器

java - 如何检查一个数字是否在 1 到 N 之间

java - 获取元素的 XPath 列表

java - 如何获得MySQL员工实践?