Java中XML如何转为BEAN

在网络通讯中,对方传的数据经常是XML格式包装的数据集合。在Java开发中,我们如何将XML转成Java实体类呢?

对方发送的报文

<ns2:response xmlns:ns2="http://service.zxl.cn/test/xml">
	<school>
		<location>长江边上</location>
		<level>初中</level>
		<scale>很大</scale>
	</school >
	<student>
		<height>150cm</height>
		<gender>女校</gender>
        <scale>很多</scale>
	</student>
	<teacher>
		<teacherAttribute id="gender">
			<attributeValue>男的</attributeValue>
		</teacherAttribute>
		<teacherAttribute id="height">
			<attributeValue>177cm</attributeValue>
		</teacherAttribute>
		<teacherAttribute id="age">
			<attributeValue>22岁</attributeValue>
		</teacherAttribute>
	</teacher>
</ns2:response>

在实体类上加上注解

在这里插入图片描述

package com.zxl.bean.xmlStudy;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@Data
@AllArgsConstructor
@NoArgsConstructor

@XmlRootElement(name = "response",namespace="http://service.zxl.cn/test/xml")
@XmlAccessorType(value = XmlAccessType.FIELD)
public class XmlInfo {

    @XmlElement(name = "school")
    private School school;

    @XmlElement(name = "student")
    private Student student;

    @XmlElement(name = "teacher")
    private Teacher teacher;
}

@XmlRootElement 根节点的注解,里面填两个参数,
一个是name,就是根节点的名称,叫做response我们就填response。
一个是namespace,根节点的命名空间,填入xml中后面跟上的url。(不填报错)

@XmlAccessorType(XmlAccessType.FIELD)
将所有属性均绑定在bean上(默认只绑public的属性,加上后绑定所有的)

@XmlElement(name = “school”)
报文分为3部分,将对应的部分名称填上
在这里插入图片描述

package com.zxl.bean.xmlStudy;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@Data
@AllArgsConstructor
@NoArgsConstructor

@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlRootElement(name = "school")
public class School {

    @XmlElement(name = "location")
    private String location;
    @XmlElement(name = "level")
    private String level;
    @XmlElement(name = "scale")
    private String scale;
}

继续填上@XmlRootElement和 @XmlElement注解
在这里插入图片描述

package com.zxl.bean.xmlStudy;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@Data
@AllArgsConstructor
@NoArgsConstructor

@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlRootElement(name = "student")
public class Student {

    @XmlElement(name = "height")
    private String height;

    @XmlElement(name = "gender")
    private String gender;

    @XmlElement(name = "scale")
    private String scale;
}

继续填上@XmlRootElement和 @XmlElement注解
在这里插入图片描述
这里的id属性,没有单独放出来
我们在建立一个类,单独放id和里面的attributeValue

package com.zxl.bean.xmlStudy;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor

@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlRootElement(name = "teacher")
public class Teacher {

    @XmlElement(name = "teacherAttribute")
    private List<TeacherAttribute> teacherAttributes;
}

package com.zxl.bean.xmlStudy;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.xml.bind.annotation.*;

@Data
@AllArgsConstructor
@NoArgsConstructor

@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlRootElement(name = "school")
public class TeacherAttribute {

    @XmlAttribute(name="id")
    private String id;

    @XmlElement(name = "attributeValue")
    private String attributeValue;

}

使用 @XmlAttribute注解,将属性值绑定到id上

解析代码

  private String getXml() throws IOException {
        // 测试xml转bean
        InputStream path = this.getClass().getResourceAsStream("/1.xml");
        BufferedReader reader = new BufferedReader(new InputStreamReader(path,"utf-8"));
        StringBuffer buffer=new StringBuffer();
        String line="";
        while((line=reader.readLine())!=null){
            buffer.append(line);
        }
        reader.close();
        return buffer.toString();
    }

读取xml的方法

    @GetMapping("/testXml2Bean")
    public XmlInfo testXml2Bean() throws IOException, JAXBException {
        String xml = this.getXml();
        StringReader reader = new StringReader(xml);
        JAXBContext jaxbContext = JAXBContext.newInstance(XmlInfo.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        XmlInfo xmlVo = (XmlInfo) jaxbUnmarshaller.unmarshal(reader);

        return xmlVo;
    }

转换方法

测试

在这里插入图片描述
成功转换!
(有问题留言)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值