首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

JAXB的marshaller有关问题

2013-12-13 
JAXB的marshaller问题本帖最后由 linwu_2006_2006 于 2013-12-10 12:58:20 编辑我想生成如下的xml结构,使

JAXB的marshaller问题
本帖最后由 linwu_2006_2006 于 2013-12-10 12:58:20 编辑 我想生成如下的xml结构,使用JAXB,请问我的类应该如何定义和增加标注。

<Roots>
  <Root>
    <Hierarchy Hierarchy_Code="TEST">测试用例</Hierarchy> 
  </Root>
</Roots>
[解决办法]
楼主要熟悉下jaxb的注解。给楼主个例子可以<hierarchy Hierarchy_Code="TEST">测试用例</hierarchy>


import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;

@XmlRootElement
public class Hierarchy {

private String code;
private String value;
@XmlAttribute(name="Hierarchy_Code")//这个注解说明为一个属性
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@XmlValue//此注解时说明这是一个元素的值不会生成一个元素或属性只是值
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public static void main(String[] args) throws Exception {
JAXBContext context=JAXBContext.newInstance(Hierarchy.class);
Marshaller marshaller=context.createMarshaller();
Hierarchy h=new Hierarchy();
h.setCode("TEST");
h.setValue("测试用例");
marshaller.marshal(h, new File("a.xml"));
}
}

热点排行