• JABX简单介绍2


    简单例子step2
    Book.java

     1package step2;
     2
     3import javax.xml.bind.annotation.XmlAccessType;
     4import javax.xml.bind.annotation.XmlAccessorType;
     5/*    @XmlAccessorType此注释可控制类中的属性和字段的默认序列化
     6 *  value=XmlAccessType.PROPERTY表示JAXB 绑定类中的每个
     7 *获取方法/设置方法对将会自动绑定到 XML
     8 * */

     9@XmlAccessorType(value = XmlAccessType.PROPERTY)
    10public class Book {
    11    
    12    private String id;
    13    private String name;
    14    private float price;
    15    
    16    public String getId() {
    17        return id;
    18    }

    19    public void setId(String id) {
    20        this.id = id;
    21    }

    22    public String getName() {
    23        return name;
    24    }

    25    public void setName(String name) {
    26        this.name = name;
    27    }

    28    public float getPrice() {
    29        return price;
    30    }

    31    public void setPrice(float price) {
    32        this.price = price;
    33    }

    34    @Override
    35    public String toString() {
    36        return "Book [id=" + id + ",name=" + name + ",price=" + price + "]";
    37    }

    38}

    39


    Customer.java

    1package step2;
    2import java.util.Set;
    3
    4import javax.xml.bind.annotation.XmlElement;
    5import javax.xml.bind.annotation.XmlElementWrapper;
    6import javax.xml.bind.annotation.XmlRootElement;
    7import javax.xml.bind.annotation.XmlType;
    8
    9@XmlRootElement
    10@XmlType(propOrder ={ "id", "name", "age","book"})
    11publicclass Customer {
    12 String name;
    13 int age;
    14 int id;
    15 Set<Book> book;
    16 @XmlElement(name="name")
    17 public String getName() {
    18 return name;
    19 }

    20
    21 publicvoid setName(String name) {
    22 this.name = name;
    23 }

    24
    25 @XmlElement(name="age")
    26 publicint getAge() {
    27 return age;
    28 }

    29
    30 publicvoid setAge(int age) {
    31 this.age = age;
    32 }

    33 @XmlElement(name="id")
    34 publicint getId() {
    35 return id;
    36 }

    37
    38 publicvoid setId(int id) {
    39 this.id = id;
    40 }

    41
    42
    43 @Override
    44 public String toString() {
    45 return"Customer [id="+ id +",name="+ name +",age="+ age +",book="+ book +"]";
    46 }

    47 @XmlElementWrapper(name="books")
    48 @XmlElement(name="book")
    49 public Set<Book> getBook() {
    50 return book;
    51 }

    52
    53 publicvoid setBook(Set<Book> book) {
    54 this.book = book;
    55 }

    56
    57
    58}

    59



    Object2XmlDemo.java

    1package step2;
    2
    3import java.io.File;
    4import java.util.HashSet;
    5import java.util.Set;
    6
    7import javax.xml.bind.JAXBContext;
    8import javax.xml.bind.JAXBException;
    9import javax.xml.bind.Marshaller;
    10
    11//Marshaller
    12publicclass Object2XmlDemo {
    13 publicstaticvoid main(String[] args) {
    14
    15 Customer customer =new Customer();
    16 customer.setId(100);
    17 customer.setName("suo");
    18 customer.setAge(29);
    19
    20 Book book =new Book();
    21 book.setId("1");
    22 book.setName("哈里波特");
    23 book.setPrice(100);
    24
    25 Book book2 =new Book();
    26 book2.setId("2");
    27 book2.setName("苹果");
    28 book2.setPrice(50);
    29
    30 Set<Book> bookSet =new HashSet<Book>();
    31 bookSet.add(book);
    32 bookSet.add(book2);
    33
    34 customer.setBook(bookSet);
    35
    36 try{
    37 File file =new File("H:\\Xfile24.xml");
    38 JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
    39 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    40 // output pretty printed
    41 jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    42 jaxbMarshaller.marshal(customer, file);
    43 jaxbMarshaller.marshal(customer, System.out);
    44 }
    catch (JAXBException e) {
    45 e.printStackTrace();
    46 }

    47 }

    48}

    49


    执行效果

    1<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    2<customer>
    3 <id>100</id>
    4 <name>suo</name>
    5 <age>29</age>
    6 <books>
    7 <book>
    8 <id>1</id>
    9 <name>鍝堥噷娉㈢壒</name>
    10 <price>100.0</price>
    11 </book>
    12 <book>
    13 <id>2</id>
    14 <name>鑻规灉</name>
    15 <price>50.0</price>
    16 </book>
    17 </books>
    18</customer>
    19
  • 相关阅读:
    mybatis中的延迟加载
    MyBatis的mapper
    MyBatis的resultMap
    mybatis入门
    mybatis中的#和$的区别(转)
    操作日志记录
    SpringMVC中的异常处理集锦
    vue.js的package.json相关问题解惑
    git的常用操作指令
    http协议参数详解
  • 原文地址:https://www.cnblogs.com/redcoatjk/p/3562353.html
Copyright © 2020-2023  润新知