分享一个XML应用读写工具
Posted JAVAFANS
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分享一个XML应用读写工具相关的知识,希望对你有一定的参考价值。
-----聊摄影 谈技术 不老的攻城狮-----
package com.wangbin.util;
import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.apache.log4j.Logger;
import org.xml.sax.InputSource;
public class JXUtil
{
private static final String DEFAULT_XOENCODING = "UTF-8";
private static final String DEFAULT_OXENCODING = "UTF-8";
private static final Logger log = Logger.getLogger(JXUtil.class);
public static Object Xml2Object(String xml, Class targetClass)
throws Exception
{
try
{
JAXBContext context = JAXBContext.newInstance(new Class[] { targetClass });
InputSource is = new InputSource();
is.setEncoding("UTF-8");
StringReader xmlStr = new StringReader(xml);
is.setCharacterStream(xmlStr);
Unmarshaller unmarshaller = context.createUnmarshaller();
return unmarshaller.unmarshal(is);
}
catch (JAXBException e) {
log.error("Xml转换Object失败", e);
throw new Exception(e);
}
}
public static String Object2Xml(Object object)
throws Exception
{
ByteArrayOutputStream baos = null;
try {
JAXBContext context = JAXBContext.newInstance(new Class[] { object.getClass() });
Marshaller marshal = context.createMarshaller();
marshal.setProperty("jaxb.formatted.output", Boolean.valueOf(false));
marshal.setProperty("jaxb.encoding", "UTF-8");
marshal.setProperty("jaxb.fragment", Boolean.valueOf(false));
baos = new ByteArrayOutputStream();
marshal.marshal(object, baos);
return new String(baos.toByteArray(), "UTF-8");
} catch (JAXBException e) {
log.error("Object转换Xml失败", e);
throw new Exception(e);
} finally {
if (baos != null)
try {
baos.close();
}
catch (Exception localException1)
{
}
}
}
public static String Object2Xml(Object object, boolean format)
throws Exception
{
ByteArrayOutputStream baos = null;
try {
JAXBContext context = JAXBContext.newInstance(new Class[] { object.getClass() });
Marshaller marshal = context.createMarshaller();
marshal.setProperty("jaxb.formatted.output", Boolean.valueOf(format));
marshal.setProperty("jaxb.encoding", "UTF-8");
marshal.setProperty("jaxb.fragment", Boolean.valueOf(false));
baos = new ByteArrayOutputStream();
marshal.marshal(object, baos);
return new String(baos.toByteArray(), "UTF-8");
} catch (JAXBException e) {
log.error("Object转换Xml失败", e);
throw new Exception(e);
} finally {
if (baos != null)
try {
baos.close();
}
catch (Exception localException1)
{
}
}
}
public static String Object2Xml(Object object, boolean format, String xmlEncoding)
throws Exception
{
ByteArrayOutputStream baos = null;
try {
JAXBContext context = JAXBContext.newInstance(new Class[] { object.getClass() });
Marshaller marshal = context.createMarshaller();
marshal.setProperty("jaxb.formatted.output", Boolean.valueOf(format));
marshal.setProperty("jaxb.encoding", (xmlEncoding != null) && (!"".equals(xmlEncoding)) ? xmlEncoding : "UTF-8");
baos = new ByteArrayOutputStream();
marshal.marshal(object, baos);
return baos.toString();
} catch (JAXBException e) {
log.error("Object转换Xml失败", e);
throw new Exception(e);
} finally {
if (baos != null)
try {
baos.close();
}
catch (Exception localException1)
{
}
}
}
}
package com.wangbin.entity;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
(XmlAccessType.FIELD)
"Person") (name=
public class PersonXml
{
private String XingMing;
private String XingBie;
private String ChuShengNianYue;
private String MinZu;
private String JiGuan;
private String ChuShengDi;
private String RuDangShiJian;
private String CanJiaGongZuoShiJian;
private String JianKangZhuangKuang;
private String ZhuanYeJiShuZhiWu;
private String ShuXiZhuanYeYouHeZhuanChang;
private String QuanRiZhiJiaoYu_XueLi;
private String QuanRiZhiJiaoYu_XueWei;
private String QuanRiZhiJiaoYu_XueLi_BiYeYuanXiaoXi;
private String QuanRiZhiJiaoYu_XueWei_BiYeYuanXiaoXi;
private String ZaiZhiJiaoYu_XueLi;
private String ZaiZhiJiaoYu_XueWei;
private String ZaiZhiJiaoYu_XueLi_BiYeYuanXiaoXi;
private String ZaiZhiJiaoYu_XueWei_BiYeYuanXiaoXi;
private String XianRenZhiWu;
private String NiRenZhiWu;
private String NiMianZhiWu;
private String JianLi;
private String JiangChengQingKuang;
private String NianDuKaoHeJieGuo;
private String RenMianLiYou;
private String ChengBaoDanWei;
private String JiSuanNianLingShiJian;
private String TianBiaoShiJian;
private String TianBiaoRen;
private String ShenFenZheng;
private List<JiaTingChengYuanXml> JiaTingChengYuan;
private byte[] ZhaoPian;
public PersonXml(String xingMing, String xingBie, String chuShengNianYue, String minZu, String jiGuan, String chuShengDi, String ruDangShiJian, String canJiaGongZuoShiJian, String jianKangZhuangKuang, String zhuanYeJiShuZhiWu, String shuXiZhuanYeYouHeZhuanChang, String quanRiZhiJiaoYu_XueLi, String quanRiZhiJiaoYu_XueWei, String quanRiZhiJiaoYu_XueLi_BiYeYuanXiaoXi, String quanRiZhiJiaoYu_XueWei_BiYeYuanXiaoXi, String zaiZhiJiaoYu_XueLi, String zaiZhiJiaoYu_XueWei, String zaiZhiJiaoYu_XueLi_BiYeYuanXiaoXi, String zaiZhiJiaoYu_XueWei_BiYeYuanXiaoXi, String xianRenZhiWu, String niRenZhiWu, String niMianZhiWu, String jianLi, String jiangChengQingKuang, String nianDuKaoHeJieGuo, String renMianLiYou, String chengBaoDanWei, String tianBiaoRen, String shenFenZheng, String jiSuanNianLingShiJian, String tianBiaoShiJian, List<JiaTingChengYuanXml> jiaTingChengYuan, byte[] zhaoPian)
{
this.XingMing = xingMing;
this.XingBie = xingBie;
this.ChuShengNianYue = chuShengNianYue;
this.MinZu = minZu;
this.JiGuan = jiGuan;
this.ChuShengDi = chuShengDi;
this.RuDangShiJian = ruDangShiJian;
this.CanJiaGongZuoShiJian = canJiaGongZuoShiJian;
this.JianKangZhuangKuang = jianKangZhuangKuang;
this.ZhuanYeJiShuZhiWu = zhuanYeJiShuZhiWu;
this.ShuXiZhuanYeYouHeZhuanChang = shuXiZhuanYeYouHeZhuanChang;
this.QuanRiZhiJiaoYu_XueLi = quanRiZhiJiaoYu_XueLi;
this.QuanRiZhiJiaoYu_XueWei = quanRiZhiJiaoYu_XueWei;
this.QuanRiZhiJiaoYu_XueLi_BiYeYuanXiaoXi = quanRiZhiJiaoYu_XueLi_BiYeYuanXiaoXi;
this.QuanRiZhiJiaoYu_XueWei_BiYeYuanXiaoXi = quanRiZhiJiaoYu_XueWei_BiYeYuanXiaoXi;
this.ZaiZhiJiaoYu_XueLi = zaiZhiJiaoYu_XueLi;
this.ZaiZhiJiaoYu_XueWei = zaiZhiJiaoYu_XueWei;
this.ZaiZhiJiaoYu_XueLi_BiYeYuanXiaoXi = zaiZhiJiaoYu_XueLi_BiYeYuanXiaoXi;
this.ZaiZhiJiaoYu_XueWei_BiYeYuanXiaoXi = zaiZhiJiaoYu_XueWei_BiYeYuanXiaoXi;
this.XianRenZhiWu = xianRenZhiWu;
this.NiRenZhiWu = niRenZhiWu;
this.NiMianZhiWu = niMianZhiWu;
this.JianLi = jianLi;
this.JiangChengQingKuang = jiangChengQingKuang;
this.NianDuKaoHeJieGuo = nianDuKaoHeJieGuo;
this.RenMianLiYou = renMianLiYou;
this.ChengBaoDanWei = chengBaoDanWei;
this.JiSuanNianLingShiJian = jiSuanNianLingShiJian;
this.TianBiaoShiJian = tianBiaoShiJian;
this.TianBiaoRen = tianBiaoRen;
this.ShenFenZheng = shenFenZheng;
this.JiaTingChengYuan = jiaTingChengYuan;
this.ZhaoPian = zhaoPian;
}
public PersonXml()
{
}
public String getXingMing() {
return this.XingMing;
}
public void setXingMing(String xingMing) {
this.XingMing = xingMing;
}
public String getXingBie() {
return this.XingBie;
}
public void setXingBie(String xingBie) {
this.XingBie = xingBie;
}
public String getChuShengNianYue() {
return this.ChuShengNianYue;
}
public void setChuShengNianYue(String chuShengNianYue) {
this.ChuShengNianYue = chuShengNianYue;
}
public String getMinZu() {
return this.MinZu;
}
public void setMinZu(String minZu) {
this.MinZu = minZu;
}
public String getJiGuan() {
return this.JiGuan;
}
public void setJiGuan(String jiGuan) {
this.JiGuan = jiGuan;
}
public String getChuShengDi() {
return this.ChuShengDi;
}
public void setChuShengDi(String chuShengDi) {
this.ChuShengDi = chuShengDi;
}
public String getRuDangShiJian() {
return this.RuDangShiJian;
}
public void setRuDangShiJian(String ruDangShiJian) {
this.RuDangShiJian = ruDangShiJian;
}
public String getCanJiaGongZuoShiJian() {
return this.CanJiaGongZuoShiJian;
}
public void setCanJiaGongZuoShiJian(String canJiaGongZuoShiJian) {
this.CanJiaGongZuoShiJian = canJiaGongZuoShiJian;
}
public String getJianKangZhuangKuang() {
return this.JianKangZhuangKuang;
}
public void setJianKangZhuangKuang(String jianKangZhuangKuang) {
this.JianKangZhuangKuang = jianKangZhuangKuang;
}
public String getZhuanYeJiShuZhiWu() {
return this.ZhuanYeJiShuZhiWu;
}
public void setZhuanYeJiShuZhiWu(String zhuanYeJiShuZhiWu) {
this.ZhuanYeJiShuZhiWu = zhuanYeJiShuZhiWu;
}
public String getShuXiZhuanYeYouHeZhuanChang() {
return this.ShuXiZhuanYeYouHeZhuanChang;
}
public void setShuXiZhuanYeYouHeZhuanChang(String shuXiZhuanYeYouHeZhuanChang) {
this.ShuXiZhuanYeYouHeZhuanChang = shuXiZhuanYeYouHeZhuanChang;
}
public String getQuanRiZhiJiaoYu_XueLi() {
return this.QuanRiZhiJiaoYu_XueLi;
}
public void setQuanRiZhiJiaoYu_XueLi(String quanRiZhiJiaoYu_XueLi) {
this.QuanRiZhiJiaoYu_XueLi = quanRiZhiJiaoYu_XueLi;
}
public String getQuanRiZhiJiaoYu_XueWei() {
return this.QuanRiZhiJiaoYu_XueWei;
}
public void setQuanRiZhiJiaoYu_XueWei(String quanRiZhiJiaoYu_XueWei) {
this.QuanRiZhiJiaoYu_XueWei = quanRiZhiJiaoYu_XueWei;
}
public String getQuanRiZhiJiaoYu_XueLi_BiYeYuanXiaoXi() {
return this.QuanRiZhiJiaoYu_XueLi_BiYeYuanXiaoXi;
}
public void setQuanRiZhiJiaoYu_XueLi_BiYeYuanXiaoXi(String quanRiZhiJiaoYu_XueLi_BiYeYuanXiaoXi)
{
this.QuanRiZhiJiaoYu_XueLi_BiYeYuanXiaoXi = quanRiZhiJiaoYu_XueLi_BiYeYuanXiaoXi;
}
public String getQuanRiZhiJiaoYu_XueWei_BiYeYuanXiaoXi() {
return this.QuanRiZhiJiaoYu_XueWei_BiYeYuanXiaoXi;
}
public void setQuanRiZhiJiaoYu_XueWei_BiYeYuanXiaoXi(String quanRiZhiJiaoYu_XueWei_BiYeYuanXiaoXi)
{
this.QuanRiZhiJiaoYu_XueWei_BiYeYuanXiaoXi = quanRiZhiJiaoYu_XueWei_BiYeYuanXiaoXi;
}
public String getZaiZhiJiaoYu_XueLi() {
return this.ZaiZhiJiaoYu_XueLi;
}
public void setZaiZhiJiaoYu_XueLi(String zaiZhiJiaoYu_XueLi) {
this.ZaiZhiJiaoYu_XueLi = zaiZhiJiaoYu_XueLi;
}
public String getZaiZhiJiaoYu_XueWei() {
return this.ZaiZhiJiaoYu_XueWei;
}
public void setZaiZhiJiaoYu_XueWei(String zaiZhiJiaoYu_XueWei) {
this.ZaiZhiJiaoYu_XueWei = zaiZhiJiaoYu_XueWei;
}
public String getZaiZhiJiaoYu_XueLi_BiYeYuanXiaoXi() {
return this.ZaiZhiJiaoYu_XueLi_BiYeYuanXiaoXi;
}
public void setZaiZhiJiaoYu_XueLi_BiYeYuanXiaoXi(String zaiZhiJiaoYu_XueLi_BiYeYuanXiaoXi)
{
this.ZaiZhiJiaoYu_XueLi_BiYeYuanXiaoXi = zaiZhiJiaoYu_XueLi_BiYeYuanXiaoXi;
}
public String getZaiZhiJiaoYu_XueWei_BiYeYuanXiaoXi() {
return this.ZaiZhiJiaoYu_XueWei_BiYeYuanXiaoXi;
}
public void setZaiZhiJiaoYu_XueWei_BiYeYuanXiaoXi(String zaiZhiJiaoYu_XueWei_BiYeYuanXiaoXi)
{
this.ZaiZhiJiaoYu_XueWei_BiYeYuanXiaoXi = zaiZhiJiaoYu_XueWei_BiYeYuanXiaoXi;
}
public String getXianRenZhiWu() {
return this.XianRenZhiWu;
}
public void setXianRenZhiWu(String xianRenZhiWu) {
this.XianRenZhiWu = xianRenZhiWu;
}
public String getNiRenZhiWu() {
return this.NiRenZhiWu;
}
public void setNiRenZhiWu(String niRenZhiWu) {
this.NiRenZhiWu = niRenZhiWu;
}
public String getNiMianZhiWu() {
return this.NiMianZhiWu;
}
public void setNiMianZhiWu(String niMianZhiWu) {
this.NiMianZhiWu = niMianZhiWu;
}
public String getJianLi() {
return this.JianLi;
}
public void setJianLi(String jianLi) {
this.JianLi = jianLi;
}
public String getJiangChengQingKuang() {
return this.JiangChengQingKuang;
}
public void setJiangChengQingKuang(String jiangChengQingKuang) {
this.JiangChengQingKuang = jiangChengQingKuang;
}
public String getNianDuKaoHeJieGuo() {
return this.NianDuKaoHeJieGuo;
}
public void setNianDuKaoHeJieGuo(String nianDuKaoHeJieGuo) {
this.NianDuKaoHeJieGuo = nianDuKaoHeJieGuo;
}
public String getRenMianLiYou() {
return this.RenMianLiYou;
}
public void setRenMianLiYou(String renMianLiYou) {
this.RenMianLiYou = renMianLiYou;
}
public String getChengBaoDanWei() {
return this.ChengBaoDanWei;
}
public void setChengBaoDanWei(String chengBaoDanWei) {
this.ChengBaoDanWei = chengBaoDanWei;
}
public String getJiSuanNianLingShiJian() {
return this.JiSuanNianLingShiJian;
}
public void setJiSuanNianLingShiJian(String jiSuanNianLingShiJian) {
this.JiSuanNianLingShiJian = jiSuanNianLingShiJian;
}
public String getTianBiaoShiJian() {
return this.TianBiaoShiJian;
}
public void setTianBiaoShiJian(String tianBiaoShiJian) {
this.TianBiaoShiJian = tianBiaoShiJian;
}
public List<JiaTingChengYuanXml> getJiaTingChengYuan() {
return this.JiaTingChengYuan;
}
public void setJiaTingChengYuan(List<JiaTingChengYuanXml> jiaTingChengYuan) {
this.JiaTingChengYuan = jiaTingChengYuan;
}
public byte[] getZhaoPian() {
return this.ZhaoPian;
}
public void setZhaoPian(byte[] zhaoPian) {
this.ZhaoPian = zhaoPian;
}
public String getTianBiaoRen() {
return this.TianBiaoRen;
}
public void setTianBiaoRen(String tianBiaoRen) {
this.TianBiaoRen = tianBiaoRen;
}
public String getShenFenZheng() {
return this.ShenFenZheng;
}
public void setShenFenZheng(String shenFenZheng) {
this.ShenFenZheng = shenFenZheng;
}
}
package com.wangbin.test;
import java.util.ArrayList;
import java.util.List;
import com.wangbin.entity.ItemXml;
import com.wangbin.entity.JiaTingChengYuanXml;
import com.wangbin.entity.PersonXml;
import com.wangbin.util.JXUtil;
public class Test
{
public static void main(String[] args)
throws Exception
{
PersonXml a = new PersonXml();
PersonXml bb = new PersonXml();
JiaTingChengYuanXml c = new JiaTingChengYuanXml();
ItemXml b = new ItemXml();
List jiaTingChengYuanXml = new ArrayList();
List itemlist = new ArrayList();
a.setXingMing("李佳慧1");
a.setCanJiaGongZuoShiJian("1996.07.01");
a.setJiangChengQingKuang("1990 中华杰出人物 2019 世界华人领袖");
a.setChuShengDi("浙江省金华市");
a.setJiaTingChengYuan(jiaTingChengYuanXml);
b.setChengWei("父亲");
b.setGongZuoDanWeiJiZhiWu("攻城狮");
b.setXingMing("李家户");
b.setZhengZhiMianMao("党员");
itemlist.add(b);
c.setItem(itemlist);
jiaTingChengYuanXml.add(c);
String s=JXUtil.Object2Xml(a, true);
bb=(PersonXml) JXUtil.Xml2Object(s, PersonXml.class);
System.out.println(bb.getXingMing());
}
}
身与JAVA同行 心与Python同梦
怀中却拥抱着佳能5DV入眠
以上是关于分享一个XML应用读写工具的主要内容,如果未能解决你的问题,请参考以下文章