我想从 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 提供程序:
- http://blog.bdoughan.com/2010/07/xpath-based-mapping.html
- http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
有效用例#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/