简体   繁体   English

JAXB-生成示例XML?

[英]JAXB - Generating sample xml?

You know how xml editors give you ability to create sample xml from xsd scheme, filling all elements and attributes with random stuff. 您知道xml编辑器如何使您能够从xsd方案创建示例xml,并用随机填充内容填充所有元素和属性。 Right now i just get empty root element tag. 现在我只得到空的根元素标签。 Is its possible to marshal xml using JAXB and achieve something similar for testing reasons? 是否可以使用JAXB封送xml并出于测试原因而实现类似目的? I am a newbie in java and jaxb, any help is appreciated. 我是Java和jaxb的新手,不胜感激。

EDIT. 编辑。 Code for root element class: 根元素类的代码:

        @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

    }

creating xml: 创建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);  

what i get: 我得到什么:

  <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
  <Header msg_type="0" />  

where's everything else? 还有什么呢 Can i receive something resembling full xml without creating all elements and attributes and setting values manually? 我可以收到类似于完整xml的内容,而无需手动创建所有元素和属性以及设置值吗?

It can be done, but rest assured that there is no simple way to do it. 可以做到,但请放心,没有简单的方法可以做到。 Among these not so simple, the least challenging would be for you to come up with a set of layouts for which you hard wire your code to match the layout, with data being randomly generated. 在这些并非那么简单的方法中,最小的挑战就是为您提供一组布局,您可以为这些布局硬编码代码以匹配该布局,并随机生成数据。 This means you define a "class" of XMLs; 这意味着您定义了XML的“类”。 using some sort of XML editor, you define how the XML should look like. 使用某种XML编辑器,您可以定义XML的外观。 When you're happy with that visualization, write the JAXB code that would generate that particular type of XML; 当您对可视化感到满意时,编写将生成该特定类型的XML的JAXB代码; use randomly generated data or any other way that would fit for your needs. 使用随机生成的数据或其他适合您需求的方式。

A "generic" way could be to rely on good JAXB knowledge and reflection API. 一种“通用”方式可能是依赖于良好的JAXB知识和反射API。 While doable, I would call this madness. 虽然可行,但我称其为疯狂。

For completeness, you could also use XSOM (no need for JAXB) to do the same. 为了完整起见,您还可以使用XSOM(无需JAXB)执行相同操作。

This is not to say that I would encourage you in any of the above, unless you have plenty of time and effort to spare... Is it possible for you to share the XSD, or at least the reason why your tool seems to not go more than your root in generating sample XML? 这并不是说我会鼓励您采取上述任何一种措施,除非您有足够的时间和精力来备用...是否有可能共享XSD,或者至少是您的工具似乎不共享的原因在生成示例XML方面比您的根源还要多? I might have a different suggestion, based on your clarifications... 根据您的澄清,我可能有不同的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM