zoukankan      html  css  js  c++  java
  • 对象与xml之间的转换_XmlElement

    package step2;
    import java.util.Set;
    
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlElementWrapper;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlType;
    
    @XmlRootElement
    @XmlType(propOrder = { "id", "name", "age","book"})
    public class Customer {
        String name;
        int age;
        int id;
        Set<Book> book;
        @XmlElement(name="name")
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @XmlElement(name="age")
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
        @XmlElement(name="id")
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        
        @Override
        public String toString() {
            return "Customer [id=" + id + ",name=" + name + ",age=" + age + ",book=" + book + "]";
        }
        @XmlElementWrapper(name="books")
        @XmlElement(name="book")
        public Set<Book> getBook() {
            return book;
        }
    
        public void setBook(Set<Book> book) {
            this.book = book;
        }
    
        
    }

    输出xml:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <customer>
        <id>100</id>
        <name>suo</name>
        <age>29</age>
        <books>
            <book>
                <id>1</id>
                <name>哈里波特</name>
                <price>100.0</price>
            </book>
            <book>
                <id>2</id>
                <name>苹果</name>
                <price>50.0</price>
            </book>
        </books>
    </customer>
  • 相关阅读:
    C++ 函数设计原则
    C++ 转换函数搭配友元函数
    C++ 自动转换和强制类型转换(用户自定义类类型)
    C++ rand函数
    C++ 状态成员
    C++ 友元函数
    C++ 运算符重载
    RabbitMQ的简单应用
    Python的正则表达式
    Python的应用小案例
  • 原文地址:https://www.cnblogs.com/xiamengfei/p/4543475.html
Copyright © 2011-2022 走看看