本文实例讲述了java bean与xml互相转换的方法。分享给大家供大家参考,具体如下:
xml和java bean互相转换是一个很有用的功能,因为两者有着前后合作的关系,但解析的过程比较痛苦。下面介绍如何进行xml和java bean的互相转换。
最近项目中用到了xml和java bean的转换
用到xml的时候我们需要遍历解析。。。然后将解析的值放入声明好的java bean,过程很痛苦,结果很美好
我们能不能直接从xml转化到java bean呢,或者直接成java bean转化到xml呢?
答案是ok的
使用jar包:org.nuxeo.common.xmap
写个小例子
xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?xml version= "1.0" encoding= "utf-8" ?>
<books>
<book no= "1" >
<title>java是傻逼</title>
<subject>java</subject>
<subject>xml</subject>
<price> 10 </price>
</book>
<book no= "2" >
<title>c++程序设计</title>
<subject>c++</subject>
<subject>c</subject>
<price> 10 </price>
</book>
</books>
|
books类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import java.util.arraylist;
import java.util.list;
import org.nuxeo.common.xmap.annotation.xnodelist;
import org.nuxeo.common.xmap.annotation.xobject;
@xobject (value = "books" )
public class books {
@xnodelist (value = "book" , type = arraylist. class , componenttype = book. class )
private list< book> book;
public list< book> getbook() {
return book;
}
public void setbook(list< book> book) {
this .book = book;
}
public void addbook(book b){
if (book== null ){
book= new arraylist< book>();
}
book.add(b);
}
}
|
book类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import java.util.arraylist;
import java.util.list;
import org.nuxeo.common.xmap.annotation.xnode;
import org.nuxeo.common.xmap.annotation.xnodelist;
import org.nuxeo.common.xmap.annotation.xobject;
@xobject
public class book {
@xnode ( "title" )
private string title;
@xnode ( "@no" )
private string no;
@xnodelist (value = "subject" , type = arraylist. class , componenttype = string. class )
private list< string> subjects;
@xnode ( "price" )
private int price;
public string gettitle() {
return title;
}
public void settitle(string title) {
this .title = title;
}
public list< string> getsubjects() {
return subjects;
}
public void setsubjects(list< string> subjects) {
this .subjects = subjects;
}
public int getprice() {
return price;
}
public void setprice( int price) {
this .price = price;
}
public string getno() {
return no;
}
public void setno(string no) {
this .no = no;
}
}
|
测试类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
import java.util.arraylist;
import java.util.list;
import org.nuxeo.common.xmap.xmap;
public class xmaptest {
public static void main(string[] args) {
try {
//声明一组书的根节点
books books= new books();
//声明一本书
book book= new book();
//书的类型
list< string> sj= new arraylist< string>();
sj.add( "java" );
sj.add( "xml" );
//设置书节点的属性
book.setprice( 10 );
book.settitle( "java是傻逼" );
book.setsubjects(sj);
book.setno( "1" );
//将书节点添加到跟节点
books.addbook(book);
//声明一本书
book book2= new book();
//书的类型
list< string> sj2= new arraylist< string>();
sj2.add( "c++" );
sj2.add( "c" );
//设置书节点的属性
book2.setprice( 10 );
book2.settitle( "c++程序设计" );
book2.setsubjects(sj2);
book2.setno( "2" );
//将书节点添加到跟节点
books.addbook(book2);
xmap xmp = new xmap();
xmp.register(books. class );
list< string> filters = new arraylist< string>();
system.out.println(xmp.asxmlstring(books, "utf-8" , filters, true ));
} catch (exception e) {
e.printstacktrace();
}
}
}
|
运行上面这个类就可以生成开头所看见的那个xml的内容
解析xml
1
2
3
4
5
|
xmap.register(books. class );
url url = new file( "book.xml" ).tourl();
inputstream in = new fileinputstream( new file( "book.xml" ));
object[] result = (object[]) xmap.loadall(in);
books books=(books)result[ 0 ];
|
数据就完全autowire by name进入到了bean,某些情况下很好用(xml很整齐,每个节点的内容都一样,固定)
xml和java bean互相转换就此实现。
ps:这里再为大家推荐几款相关在线工具供大家参考:
xml在线压缩/格式化工具:https://tool.zzvips.com/t/xml/
希望本文所述对大家java程序设计有所帮助。
原文链接:https://www.cnblogs.com/aademeng/articles/6235950.html