[quote]jaxb jdk 自带的解析xml的一种方式支持,只需要用注解对javabean进行数据绑定[/quote]


package com.nnk.flowrecharge.common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import com.nnk.flowrecharge.config.SystemConfig;

public class XmlUtil {

private static String DEFAULT_CHARSET = SystemConfig.DEFAULT_CHARSET;

public static String toXml(Object model) throws JAXBException, IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
marshal(model, output);
output.flush();
return new String(output.toByteArray(), DEFAULT_CHARSET);
}
public static String toXml(Object model,boolean isFormatOut) throws JAXBException, IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
marshal(model, output,isFormatOut);
output.flush();
return new String(output.toByteArray(), DEFAULT_CHARSET);
}
public static void marshal(Object model, OutputStream output) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(model.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, DEFAULT_CHARSET);
jaxbMarshaller.marshal(model, output);
}
public static void marshal(Object model, OutputStream output,boolean isFormatOut) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(model.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, isFormatOut);
jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, DEFAULT_CHARSET);
jaxbMarshaller.marshal(model, output);
}

public static <T> T parseXml(Class<T> clazz, String xml) throws JAXBException, IOException {
byte[] buf = xml.getBytes(DEFAULT_CHARSET);
ByteArrayInputStream input = new ByteArrayInputStream(buf, 0, buf.length);
return unmarshal(clazz, input);
}

@SuppressWarnings("unchecked")
public static <T> T unmarshal(Class<T> clazz, InputStream input) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return (T) jaxbUnmarshaller.unmarshal(input);
}

public static void saveXmlToFile(Object model, String filename) throws FileNotFoundException, JAXBException {
FileOutputStream fos = new FileOutputStream(filename);
marshal(model, fos);
}

public static void saveXmlToFile(Object model, File file) throws FileNotFoundException, JAXBException {
FileOutputStream fos = new FileOutputStream(file);
marshal(model, fos);
}

public static <T> T loadXmlFromFile(Class<T> clazz, String filename) throws FileNotFoundException, JAXBException {
return unmarshal(clazz, new FileInputStream(filename));
}

public static <T> T loadXmlFromFile(Class<T> clazz, File file) throws FileNotFoundException, JAXBException {
return unmarshal(clazz, new FileInputStream(file));
}

public static <T> T loadXmlFromFile(Class<T> clazz, InputStream is) throws JAXBException {
return unmarshal(clazz, is);
}
}

[quote]
动态生成xml 的javabean
[/quote]

package com.nnk.flowrecharge.config;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;

@XmlAccessorType(XmlAccessType.NONE)
public class CodeConfig {

@XmlAttribute
private String key = "";
@XmlAttribute
private String text = "";
@XmlAttribute
private String value = "";

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

}

[quote]
package com.nnk.flowrecharge.config;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

import com.nnk.msgsrv.client.common.XmlUtil;

