zoukankan      html  css  js  c++  java
  • [置顶] JAXB Hello World

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

    1.     Marshalling       - 将Java对象转换成XML文件。
    2.     Unmarshalling - 将XML内容转换成Java对象。

        本文使用到的相关技术:

    1.     JDK 1.6
    2.     JAXB 2.0

        使用JAXB很简单。只需用JAXB注解标注对象,然后使用jaxbMarshaller.marshal() 或者 jaxbMarshaller.unmarshal() 来做 XML/Object 的转换工作。

      1.JAXB 依赖 

        如果你使用的时JDK1.6或以上版本,你不需要添加额外的类库,因为JAXB被绑定在JDK1.6中。

        注释:

                 如果你使用的时JDK < 1.6,你需要将下载的"jaxb-api.jar"和"jaxb-impl.jar"包添加到你的项目CLASSPATH中。

         

      2.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;
    	}
     
    }


        3.对象转换成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>


     

        4.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();
    	  }
     
    	}
    }


        输出:

    Customer [name=benson, age=23, id=100]


     

  • 相关阅读:
    实例!使用Idea创建SSM框架的Maven项目
    springboot开发中的领域模型pojo
    JDK源码阅读:Object类阅读笔记
    DevSecOps: JIRA、Confluence工具、JIRA插件-Xray、eazybi、FishEye、Crucible、jenkins
    MySQL监控及优化
    二叉树 红黑树 B树 B+树 理解
    mysql高性能分页语句_如何优化Mysql千万级快速分页
    使用.Net MinIO SDK 踩的坑
    Windows下Minio介绍、安装及使用、密码修改
    使用docker mediawiki,搭建网页wiki
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3002696.html
Copyright © 2011-2022 走看看