JAXB - 如何在绑定中指定 xml 属性 Java Class
JAXB - How to specify xml attributes in binding Java Class
我知道创建一个 JAXB Class 到 marshal/unmarshall 一个 xml 像这样
<outertag>
<innerelement>
<innerElementDetail1>some value</inner-element-detail1>
</innerelement>
</outertag>
这是我创建的 class
@XmlRootElement(name ="outertag")
@XmlAccessorType(XmlAccessType.FIELD)
public class OuterTag {
@XmlElement(name = "innerelement")
private List<InnerElement> innerElemements
public static InnerElement{
private String innerElementDetail;
// getters and setters
}
}
如果我必须像这样在其中一个内部元素上设置属性
<outertag>
<innerelement attribute1="attribute1value">
<innerElementDetail1>some value</inner-element-detail1>
</innerelement>
</outertag>
我该怎么做?
应该这样做:
@XmlRootElement(name ="outertag")
@XmlAccessorType(XmlAccessType.FIELD)
public class OuterTag {
@XmlElement(name = "innerelement")
private List<InnerElement> innerElemements
public static InnerElement{
@XmlAttribute(name = "attribute1")
protected String attribute1;
private String innerElementDetail;
// getters and setters
}
}
我知道创建一个 JAXB Class 到 marshal/unmarshall 一个 xml 像这样
<outertag>
<innerelement>
<innerElementDetail1>some value</inner-element-detail1>
</innerelement>
</outertag>
这是我创建的 class
@XmlRootElement(name ="outertag")
@XmlAccessorType(XmlAccessType.FIELD)
public class OuterTag {
@XmlElement(name = "innerelement")
private List<InnerElement> innerElemements
public static InnerElement{
private String innerElementDetail;
// getters and setters
}
}
如果我必须像这样在其中一个内部元素上设置属性
<outertag>
<innerelement attribute1="attribute1value">
<innerElementDetail1>some value</inner-element-detail1>
</innerelement>
</outertag>
我该怎么做?
应该这样做:
@XmlRootElement(name ="outertag")
@XmlAccessorType(XmlAccessType.FIELD)
public class OuterTag {
@XmlElement(name = "innerelement")
private List<InnerElement> innerElemements
public static InnerElement{
@XmlAttribute(name = "attribute1")
protected String attribute1;
private String innerElementDetail;
// getters and setters
}
}