您知道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。 –