jaxb 组装及解析xml

参考

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());
} }

  

上一篇:Bootstrap3基础 warning/active... 表格的状态类(不同的背景色)


下一篇:关于SWT中的表格(TableViewer类)