zoukankan      html  css  js  c++  java
  • JAXB XML到java object的转换

    JAXB是Java Architecture for XML Binding的缩写。使用JAXB注解将Java对象转换成XML文件。在这篇教程中,我们将会展示如何使用JAXB来做以下事情:

    1. marshall 将java对象转化成xml文件

    2. unmarshalling 将xml内容转换成java对象

    JAXB 注解(Annotation)

    如果一个对象需要被转换成XML文件,或者从XML文件中生成,该对象需要用JAXB注解来标注。这些注解光凭名字就知道是什么意思了。具体可参考官网:jaxb guide

    package com.jaxb.core;
     
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
     
    @XmlRootElement
    public class Customer {
     
        String name;
        int age;
        int id;
     
        public String getName() {
            return name;
        }
     
        @XmlElement
        public void setName(String name) {
            this.name = name;
        }
     
        public int getAge() {
            return age;
        }
     
        @XmlElement
        public void setAge(int age) {
            this.age = age;
        }
     
        public int getId() {
            return id;
        }
     
        @XmlAttribute
        public void setId(int id) {
            this.id = id;
        }
     
    }

    对象转换成XML

    JAXB marshalling例子,将customer对象转换成XML文件。jaxbMarshaller.marshal()包含了许多重载方法,哪个输出符合你的要求就选择哪个方法。

    package com.jaxb.core;
     
    import java.io.File;
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Marshaller;
     
    public class JAXBExample {
        public static void main(String[] args) {
     
          Customer customer = new Customer();
          customer.setId(100);
          customer.setName("benson");
          customer.setAge(23);
     
          try {
     
            File file = new File("C:\file.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
     
            // output pretty printed
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
     
            jaxbMarshaller.marshal(customer, file);
            jaxbMarshaller.marshal(customer, System.out);
     
              } catch (JAXBException e) {
            e.printStackTrace();
              }
     
        }
    }

    输出:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <customer id="100">
        <age>23</age>
        <name>benson</name>
    </customer>

    XML转换成对象:

    JAXB unmarshalling例子,将XML文件内容转换成customer对象。jaxbMarshaller.unmarshal()包含了许多重载方法,哪个适合你的输出,你就选择哪个方法。

    package com.jaxb.core;
     
    import java.io.File;
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Unmarshaller;
     
    public class JAXBExample {
        public static void main(String[] args) {
     
         try {
     
            File file = new File("C:\file.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
     
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
            System.out.println(customer);
     
          } catch (JAXBException e) {
            e.printStackTrace();
          }
     
        }
    }
  • 相关阅读:
    12套有用的免费 PSD 格式 Android UI 素材
    使用 Canvas 和 JavaScript 创建逼真的下雨效果
    在网页设计中使用漂亮字体的16个优秀例子
    Koala – 开源的前端预处理器语言图形编译工具
    BackgroundCheck – 根据图片亮度智能切换元素样式
    经典网页设计:18个示例展示图片在网页中的使用
    TogetherJS – 酷!在网站中添加在线实时协作功能
    30个令人惊叹的灵感来自自然风光的网站设计《下篇》
    太有才了!创新的街头涂鸦手绘欣赏【中篇】
    15款美丽的设备模板,帮助展示你的 APP
  • 原文地址:https://www.cnblogs.com/xinsheng/p/3899185.html
Copyright © 2011-2022 走看看