引用
jaxb jdk 自带的解析xml的一种方式支持,只需要用注解对javabean进行数据绑定
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); } }
引用
动态生成xml 的javabean
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; } }
引用
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;
}
}
引用
@XmlAttribute 表示属性节点绑定 @XmlRootElement 根节点 @XmlAccessorType xml接触类型,XmlAccessType.NONE表示没有注解标注的不进行动态绑定 XmlAccessType.Filed 表示javabean属性字段都进行绑定,无需再加注解
@XmlElement表示节点绑定