上节中所说的最终xml格式,
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persion>
<filed name="userid">112</filed>
<filed name="username">就不告诉你</filed>
<filed name="birthday">2011-09-28</filed>
</persion>
聪明的读者应该自己就会实现了,通过集合+属性值的方式就ok了,看代码
package cn.uyunsky.blog.xml.demo6;
import java.io.StringReader;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
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>属性+集合 实现非正常思维的xml</p>
*/
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Persion {
@XmlElement(name = "filed")
private List<DemoField> infos;
public List<DemoField> getInfos() {
return infos;
}
public void setInfos(List<DemoField> infos) {
this.infos = infos;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Persion [infos=");
builder.append(infos);
builder.append("]");
return builder.toString();
}
public static void main(String[] args) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Persion.class);
Persion persion = new Persion();
List<DemoField> infos = new ArrayList<DemoField>();
DemoField userid = new DemoField();
userid.setName("userid");
userid.setValue("112");
infos.add(userid);
DemoField username = new DemoField();
username.setName("username");
username.setValue("就不告诉你");
infos.add(username);
DemoField birthday = new DemoField();
birthday.setName("birthday");
birthday.setValue(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
infos.add(birthday);
persion.setInfos(infos);
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();
}
}
}
DemoField对象
package cn.uyunsky.blog.xml.demo6;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
@XmlAccessorType(XmlAccessType.FIELD)
public class DemoField {
@XmlAttribute
private String name;
@XmlValue
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("DemoField [name=");
builder.append(name);
builder.append(", value=");
builder.append(value);
builder.append("]");
return builder.toString();
}
}
教程到此告一段落,读者们可自己研究javax.xml.bind.annotation包下的注释,实现更多的功能
分享到:
相关推荐
JAXB学习笔记 项目为了进行xml与java bean的互转,参考了各位网友的推荐,最后选型为JAXB,闲暇之余整理了一下笔记。废话不多说,上代码
多个关键字请用空格分隔,最多填写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 ...
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注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术
JAXB的HelloWorld源码,适合想学习JAXB技术的新手学习。
使用jaxb生成XML例子,含有例子和注解解析
有关Maven项目中缺少jaxb-api的异常报错解决,jaxb-api-2.3.0.jar
一个JAXB2的基础教程,教你快速运用JAXB通过XSD绑定XML和读写XML
jaxb-2.2.jar jaxb jax
NULL 博文链接:https://xuxiheng.iteye.com/blog/2233860
jaxb-api jaxb-impl jaxb-xjc jaxws-rt 这四个文件对应的jar包