通过jaxb方式把xml文件映射成bean对象。
1、新建java工程或者web工程都可以。
2、通过精简必须导入jar包
activation.jar
jaxb-api-2.0.jar
jaxb-impl-2.0.1.jar
jsr173_api-1.0.jar
3、 新建xml文件
<?xml version="1.0" encoding="UTF-8"?>
<
root>
<
template
start="true"
ip="127.0.0.1"
port="3344"
server="server"
/>
<template ... ... />
</root>
4、新建bean对象,xml的映射文件。
Vo.java 如下
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
})
@XmlRootElement(name = "[color=red]template[/color]")
public class Vo {
@XmlAttribute
private String start;
@XmlAttribute
private String ip;
@XmlAttribute
private String port;
@XmlAttribute
private String server;
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getServer() {
return server;
}
public void setServer(String server) {
this.server = server;
}
}
该文件对应xml文件中 循环节点 template 元素
Root.java 文件如下
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
})
@XmlRootElement(name = "[color=blue]root[/color]")
public class Root {
protected List<Vo> [color=red]template[/color];
public List<Vo> getTemplateList() {
if (template == null) {
template = new ArrayList<Vo>();
}
return this.template;
}
}
注意bean对象与xml映射的关系。
5、 解析xml为bean类
public static void gernateConfig() throws Exception{
StringBuffer buffer = null;
JAXBContext jaxbContext;
try {
//读入xml文件流
InputStream is = Excecute.class.getResourceAsStream(“/conf/config.xml”);
BufferedReader in = new BufferedReader(new InputStreamReader(is));
buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
}
//加载映射bean类
jaxbContext = JAXBContext.newInstance(Root.class);
//创建解析
Unmarshaller um = jaxbContext.createUnmarshaller();
StreamSource streamSource = new StreamSource(new StringReader(buffer.toString()));
Root root = (Root) um.unmarshal(streamSource);
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e.getMessage());
}
}
6、 把bean对象生成xml字符串方法
public static String gernateConfigXml(Root root) throws Exception {
if (root != null) {
try {
JAXBContext context = JAXBContext.newInstance(Root.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Writer writer = new StringWriter();
m.marshal(root, writer);
try {
String xml = writer.toString();
writer.flush();
writer.close();
return xml;
} catch (IOException e) {
return "";
}
} catch (Exception e) {
throw new Exception("失败!");
}
} else {
return null;
}
}
附件为仅包含jaxb解析,反解析过程java工程代码。
分享到:
相关推荐
使用jaxb生成XML例子,含有例子和注解解析
jaxb解析生成xml例子
教你使用jaxb解析xml,介绍了主要的注解,以及核心api
JAXB教程 JAXB JAXB插件 里面有很详细的使用说明,看了就知道,JAXB解析XML真的很好用
JAXB工具类 xml转为java对象 java对象转为xml ,本人亲测,可以使用!!!
使用woodstax+jaxb进行xml的流解析,包括解析类,解析文件,所需jar包,带注解的实体类。提高了解析效率,减少了内存消耗。
JAVA JAXB 解析XML嵌套子节点为字符串
NULL 博文链接:https://luyuwww.iteye.com/blog/1988355
jaxb将xml转换为对象或将对象转换为xml。测试通过。
jaxb xml转化为对象,对象转化为xml,xml转化为xsd,数据库表转化为对象
NULL 博文链接:https://xiongpan0807.iteye.com/blog/1832124
该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例文档。从另一方面来讲,JAXB提供了快速而简便的方法将XML模式绑定到Java表示,从而使得Java开发者在Java应用...
JAXB解析XML真的很好用,很强大,而且还是JDK自带的,今天自己总结了一个DEMO,自已感觉非常棒,哈哈。。。里面还有JAXB插件,可以自动生成JAVA BEAN,哈哈,当然了賠本的买卖咱不干,那就请需要的宝贝们奉獻点分吧...
jaxb 将xml里面的对象转化为一个个类,大大地简化了xml的相关操作。unmarshal marshal
使用java jdk的JAXB技术实现xml与java对象互相转化代码教程: JDK中JAXB相关的重要Class和Interface:(来源于百度百科JAXB) •JAXBContext类,是应用的入口,用于管理XML/Java绑定信息。 •Marshaller接口,将Java...
JAXB注解 java 关于xml的注解,自动生成xml文件
JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。JAXB与xml相互转换实例。
Java 解析XML性能分析Demo。包含了DOM解析,SAX解析, JDOM解析,DOM4J解析,JAXB解析例子。