打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
jaxb解析xml为对象例子
通过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 如下
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 文件如下
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类
Java代码 
 
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字符串方法
Java代码 
 
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;
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
JAXB2.0 使用文档
使用jaxb将XML转化为JAVA BEAN
Jaxb 完全手册
JAXB--简单应用(一)
通过JAXB请求和解析WebService
JAXB(Java Architecture for XML Binding)新手學習筆記
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服