jaxb解析xml为对象例子
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Java代码
1.publicstaticvoidgernateConfig()throwsException{
2.StringBuffer buffer =null;
3.JAXBContext jaxbContext;
4.try{ห้องสมุดไป่ตู้
5.//读入xml文件流
6.InputStream is = Excecute.class.getResourceAsStream(“/conf/config.xml”);
17.@XmlAttribute
18.privateString port;
19.@XmlAttribute
20.privateString server;
21.
22.
23.publicString getStart() {
24.returnstart;
25.}
26.publicvoidsetStart(String start) {
27.this.start = start;
28.}
29.publicString getIp() {
30.returnip;
31.}
32.publicvoidsetIp(String ip) {
33.this.ip = ip;
34.}
35.publicString getPort() {
36.returnport;
Vo.java 如下
Java代码
1.importjavax.xml.bind.annotation.XmlAccessType;
2.importjavax.xml.bind.annotation.XmlAccessorType;
3.importjavax.xml.bind.annotation.XmlAttribute;
2.if(root !=null) {
3.try{
4.JAXBContext context = JAXBContext.newInstance(Root.class);
5.Marshaller m = context.createMarshaller();
6.m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
11.buffer.append(line);
12.}
13.
14.//加载映射bean类
15.jaxbContext = JAXBContext.newInstance(Root.class);
16.//创建解析
17.Unmarshaller um = jaxbContext.createUnmarshaller();
9.})
10.@XmlRootElement(name ="[color=red]template[/color]")
11.publicclassVo {
12.
13.@XmlAttribute
14.privateString start;
15.@XmlAttribute
16.privateString ip;
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的映射文件。
18.StreamSource streamSource =newStreamSource(newStringReader(buffer.toString()));
19.Root root = (Root) um.unmarshal(streamSource);
20.}catch(Exception e) {
7.BufferedReader in =newBufferedReader(newInputStreamReader(is));
8.buffer =newStringBuffer();
9.String line ="";
10.while((line = in.readLine()) !=null) {
45.this.server = server;
46.}
47.}
48.
该文件对应xml文件中 循环节点 template 元素
Root.java 文件如下
Java代码
1.importjava.util.ArrayList;
2.importjava.util.List;
3.
4.importjavax.xml.bind.annotation.XmlAccessType;
5.importjavax.xml.bind.annotation.XmlAccessorType;
6.importjavax.xml.bind.annotation.XmlRootElement;
7.importjavax.xml.bind.annotation.XmlType;
8.
9.@XmlAccessorType(XmlAccessType.FIELD)
该文件对应xml文件中循环节点template元素
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
37.}
38.publicvoidsetPort(String port) {
39.this.port = port;
40.}
41.publicString getServer() {
42.returnserver;
43.}
44.publicvoidsetServer(String server) {
10.@XmlType(name ="", propOrder = {
11.})
12.@XmlRootElement(name ="[color=blue]root[/color]")
13.publicclassRoot {
14.
15.protectedList<Vo> [color=red]template[/color];
7.Writer writer =newStringWriter();
8.m.marshal(root, writer);
9.try{
10.String xml = writer.toString();
11.writer.flush();
12.writer.close();
13.returnxml;
4.importjavax.xml.bind.annotation.XmlRootElement;
5.importjavax.xml.bind.annotation.XmlType;
6.
7.@XmlAccessorType(XmlAccessType.FIELD)
8.@XmlType(name ="", propOrder = {
14.}catch(IOException e) {
15.return"";
16.}
17.}catch(Exception e) {
18.thrownewException("失败!");
19.}
20.}else{
21.returnnull;
22.}
23.}
16.
17.publicList<Vo> getTemplateList() {
18.if(template ==null) {
19.template =newArrayList<Vo>();
20.}
21.returnthis.template;
22.}
23.}
注意bean对象与xml映射的关系。
5、 解析xml为bean类
21.e.printStackTrace();
22.thrownewException(e.getMessage());
23.}
24.}
6、 把bean对象生成xml字符串方法
Java代码
1.publicstaticString gernateConfigXml(Root root)throwsException {
1.publicstaticvoidgernateConfig()throwsException{
2.StringBuffer buffer =null;
3.JAXBContext jaxbContext;
4.try{ห้องสมุดไป่ตู้
5.//读入xml文件流
6.InputStream is = Excecute.class.getResourceAsStream(“/conf/config.xml”);
17.@XmlAttribute
18.privateString port;
19.@XmlAttribute
20.privateString server;
21.
22.
23.publicString getStart() {
24.returnstart;
25.}
26.publicvoidsetStart(String start) {
27.this.start = start;
28.}
29.publicString getIp() {
30.returnip;
31.}
32.publicvoidsetIp(String ip) {
33.this.ip = ip;
34.}
35.publicString getPort() {
36.returnport;
Vo.java 如下
Java代码
1.importjavax.xml.bind.annotation.XmlAccessType;
2.importjavax.xml.bind.annotation.XmlAccessorType;
3.importjavax.xml.bind.annotation.XmlAttribute;
2.if(root !=null) {
3.try{
4.JAXBContext context = JAXBContext.newInstance(Root.class);
5.Marshaller m = context.createMarshaller();
6.m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
11.buffer.append(line);
12.}
13.
14.//加载映射bean类
15.jaxbContext = JAXBContext.newInstance(Root.class);
16.//创建解析
17.Unmarshaller um = jaxbContext.createUnmarshaller();
9.})
10.@XmlRootElement(name ="[color=red]template[/color]")
11.publicclassVo {
12.
13.@XmlAttribute
14.privateString start;
15.@XmlAttribute
16.privateString ip;
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的映射文件。
18.StreamSource streamSource =newStreamSource(newStringReader(buffer.toString()));
19.Root root = (Root) um.unmarshal(streamSource);
20.}catch(Exception e) {
7.BufferedReader in =newBufferedReader(newInputStreamReader(is));
8.buffer =newStringBuffer();
9.String line ="";
10.while((line = in.readLine()) !=null) {
45.this.server = server;
46.}
47.}
48.
该文件对应xml文件中 循环节点 template 元素
Root.java 文件如下
Java代码
1.importjava.util.ArrayList;
2.importjava.util.List;
3.
4.importjavax.xml.bind.annotation.XmlAccessType;
5.importjavax.xml.bind.annotation.XmlAccessorType;
6.importjavax.xml.bind.annotation.XmlRootElement;
7.importjavax.xml.bind.annotation.XmlType;
8.
9.@XmlAccessorType(XmlAccessType.FIELD)
该文件对应xml文件中循环节点template元素
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
37.}
38.publicvoidsetPort(String port) {
39.this.port = port;
40.}
41.publicString getServer() {
42.returnserver;
43.}
44.publicvoidsetServer(String server) {
10.@XmlType(name ="", propOrder = {
11.})
12.@XmlRootElement(name ="[color=blue]root[/color]")
13.publicclassRoot {
14.
15.protectedList<Vo> [color=red]template[/color];
7.Writer writer =newStringWriter();
8.m.marshal(root, writer);
9.try{
10.String xml = writer.toString();
11.writer.flush();
12.writer.close();
13.returnxml;
4.importjavax.xml.bind.annotation.XmlRootElement;
5.importjavax.xml.bind.annotation.XmlType;
6.
7.@XmlAccessorType(XmlAccessType.FIELD)
8.@XmlType(name ="", propOrder = {
14.}catch(IOException e) {
15.return"";
16.}
17.}catch(Exception e) {
18.thrownewException("失败!");
19.}
20.}else{
21.returnnull;
22.}
23.}
16.
17.publicList<Vo> getTemplateList() {
18.if(template ==null) {
19.template =newArrayList<Vo>();
20.}
21.returnthis.template;
22.}
23.}
注意bean对象与xml映射的关系。
5、 解析xml为bean类
21.e.printStackTrace();
22.thrownewException(e.getMessage());
23.}
24.}
6、 把bean对象生成xml字符串方法
Java代码
1.publicstaticString gernateConfigXml(Root root)throwsException {