`
sillycat
  • 浏览: 2536024 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Java Object to XML in Order with Annotation

 
阅读更多
Java Object to XML in Order with Annotation

Here is the sample Course.java POJO

package com.sillycat.connectors.quickbooks.models;
import javax.xml.bind.annotation.*;
import java.util.Date;
@XmlRootElement(name = "Course")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {
"id",
        "date",
        "name",
        "description",
        "attendees"
})
public class Course {

    private Long id;
    private Integer attendees;
    private String name;
    private String description;
    private Date date;
…snip…
}
Student POJO which demo the complex Object
package com.sillycat.connectors.quickbooks.models;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "Student")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {
    "id”,
    "age”,
    "name”,
    "others”,
    "main" })
public class Student {
private Long id;
private String name;
private Integer age;
private Course main;
private List<Course> others;
…snip…
}
Here is the Main Class to Run that
package com.sillycat.connectors.quickbooks.models;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class ConvertObjectToXmlMain {
public static void main(String[] args) throws JAXBException {
Course c1 = new Course();
c1.setAttendees(100);
c1.setDate(new Date());
c1.setDescription("test desc");
c1.setId(10l);
c1.setName("Yiyi Kang");
Course c2 = new Course();
c2.setAttendees(122);
c2.setDate(new Date());
c2.setDescription("test desc java");
c2.setId(11l);
c2.setName("Java");
Course c3 = new Course();
c3.setAttendees(133);
c3.setDate(new Date());
c3.setDescription("test desc python");
c3.setId(2l);
c3.setName("Python");
Student s = new Student();
List<Course> others = new ArrayList<Course>();
others.add(c2);
others.add(c3);
s.setAge(12);
s.setId(1001l);
s.setMain(c1);
//s.setOthers(others);

// create jaxb context
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
// create jaxb marshaller
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// pretty print xml
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter sw = new StringWriter();
// marshal object
jaxbMarshaller.marshal(s, sw);

String xml = sw.toString();

System.out.println(xml);
}
}

References:
https://stackoverflow.com/questions/9151690/getting-marshall-result-into-string
https://stackoverflow.com/questions/5435138/jaxb-and-property-ordering
https://kodejava.org/how-do-i-define-the-xml-element-order-in-jaxb/
https://memorynotfound.com/convert-java-object-xml-specific-order-using-jax-b/


分享到:
评论

相关推荐

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.2 -------------------------------------------------------------------------------- 目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate...

    Hibernate教程

    6.4.2. 使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. ...

    Mockito的重要入门资料

    InOrder inOrder = inOrder(list); list.add("one"); list.add("two"); inOrder.verify(list).add("one"); inOrder.verify(list).add("two"); } ``` #### Mock对象的重置 有时可能需要重置mock对象的状态,...

    前端-后端java的Util类的工具类

    │ Dao.java │ GeneratorDemo.java │ hibernate.cfg.xml │ HibernateDaoImpl.java │ HibernateSessionFactory.java │ HibernateUtil.java │ JsonUtil.java │ list.txt │ log4j.properties │ ...

    最全Hibernate 参考文档

    5.4.2. 使用 JDK 5.0 的注解(Annotation) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合...

    SpringBoot使用AOP完成多数据源切换

    import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Aspect @Component @Order(1) public class DataSourceAspect { @Autowired private DynamicDataSource...

    一个使用SSH2简单采购项目

    1. 数据库模型(Entity):定义采购相关的实体类,如采购订单(PurchaseOrder),供应商(Supplier),商品(Product)等。 2. DAO(Data Access Object):处理数据库操作的接口和实现,如PurchaseOrderDAO,使用...

    hibernate3.04中文文档.chm

    符合Java习惯的关系数据库持久化 目录 前言 1. 翻译说明 2. 版权声明 1. 在Tomcat中快速上手 1.1. 开始Hibernate之旅 1.2. 第一个持久化类 1.3. 映射cat 1.4. 与Cat同乐 1.5. 结语 2. Hibernate入门 ...

    Hibernate中文详细学习文档

    符合Java习惯的关系数据库持久化 前言 1. 翻译说明 2. 版权声明 1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. ...

    HibernateAPI中文版.chm

    HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.2 -------------------------------------------------------------------------------- 目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate...

    Spring/泛型Hibernate的实现

    &lt;value&gt;Order.hbm.xml &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect &lt;prop key="hibernate.show_sql"&gt;true &lt;!-- Hibernate Template --&gt; &lt;!-- AOP事务管理 --&gt; ...

    hibernate3.2中文文档(chm格式)

    HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.2 -------------------------------------------------------------------------------- 目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate...

    hibernate 框架详解

    使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合...

    hibernate 体系结构与配置 参考文档(html)

    使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...

    Hibernate3+中文参考文档

    5.4.2. 使用 JDK 5.0 的注解(Annotation) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合...

    Hibernate+中文文档

    共有组合键属性的多对多(Many-to-many with shared composite key attribute) 23.4.4. Content based discrimination 23.4.5. Associations on alternate keys 24. 最佳实践(Best Practices) 表格清单 1. ...

    Hibernate 中文 html 帮助文档

    目录 ... 共有组合键属性的多对多(Many-to-many with shared composite key attribute) 23.4.4. Content based discrimination 23.4.5. Associations on alternate keys 24. 最佳实践(Best Practices)

Global site tag (gtag.js) - Google Analytics