服务器之家

服务器之家 > 正文

使用JAXBContext轻松实现Java和xml的互相转换方式

时间:2021-11-12 13:21     来源/作者:它山之石,可以攻玉

JAXBContext实现Java和xml的互相转换

类文件注解:@XmlRootElement不可缺少

?
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
<span style="font-size:14px;">@XmlRootElement
public class Man { 
    private String name; 
    private int age;
    public Student() { 
        super(); 
    }
    public Student(String name, int age) { 
        super(); 
        this.name = name; 
        this.age = age; 
    
    public String getName() { 
        return name; 
    
    public void setName(String name) { 
        this.name = name; 
    
    public int getAge() { 
        return age; 
    
    public void setAge(int age) { 
        this.age = age; 
    }  
}  </span>

Java 转换 Xml:

?
1
2
3
4
5
6
7
8
9
10
11
@Test
public void testJava2Xml(){ 
    try
        JAXBContext jc = JAXBContext.newInstance(Man.class); 
        Marshaller ms = jc.createMarshaller(); 
        Man man = new Man("man",100); 
        ms.marshal(man, System.out); 
    } catch (JAXBException e) { 
        e.printStackTrace(); 
    
}

Xml 转换 Java:

?
1
2
3
4
5
6
7
8
@Test
public void testXml2Java() throws JAXBException{ 
    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Man><age>100</age><name>man</name></Man>"
    JAXBContext jc = JAXBContext.newInstance(Man.class); 
    Unmarshaller unmar = jc.createUnmarshaller(); 
    Man man = (Man) unmar.unmarshal(new StringReader(xml)); 
    System.out.println(man.getName()); 
}

JAXBContext 解析 xml

前提:

?
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
package com.sgcc.load.vo;
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.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement(name = "gldxresult")
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlAnalysis { // 泛化, 聚合
    @XmlElement(name = "success")
    private String success;
    @XmlElement(name = "totalnum")
    private String totalnum;
 
    @XmlElement(name = "accounts")
    List<Accounts> accounts = new ArrayList<>();
    
    public List<Accounts> getAccounts() {
        return accounts;
    }
 
    public void setAccounts(List<Accounts> accounts) {
        this.accounts = accounts;
    }
 
    public String getSuccess() {
        return success;
    }
 
    public void setSuccess(String success) {
        this.success = success;
    }
 
    public String getTotalnum() {
        return totalnum;
    }
 
    public void setTotalnum(String totalnum) {
        this.totalnum = totalnum;
    
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.sgcc.load.vo;
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.XmlElement;
@SuppressWarnings("serial")
@XmlAccessorType(XmlAccessType.FIELD)
public class Accounts extends ArrayList<Account>{
   
   @XmlElement(name = "account")
   public List<Account> getAccount() {
            return this;
   }  
}
?
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
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.sgcc.load.vo;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class Account {
    @XmlElement(name = "seqid")
    private String seqid;
    @XmlElement(name = "dxid")
    private String dxid;
    @XmlElement(name = "compid")
    private String compid;
    @XmlElement(name = "rspcode")
    private String rspcode;
    @XmlElement(name = "code")
    private String code;
    @XmlElement(name = "name")
    private String name;     
   
    public String getSeqid() {
        return seqid;
    }
    public void setSeqid(String seqid) {
        this.seqid = seqid;
    }
    public String getDxid() {
        return dxid;
    }
    public void setDxid(String dxid) {
        this.dxid = dxid;
    }
    public String getCompid() {
        return compid;
    }
    public void setCompid(String compid) {
        this.compid = compid;
    }
    public String getRspcode() {
        return rspcode;
    }
    public void setRspcode(String rspcode) {
        this.rspcode = rspcode;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }     
}

测试:

?
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
package com.sgcc.load.test;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import com.sgcc.load.vo.Accounts;
import com.sgcc.load.vo.XmlAnalysis;
import groovyjarjarcommonscli.ParseException;
public class Test {
    
    private static XmlAnalysis transToVOs(String webserviceMsg) throws JAXBException, ParseException {
        final StringReader reader = new StringReader(webserviceMsg);
        JAXBContext ctx=JAXBContext.newInstance(new XmlAnalysis().getClass());
        Unmarshaller um=ctx.createUnmarshaller();
        XmlAnalysis ds=(XmlAnalysis)um.unmarshal(reader);      
        return ds;
    }
    public static void main(String[] args) {
        String xmlMsg = "<?xml version=\"1.0\" encoding=\"GBK\"?> \n<gldxresult><success>0</success><totalnum>1</totalnum>"+
"<accounts><yzy>hahaha</yzy><account><seqid></seqid><dxid>1136005118</dxid><compid>0603</compid><rspcode>New0001</rspcode><rspmsg>值:00 没有匹配到分类</rspmsg>"+
"<code>EHBSXPZYFDC000</code><name>哈哈哈哈哈哈哈哈哈</name></account></accounts></gldxresult>";
        
        try{
            XmlAnalysis transToVOs = transToVOs(xmlMsg);
            for(int index = 0;index<transToVOs.getAccounts().get(0).getAccount().size();index++){
                System.out.println(transToVOs.getAccounts().get(0).getAccount().get(index).getName());
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }  
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/y534560449/article/details/73250062

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部