JAXB - 生成示例xml?

时间:2012-01-19 12:31:30

标签: java xml testing xsd jaxb

您知道xml编辑器如何使您能够从xsd方案创建示例xml,并使用随机内容填充所有元素和属性。现在我只是得到空的根元素标签。是否有可能使用JAXB编组xml并为测试原因实现类似的功能?  我是java和jaxb的新手,感谢任何帮助。

修改 根元素类的代码:

        @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的内容吗?

1 个答案:

答案 0 :(得分:1)

可以做到,但请放心,没有简单的方法可以做到。在这些不那么简单的情况下,最不具挑战性的是你想出一套布局,你可以硬编码你的代码以匹配布局,随机生成数据。这意味着您定义了XML的“类”;使用某种XML编辑器,您可以定义XML的外观。当您对该可视化感到满意时,请编写将生成该特定类型的XML的JAXB代码;使用随机生成的数据或任何其他适合您需求的方式。

“通用”方式可能依赖于良好的JAXB知识和反射API。虽然可行,但我会称之为疯狂。

为了完整性,您还可以使用XSOM(不需要JAXB)来执行相同的操作。

这并不是说我会鼓励你进行上述任何一项,除非你有足够的时间和精力来备用...你是否有可能分享XSD,或者至少你的工具为什么在生成示例XML时,似乎不仅仅是你的根本?根据你的澄清,我可能有不同的建议......