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

JAXB2完成java bean到xml的互相转换【转】

2012-09-08 
JAXB2完成java bean到xml的相互转换【转】!--rdf:RDF xmlns:rdfhttp://www.w3.org/1999/02/22-rdf-synta

JAXB2完成java bean到xml的相互转换【转】
<!--<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:dc="http://purl.org/dc/elements/1.1/"xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"><rdf:Descriptionrdf:about="http://www.blogjava.net/landor2004/archive/2011/06/03/351656.html"dc:identifier="http://www.blogjava.net/landor2004/archive/2011/06/03/351656.html"dc:title="JAXB2完成java bean到xml的相互转换"trackback:ping="http://www.blogjava.net/landor2004/services/trackbacks/351656.aspx" /></rdf:RDF>-->};
????public?String?getUserName(){JAXB2完成java bean到xml的互相转换【转】};2 加入@XmlElement注解,生成xml中的子节点,其实可以不加,不加默认就是生成,可以指定name修改节点名
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->??? private?String?userId;
????private?String?userName;
???? @XmlElement??? public?String?getUserId(){JAXB2完成java bean到xml的互相转换【转】};
????public?String?getUserName(){JAXB2完成java bean到xml的互相转换【转】};3 现在就可以生成xml了,代码如下:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->public?class?TestJAXB?{
????@Test
????public?void?bean2XML()?throws?Exception{
????????User?user?=?new?User();
????????user.setUserId("id1");
????????user.setUserName("aaa");
????????JAXBContext?context?=?JAXBContext.newInstance(user.getClass());
????????Marshaller?m?=?context.createMarshaller();
????????StringWriter?sw?=?new?StringWriter();
????????m.marshal(user,sw);
????????System.out.println(sw.toString());
????}
}4 生成了xml文件,可以转换成bean,比如:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->public?class?TestJAXB?{
????@Test
????public?void xml2Bean()?throws?Exception{
????????User?user?=?new?User();
????????String?xml?=?"xml字符串";
????????JAXBContext?context?=?JAXBContext.newInstance(user.getClass());
????????Unmarshaller?um?=context.createUnmarshaller();
????????StringReader?sr?=?new?StringReader(xml);
????????user?=?(User)um.unmarshal(sr);
????????
????????System.out.println(user.getUserId());
????}
}
简单转化就做完了,有几个地方:
1 生成xml比较简单,如果想把User里的userId和name变成xml根节点的一个属性可用(如果有get方法,需要加到get方法上)
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->????@XmlAttribute
????String?userId;
??? @XmlAttribute??? String?userName;生成的xml类似:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><user?userId="id1"?userName="aaa">
????<!--其他元素内容-->
</user>
2 如果想在节点外面包裹一层,可以用
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->????@XmlElementWrapper(name="wapper"?)
????@XmlAttribute
????private?String?userName;生成的xml类似:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><user>
????<userId>id1</userId>
????<wapper>
????????<userName>aaa</userName>
????</wapper>
????<!--其他元素内容-->
</user>3 关于多态,JAXB也是支持的:有两种方式
??? A 通过@XmlSeeAlso,比如:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->@XmlRootElement
public?class?User{
????Person?person;
}

@XmlSeeAlso({Man.class,?Girl.class})
public?class?Person{
}

public?class?Man?extends?Person{
}

public?class?Girl?extends?Person{
}
? B 用SeeAlso注解的缺点是:每次加一个子类都需要修改注解代码重新编译,其实可以直接在JAXBContext.newInstance()中加入这些子类:比如
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->JAXBContext?context?=?JAXBContext.newInstance(user.getClass(),?Man.class,?Girl.class);
以上两种方式都可以把xml? Unmarshaller 成java bean;根据情况任选其一。

热点排行