登录  /  注册
首页 > Java > java教程 > 正文

java 中xml转换为Bean实例解析(纯代码)

php是最好的语言
发布: 2018-08-08 13:42:35
原创
3216人浏览过

最近用到,记录一个自己写的demo

  1. 在根元素上使用@xmlrootelement注解,name为元素名

  2. 子元素属性使用@XmlElement,name为元素名

  3. 若有属性,例如,则使用@XmlAttribute,name为属性名

xml:

<?xml version="1.0" encoding="UTF-8"?>
<employees>
    <employee>
        <userId>johnsmith@company.com</userId>
        <password>abc123_</password>
        <name>John Smith</name>
        <age>24</age>
        <gender>Male</gender>
    </employee>
    <employee>
        <userId>christinechen@company.com</userId>
        <password>123456</password>
        <name>Christine Chen</name>
        <age>27</age>
        <gender>Female</gender>
    </employee>
</employees>
登录后复制

Employees:

import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "employees")
public class Employees {

    private List<Employee> eList;
    @XmlElement(name = "employee")
    public List<Employee> geteList() {
        return eList;
    }

    public void seteList(List<Employee> eList) {
        this.eList = eList;
    }

}
登录后复制

Employee:

import javax.xml.bind.annotation.XmlElement;

public class Employee {
    private String userId;
    private String password;
    private String name;
    private String age;
    private String gender;
    @Override
    public String toString() {
        return "Employee [userId=" + userId + ", password=" + password
                + ", name=" + name + ", age=" + age + ", gender=" + gender
                + "]";
    }
    @XmlElement(name="userId")
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    @XmlElement(name="password")
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @XmlElement(name="name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @XmlElement(name="age")
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    @XmlElement(name="gender")
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }

}
登录后复制

解析类

 public static void main(String[] args) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(Employees.class);
        Unmarshaller createUnmarshaller = context.createUnmarshaller();
        Object unmarshal = createUnmarshaller.unmarshal(
                new File("D:/java/workspacedev/JavaTest/xml/employees.xml"));
        Employees em = (Employees) unmarshal;
        List<Employee> list = em.geteList();
        for (Employee employee : list) {
            System.out.println(employee);
        }
        
    }
登录后复制

相关推荐:

Java&Xml教程(八)使用JDOM将Java对象转换为XML

Jaxb2实现Bean与xml互转的示例代码详解

以上就是java 中xml转换为Bean实例解析(纯代码)的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号