参考
http://blog.csdn.net/yanan_seachange/article/details/7325708
a.添加依赖
b.建立绑定关系
c.测试
a.添加依赖
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.</version>
</dependency>
</dependency>
b.建立绑定关系
package com.yun.jaxb; import java.util.Vector; import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="earth")
public class Earth {
private String description="earth";
private Vector<Country> countries;
@XmlAttribute
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@XmlElementWrapper(name="countrys")
@XmlElement(name="country")
public Vector<Country> getCountries() {
return countries;
}
public void setCountries(Vector<Country> countries) {
this.countries = countries;
}
}
package com.yun.jaxb; import java.util.Vector;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="country")
public class Country {
private String description;
private String name;
private Vector<Province> provinces;
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@XmlElementWrapper(name="provinces")
@XmlElement(name="province")//如果provice对象中没有集合或者数组类型的属性了,该注解可以省略。否则,它是必须的。
public Vector<Province> getProvinces() {
return provinces;
}
public void setProvinces(Vector<Province> provinces) {
this.provinces = provinces;
}
}
package com.yun.jaxb; import java.util.Vector;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="province")
public class Province {
private String name;
private Vector<City> cities;
@XmlElementWrapper(name="cities")
@XmlElement(name="city")
public Vector<City> getCities() {
return cities;
}
public void setCities(Vector<City> cities) {
this.cities = cities;
}
@XmlElement()
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.yun.jaxb; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="city")
public class City {
private String name;
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
c.测试
public static void vectorXml() {
City zhengzhou=new City();
City jinan=new City();
zhengzhou.setName("郑州");
jinan.setName("济南"); Province henan=new Province();
henan.setName("河南");
Vector<City> henanCity=new Vector<City>();
henanCity.add(zhengzhou);
henan.setCities(henanCity);
Province shandong=new Province();
shandong.setName("山东");
Vector<City> sdCity=new Vector<City>();
sdCity.add(zhengzhou);
henan.setCities(sdCity); Country country=new Country();
country.setDescription("这里是天朝");
country.setName("中国");
Vector<Province> provinces=new Vector<Province>();
provinces.add(henan);
provinces.add(shandong);
country.setProvinces(provinces); Earth earth=new Earth();
earth.setDescription("地球...");
Vector<Country> countrys=new Vector<Country>();
countrys.add(country);
earth.setCountries(countrys); FileOutputStream outPut=null;
try {
outPut=new FileOutputStream("earth.xml");
JAXBContext jc=JAXBContext.newInstance(Earth.class);
Marshaller m=jc.createMarshaller();
m.marshal(earth, outPut);
} catch(Exception e){
e.printStackTrace();
}
finally {
try {
outPut.close();
} catch (IOException e) { }
}
}
结果:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<earth description="地球...">
<countrys>
<country>
<description>这里是天朝</description>
<name>中国</name>
<provinces>
<province>
<cities>
<city>
<name>郑州</name>
</city>
</cities>
<name>河南</name>
</province>
<province>
<name>山东</name>
</province>
</provinces>
</country>
</countrys>
</earth>
解析:
public static void unmarshalVectorXml() {
try {
File file = new File("earth.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Earth.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Earth earth = (Earth) jaxbUnmarshaller.unmarshal(file);
System.out.println(earth);
} catch (JAXBException e) {
e.printStackTrace();
}
}
封装util
package com.yun.xml.util; import java.io.ByteArrayOutputStream;
import java.io.StringReader; import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter; public class JaxbUtil { private static final String ENCODING_UTF8 = "UTF8"; /**
* JavaBean转换成xml
*
* @param obj
* @param encoding
* @return
*/
public static String convertToXml(Object obj) { try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, JaxbUtil.ENCODING_UTF8);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); ByteArrayOutputStream baos = new ByteArrayOutputStream();
//注意jdk版本
XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter xmlStreamWriter = xmlOutputFactory
.createXMLStreamWriter(baos, (String) marshaller
.getProperty(Marshaller.JAXB_ENCODING));
xmlStreamWriter.writeStartDocument(
(String) marshaller.getProperty(Marshaller.JAXB_ENCODING),
"1.0");
marshaller.marshal(obj, xmlStreamWriter);
xmlStreamWriter.writeEndDocument();
xmlStreamWriter.close();
return new String(baos.toString(JaxbUtil.ENCODING_UTF8)); } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null; } /**
* xml转换成JavaBean
*
* @param xml
* @param c
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T converyToJavaBean(String xml, Class<T> c) {
T t = null;
try {
JAXBContext context = JAXBContext.newInstance(c);
Unmarshaller unmarshaller = context.createUnmarshaller();
t = (T) unmarshaller.unmarshal(new StringReader(xml));
} catch (Exception e) {
e.printStackTrace();
} return t;
} }
测试
package com.yun.xml.test; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector; import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller; import com.yun.xml.bo.City;
import com.yun.xml.bo.Country;
import com.yun.xml.bo.Earth;
import com.yun.xml.bo.Province;
import com.yun.xml.util.JaxbUtil; public class XmlTest { public static void unmarshalVectorXml() {
try {
File file = new File("earth.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Earth.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Earth earth = (Earth) jaxbUnmarshaller.unmarshal(file);
System.out.println(earth);
} catch (JAXBException e) {
e.printStackTrace();
}
} public static void marshalVectorXml() {
Earth earth = installBean();
FileOutputStream outPut=null;
try {
outPut=new FileOutputStream("earth.xml");
JAXBContext jc=JAXBContext.newInstance(Earth.class);
Marshaller m=jc.createMarshaller();
m.marshal(earth, outPut);
} catch(Exception e){
e.printStackTrace();
}
finally {
try {
outPut.close();
} catch (IOException e) { }
}
} private static Earth installBean() {
City zhengzhou=new City();
City jinan=new City();
zhengzhou.setName("郑州");
jinan.setName("济南"); Province henan=new Province();
henan.setName("河南");
Vector<City> henanCity=new Vector<City>();
henanCity.add(zhengzhou);
henan.setCities(henanCity);
Province shandong=new Province();
shandong.setName("山东");
Vector<City> sdCity=new Vector<City>();
sdCity.add(zhengzhou);
henan.setCities(sdCity); Country country=new Country();
country.setDescription("这里是天朝");
country.setName("中国");
Vector<Province> provinces=new Vector<Province>();
provinces.add(henan);
provinces.add(shandong);
country.setProvinces(provinces); Earth earth=new Earth();
earth.setDescription("地球...");
Vector<Country> countrys=new Vector<Country>();
countrys.add(country);
earth.setCountries(countrys);
return earth;
} public static String toXml() {
Earth earth = installBean();
return JaxbUtil.convertToXml(earth);
} public static Earth toBean() {
String xml = "<?xml version=\"1.0\" encoding=\"UTF8\"?><earth description=\"地球...\"><countrys><country><description>这里是天朝</description><name>中国</name><provinces><province><cities><city><name>郑州</name></city></cities><name>河南</name></province><province><name>山东</name></province></provinces></country></countrys></earth>";
return JaxbUtil.converyToJavaBean(xml, Earth.class);
} public static void main(String[] args) {
// marshalVectorXml();
// unmarshalVectorXml();
System.out.println(toXml());
Earth earth = toBean();
System.out.println(earth.getDescription());
} }
jaxb 组装及解析xml的更多相关文章
-
java生成解析xml的另外两种方法Xstream
Xstream生成和解析xm和JAXB生成和解析xml的方法. 一,Xstream Xstream非jdk自带的,需要到入Xstream-1.4.3.jar和xpp3_min-1.1.4.jar 1. ...
-
使用JAXB解析xml文件(一)
1.java中解析xml的几种方式 1.1 JDK原生dom形式 原理:一次性把xml读入内存,在内存中构建成树形结构.优点:对节点操作方便,缺点:需要大量的内存空间,浪费资源 1.2 SAX形式 ...
-
JAXB解析XML为对象
JAXB支持注解将XML转化为对象,具体看一个简单的例子: <?xml version="1.0" encoding="utf-8"?> <A ...
-
dom4j组装xml 以及解析xml
dom4j组装xml 以及解析xml: 1.下载dom4j的jar包,地址:https://dom4j.github.io/ 2.java代码: package test; import java.i ...
-
jaxb解析xml工具类
[quote]jaxb jdk 自带的解析xml的一种方式支持,只需要用注解对javabean进行数据绑定[/quote] package com.nnk.flowrecharge.common;im ...
-
webservice04#对象与xml转换-jaxb#Stax解析xml#新建修改xml
1,Student类 package com.yangw.xml; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement / ...
-
解析xml并且导入mysql
https://www.zhihu.com/question/28139319 解析超过500G的xml 写入mysql,如何尽快写入? 解析超过500G的xml 写入mysql,如何尽快写入? 现在 ...
-
java 中解析xml的技术
最初,XML 语言仅仅是意图用来作为 HTML 语言的替代品而出现的,但是随着该语言的不断发展和完善,人们越来越发现它所具有的优点:例如标记语言可扩展,严格的语法规定,可使用有意义的标记,内容存储和表 ...
-
Java Dom解析xml
Dom解析是将xml文件全部载入,组装成一颗dom树,然后通过节点以及节点之间的关系来解析xml文件,下面结合这个xml文件来进行dom解析. <?xml version="1.0&q ...
随机推荐
-
[deviceone开发]-利用do_ListView模拟单选功能
一.简介 这个是利用do_ListView组件实现多个选项里选择一项的功能,示例很简单,但是有助于理解复用机制,也可以直接参考使用.初学者推荐.二.效果图 三.相关下载 https://github. ...
-
理解和解决MySQL乱码问题
本文将详细介绍MySQL乱码的成因和具体的解决方案 在阅读本文之前,强烈建议对字符集编码概念还比较模糊的同学 阅读下博主之前对相关概念的一篇科普:十分钟搞清字符集和字符编码 MySQL出现乱码的原因 ...
-
剑指 offer set 5 二进制中 1 的个数
总结 1. 负数右移会保持其符号. 比如 0x80000000 右移, 其对应的绝对值也是 0X80000000, 其右移一位并保持符号, 得到 0XC0000000. 符号位保持, 使得负数永远都无 ...
-
JavaScript 防止事件冒泡
在我们书写一个弹窗的时候,我们往往需要点击弹窗的其他地方来隐藏弹窗. 通常我们会写成: $(document).bind('click',function(){ $('.pop-box').hide( ...
-
五指CMS v1.2 GBK 发布下载
五指CMS v1.2 GBK 版本下载地址: http://www.wuzhicms.com/uploadfile/wuzhicms/wuzhicms-v1.2.zip 从内测到公测,五指 ...
-
为啥都不用Qt Quick Controls 2呢
为啥都不用Qt Quick Controls 2呢 https://github.com/qt/qtquickcontrols2/
-
【 bzoj4537】HNOI2016 最小公倍数
首先将边按a的值分组,每$\sqrt{m}$一组. 对于每一组,将符合一组a的询问选出来,将这些询问和这一块之前的边(a一定小于这些询问)按b排序,然后交替插入,询问,对于一个询问,在当前块也有可能有 ...
-
C语言排序算法学习笔记——交换类排序
交换类排序:根据序列中两个元素关键字的比较结果来交换他俩在序列中的位置. 冒泡排序:假设待排序表长为n,从后往前(或从前往后)两两比较相邻元素的值,若为逆序(即A[i-1]>A[i])则交换他们 ...
-
JavaScript中原型链的那些事
引言 在面向对象的语言中继承是非常重要的概念,许多面向对象语言都支持两种继承方式:接口继承和实现继承.接口继承制只继承方法签名,而实现继承继承实际的方法.在ECMAScript中函数没有签名,所以EC ...
-
AngularJS 中{{}}与ng-bind指令
面试中,有被问题关于{{}}与ng-bind指令的问题,在此,分享下自己的知识点. 在脚本没有加载完成时,用户会看到{{}},界面比较丑陋. 一般的解决方法: 在index.html里面使用n ...