您知道xml编辑器如何让您能够从xsd方案创建示例xml,使用随机内容填充所有元素和属性。现在我只是得到空的根元素标签。是否有可能使用JAXB编组xml并且为了测试的原因实现类似的东西? 我是java和jaxb的新手,任何帮助表示赞赏。JAXB - 生成样本xml?
编辑。 规范根元素类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"document",
"taskList",
"addDocuments",
"expansion",
"acknowledgement"
})
@XmlRootElement(name = "Header")
public class Header {
@XmlElement(name = "Document")
protected DocumentType document;
@XmlElement(name = "TaskList")
protected TaskListType taskList;
@XmlElement(name = "AddDocuments")
protected AddDocumentsType addDocuments;
@XmlElement(name = "Expansion")
protected ExpansionType expansion;
@XmlElement(name = "Acknowledgement")
protected AcknowledgementType acknowledgement;
@XmlAttribute(name = "time", required = true)
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar time;
@XmlAttribute(name = "msg_type", required = true)
protected short msgType;
@XmlAttribute(name = "msg_id", required = true)
protected String msgId;
@XmlAttribute(name = "msg_acknow")
protected Short msgAcknow;
@XmlAttribute(name = "from_org_id", required = true)
protected String fromOrgId;
@XmlAttribute(name = "from_organization", required = true)
protected String fromOrganization;
@XmlAttribute(name = "from_department")
protected String fromDepartment;
@XmlAttribute(name = "from_sys_id", required = true)
protected String fromSysId;
@XmlAttribute(name = "from_system", required = true)
protected String fromSystem;
@XmlAttribute(name = "from_system_details")
protected String fromSystemDetails;
@XmlAttribute(name = "to_org_id")
protected String toOrgId;
@XmlAttribute(name = "to_organization", required = true)
protected String toOrganization;
@XmlAttribute(name = "to_department")
protected String toDepartment;
@XmlAttribute(name = "to_sys_id")
protected String toSysId;
@XmlAttribute(name = "to_system")
protected String toSystem;
@XmlAttribute(name = "to_system_details")
protected String toSystemDetails;
// getters n setters are omitted
}
创建XML:
ObjectFactory objectFactory = new ObjectFactory();
Header header = objectFactory.createHeader();
JAXBContext jaxbContext = JAXBContext.newInstance(Header.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(header, file);
我得到什么:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Header msg_type="0" />
哪来的一切?我能否收到类似完整xml的东西,而无需手动创建所有元素和属性以及设置值?
它对我来说不是100%清楚你想达到什么目的。你是否希望你的JAXB注释类被渲染为带有测试数据的XML? –
现在我只是得到空的根元素标记,我想用一些测试数据填充所有嵌套的元素和属性。 – bunnyjesse112
使用schemagen从JAXB带注释的bean生成XSD,并要求XML编辑器从此XSD生成示例XML。 –