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/
分享到:
相关推荐
InOrder inOrder = inOrder(userRepository); inOrder.verify(userRepository).save(user); inOrder.verify(userRepository).update(user); ``` #### 对Mock对象方法的调用次数、顺序和超时进行验证 除了基本的...
HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.2 -------------------------------------------------------------------------------- 目录 前言 1. 翻译说明 2. 版权声明 1. 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. ...
InOrder inOrder = inOrder(list); list.add("one"); list.add("two"); inOrder.verify(list).add("one"); inOrder.verify(list).add("two"); } ``` #### Mock对象的重置 有时可能需要重置mock对象的状态,...
│ Dao.java │ GeneratorDemo.java │ hibernate.cfg.xml │ HibernateDaoImpl.java │ HibernateSessionFactory.java │ HibernateUtil.java │ JsonUtil.java │ list.txt │ log4j.properties │ ...
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. 集合...
import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Aspect @Component @Order(1) public class DataSourceAspect { @Autowired private DynamicDataSource...
1. 数据库模型(Entity):定义采购相关的实体类,如采购订单(PurchaseOrder),供应商(Supplier),商品(Product)等。 2. DAO(Data Access Object):处理数据库操作的接口和实现,如PurchaseOrderDAO,使用...
符合Java习惯的关系数据库持久化 目录 前言 1. 翻译说明 2. 版权声明 1. 在Tomcat中快速上手 1.1. 开始Hibernate之旅 1.2. 第一个持久化类 1.3. 映射cat 1.4. 与Cat同乐 1.5. 结语 2. 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. ...
HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.2 -------------------------------------------------------------------------------- 目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate...
<value>Order.hbm.xml <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect <prop key="hibernate.show_sql">true <!-- Hibernate Template --> <!-- AOP事务管理 --> ...
HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.2 -------------------------------------------------------------------------------- 目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate...
使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合...
使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...
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. 集合...
共有组合键属性的多对多(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. ...
目录 ... 共有组合键属性的多对多(Many-to-many with shared composite key attribute) 23.4.4. Content based discrimination 23.4.5. Associations on alternate keys 24. 最佳实践(Best Practices)