想知道生成的xml节点名字怎么换吗,如果你看下annotation @XmlElement的源码就明白了,看这个例子
package cn.uyunsky.blog.xml.demo3;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Date;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* <p>把节点名字换换</p>
*/
@XmlRootElement(name="PERSION")
@XmlAccessorType(XmlAccessType.FIELD)
public class Persion {
@XmlElement(name="USER_ID")
private Integer userid;
@XmlElement(name="USER_NAME")
private String username;
@XmlElement(name="BIRTHDAY")
private Date birthday;
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Persion [userid=");
builder.append(userid);
builder.append(", username=");
builder.append(username);
builder.append(", birthday=");
builder.append(birthday);
builder.append("]");
return builder.toString();
}
public static void main(String[] args) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Persion.class);
Persion persion = new Persion();
persion.setUserid(112);
persion.setUsername("就不告诉你");
persion.setBirthday(new Date());
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter writer = new StringWriter();
marshaller.marshal(persion, writer);
String xml = writer.getBuffer().toString();
System.out.println(xml);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object bean = unmarshaller.unmarshal(new StringReader(xml));
System.out.println(bean);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
输出结果
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PERSION>
<USER_ID>112</USER_ID>
<USER_NAME>就不告诉你</USER_NAME>
<BIRTHDAY>2011-09-28T11:29:28.375+08:00</BIRTHDAY>
</PERSION>
Persion [userid=112, username=就不告诉你, birthday=Wed Sep 28 11:29:28 CST 2011]
JAXB转换方式的实现通过annotation来处理,因此我们想要什么功能可以仔细研究下javax.xml.bind.annotation每个注释的意义,语感好的通过类名就能猜到了
java与E文一样需要好的语感的~
分享到:
相关推荐
JAXB学习笔记 项目为了进行xml与java bean的互转,参考了各位网友的推荐,最后选型为JAXB,闲暇之余整理了一下笔记。废话不多说,上代码
NULL 博文链接:https://kamuikyo.iteye.com/blog/1182316
多个关键字请用空格分隔,最多填写5个。点击右侧Tag快速添加 多个关键字请用空格分隔,最多填写5个。点击右侧Tag快速添加
jaxb学习资料
JAXB的使用JAXB的使用JAXB的使用JAXB的使用
无需安装,解压后即可。jaxb-api.jar,jaxb-xjc.jar,jaxb-impl.jar,activation.jar等相关jar包在lib文件夹下。
jaxb-api jaxb-impl jar
JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件
JAXB的安装包及插件
NULL 博文链接:https://lijie-insist.iteye.com/blog/2160015
在使用webservice,mule esb等需要jaxb的项目里经常会出现 JAXB 2.0 API is being loaded from the bootstrap classloader这个错误,按照打出的信息Use the endorsed directory mechanism to place jaxb-api.jar in ...
JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术
java -jar JAXB2_20060607.jar On Windows, you can just double-click the jar file to execute. Release Notes Browse the release notes online, including what's new. Technical Support Please subscribe to...
JAXB的HelloWorld源码,适合想学习JAXB技术的新手学习。
使用jaxb生成XML例子,含有例子和注解解析
jaxb-2.2.jar jaxb jax
一个JAXB2的基础教程,教你快速运用JAXB通过XSD绑定XML和读写XML
有关Maven项目中缺少jaxb-api的异常报错解决,jaxb-api-2.3.0.jar
NULL 博文链接:https://xuxiheng.iteye.com/blog/2233860
jaxb-api jaxb-impl jaxb-xjc jaxws-rt 这四个文件对应的jar包