@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.NONE)
public class InterfaceConfig {

@XmlElement(name = "recharge")
private Recharge recharge = new Recharge();
@XmlElement(name = "query")
private Query query = new Query();
@XmlElement(name = "global")
private Global global = new Global();

public static Map<String, CodeConfig> getRechargecodes() {
return rechargecodes;
}
public Global getGlobal() {
return global;
}
public static Map<String, Prama> getPramas() {
return Pramas;
}
public static void setPramas(Map<String, Prama> Pramas) {
InterfaceConfig.Pramas = Pramas;
}
private static InterfaceConfig instance = null;

private static Map<String, CodeConfig> rechargecodes = new HashMap<String, CodeConfig>();
private static Map<String, CodeConfig> queryCodes = new HashMap<String, CodeConfig>();
private static Map<String, Prama> Pramas = new HashMap<String, Prama>();
public Recharge getRecharge() {
return recharge;
}

public Query getQuery() {
return query;
}
public static Map<String, CodeConfig> getQueryCodes() {
return queryCodes;
}
public static synchronized InterfaceConfig getInstance() {
if (instance == null) {
try {
instance = XmlUtil.loadXmlFromFile(InterfaceConfig.class, "config/interface.xml");

rechargecodes.clear();
List<CodeConfig> _codes = instance.getRecharge().getCodes();
for (CodeConfig cc : _codes) {
rechargecodes.put(cc.getKey(), cc);
}
queryCodes.clear();
_codes = instance.getQuery().getCodes();
for (CodeConfig cc : _codes) {
queryCodes.put(cc.getKey(), cc);
}

Pramas.clear();
List<Prama> _pramas = instance.getGlobal().getPramas();
for (Prama cc : _pramas) {
Pramas.put(cc.getKey(), cc);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
return instance;
}
}

@XmlAccessorType(XmlAccessType.NONE)
class Recharge {

@XmlElement(name = "code")
@XmlElementWrapper(name = "codes")
private List<CodeConfig> codes = new ArrayList<CodeConfig>();

public List<CodeConfig> getCodes() {
return codes;
}

public void setCodes(List<CodeConfig> codes) {
this.codes = codes;
}

}
@XmlAccessorType(XmlAccessType.NONE)
class Query
{
@XmlElement(name = "code")
@XmlElementWrapper(name = "codes")
private List<CodeConfig> codes = new ArrayList<CodeConfig>();
public List<CodeConfig> getCodes() {
return codes;
}
public void setCodes(List<CodeConfig> codes) {
this.codes = codes;
}
}
@XmlAccessorType(XmlAccessType.NONE)
class Global{
@XmlElement(name = "prama")
@XmlElementWrapper(name = "pramas")
private List<Prama> Pramas = new ArrayList<Prama>();

public List<Prama> getPramas() {
return Pramas;
}

public void setPramas(List<Prama> Pramas) {
this.Pramas = Pramas;
}
}[/quote]
[quote]
@XmlAttribute 表示属性节点绑定 @XmlRootElement 根节点 @XmlAccessorType xml接触类型,XmlAccessType.NONE表示没有注解标注的不进行动态绑定 XmlAccessType.Filed 表示javabean属性字段都进行绑定,无需再加注解
@XmlElement表示节点绑定
[/quote]

jaxb解析xml工具类的更多相关文章

  1. android 解析XML 工具类

    /** * Created by John on 2016/3/29. */ public class XmlParser { private static final String ns = nul ...

  2. XML工具类 - XmlUtils.java

    XML工具类,提供序列化XML.反序列化XML.获取指定节点的值的方法. 源码如下:(点击下载 - XmlUtils.java.dom4j-1.6.1.jar.xstream-1.4.7.jar ) ...

  3. JaxbUtil转json转XML工具类

    json转换为XML工具类 package com.cxf.value; import org.springframework.util.StringUtils; import javax.xml.b ...

  4. java 解析excel工具类

      java 解析excel工具类 CreateTime--2018年3月5日16:48:08 Author:Marydon ReadExcelUtils.java import java.io.Fi ...

  5. Excel解析easyexcel工具类

    Excel解析easyexcel工具类 easyexcel解决POI解析Excel出现OOM <!-- https://mvnrepository.com/artifact/com.alibab ...

  6. Java常用工具类---XML工具类、数据验证工具类

    package com.jarvis.base.util; import java.io.File;import java.io.FileWriter;import java.io.IOExcepti ...

  7. DOM4j XML 工具类

    之前项目有跟客户系统对接一个webservice系统,该接口有传参和返回都是xml,所以找时间百度研究了一下dom4j,dom4j是一个Java的XML API,是jdom的升级品,用来读写XML文件 ...

  8. flink---实时项目--day02-----1. 解析参数工具类 2. Flink工具类封装 3. 日志采集架构图 4. 测流输出 5. 将kafka中数据写入HDFS 6 KafkaProducer的使用 7 练习

    1. 解析参数工具类(ParameterTool) 该类提供了从不同数据源读取和解析程序参数的简单实用方法,其解析args时,只能支持单只参数. 用来解析main方法传入参数的工具类 public c ...

  9. JAXB解析XML为对象

    JAXB支持注解将XML转化为对象,具体看一个简单的例子: <?xml version="1.0" encoding="utf-8"?> <A ...

随机推荐

  1. SQLserver本地数据库开发

    远程端数据库中生成脚本 注意 远程端的数据库 是中文版的还是英文版的,一般我们装的是英文版的, 如果远程端数据库是中文版的,那么我们本地的是英文版,在生成的脚本那需要修改,同时去除相应的路劲代码. 修 ...

  2. 03 vue项目结构

    上一篇已介绍根据vue-cli创建项目,本篇介绍根据vue-cli官方脚手架创建的项目的项目结构. 一.图看结构 build  [webpack配置]         webpack相关配置,都已经配 ...

  3. app测试基础知识之命令

    app测试点:功能测试,安全测试,用户体验测试,交叉事件测试,兼容性测试,性能测试,安装/升级/卸载 ,UI测试 命令操作: adb connect 名 adb devices adb  instal ...

  4. 调用百度api的原理流程

    1.为了实现酒店地址的定位 2.使用可视化便捷的百度地图API生成器:设置公司的地址和地图等级 3.设置地图的滚轮.缩放功能 4.获取代码,拷贝到html页面中 5.申请秘钥,在html中引用地图AP ...

  5. 强化学习应用于游戏Tic-Tac-Toe

    Tic-Tac-Toe游戏为3*3格子里轮流下棋,一方先有3子成直线的为赢家. 参考代码如下,我只删除了几个没用的地方: ####################################### ...

  6. Ubuntu安装deepin wine版QQ

    1.安装deepin wine环境 https://github.com/wszqkzqk/deepin-wine-ubuntu 直接下载zip包(或者用git方式克隆) 使用unzip解压到指定文件 ...

  7. C++学习笔记-C++对C语言的扩充和增强

    C++兼容C,在C的基础上学习C++是一个不错的选择,也能够更好的了解C与C++的区别与联系. 变量定义 C语言中的变量都必须在作用域开始的位置定义 C++中更强调语言的实用性,所有的变量都可以在需要 ...

  8. 给Date的构造函数添加属性和方法

    let d = Date.prototype; Object.defineProperties(d, { 'year': { get: function () { return this.getFul ...

  9. Centos磁盘空间不足,找不到占用文件

    服务器报警,系统"/"空间不足,但找不到哪些文件占用. 1.使用du -sh *,层层目录查看依然找不到 2.使用"lsof / | grep -i delete&quo ...

  10. [爬虫] BeautifulSoup库

    Beautiful Soup库基础知识 Beautiful Soup库是解析xml和html的功能库.html.xml大都是一对一对的标签构成,所以Beautiful Soup库是解析.遍历.维护“标 ...