如何使用JAXB,
从此xml文件创建java对象<?xml version="1.0" encoding="ISO-8859-1" ?>
<Parameters>
<Varremise>
<Name>Amazigh</Name>
<Var>Notprovided</Var>
<Var>Notprovided</Var>
</Varremise>
<Varheader>
<Name>CdAppOr</Name>
<Var>P</Var>
<Var>A</Var>
<Var>O</Var>
</Varheader>
<Vardetail>
<Name>TopUC</Name>
<Var>U</Var>
<Var>C</Var>
</Vardetail>
</Parameters>
我试过这个,但它不起作用,
参数类:
import java.util.Set;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "parametersContainer")
@XmlRootElement(name = "Parameters")
public class Parameters {
@XmlElement(name = "Varremise")
@XmlElementWrapper(name = "Varremise")
private Set<VarRemise> varremise;
@XmlElement(name = "Varheader")
@XmlElementWrapper(name = "Varheader")
private Set<VarHeader> varheader;
@XmlElement(name = "Vardetail")
@XmlElementWrapper(name = "Vardetail")
private Set<VarDetail> vardetail;
public Set<VarHeader> getVarheader() {
return varheader;
}
public void setVarheader(final Set<VarHeader> varheader) {
this.varheader = varheader;
}
public Set<VarDetail> getVardetail() {
return vardetail;
}
public void setVardetail(final Set<VarDetail> vardetail) {
this.vardetail = vardetail;
}
public Set<VarRemise> getVarremise() {
return varremise;
}
public void setVarremise(final Set<VarRemise> varremise) {
this.varremise = varremise;
}
}
AllVarsDefinition类:
import java.util.Set;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class AllVarsDefinition {
@XmlElement(name = "Name", required = true)
private String name;
@XmlElement(name = "Var", required = true)
private Set<String> var;
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public Set<String> getVar() {
return var;
}
public void setVar(final Set<String> var) {
this.var = var;
}
}
VarHeader类:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
@XmlAccessorType(XmlAccessType.FIELD)
public class VarHeader extends AllVarsDefinition {
}
与Vardetail和varRemise类相同的代码,
当我执行此代码时,它会创建一个空对象
谢谢,
答案 0 :(得分:0)
问题是您的XML不是预期的。 您已指定了包装器和具有相同名称的元素。
@XmlElement(name = "Varheader")
@XmlElementWrapper(name = "Varheader")
预期的xml实际上是以下形式:
<Varheader>
<Varheader></Varheader>
</Varheader>
修改强>
基于一些评论,解决方案应该是:
答案 1 :(得分:0)
参数类
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Parameters")
public class Parameters {
@XmlElement(name = "Varremise")
private List<VarRemise> varremise;
public List<VarRemise> getVarremise() {
return varremise;
}
public void setVarremise(final List<VarRemise> varremise) {
this.varremise = varremise;
}
@XmlElement(name = "Varheader")
private List<VarHeader> varheader;
@XmlElement(name = "Vardetail")
private List<VarDetail> vardetail;
public List<VarHeader> getVarheader() {
return varheader;
}
public void setVarheader(final List<VarHeader> varheader) {
this.varheader = varheader;
}
public List<VarDetail> getVardetail() {
return vardetail;
}
public void setVardetail(final List<VarDetail> vardetail) {
this.vardetail = vardetail;
}
}
VarRemise类
import java.util.Set;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class VarRemise {
@XmlElement(name = "Name", required = true)
private String name;
@XmlElement(name = "Var", required = true)
private Set<String> vars;
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public Set<String> getVars() {
return vars;
}
public void setVars(final Set<String> vars) {
this.vars = vars;
}
}
与vardetail和varhead类相同的代码,它可以工作
谢谢cvesters