【问题标题】:JAXB - Generating sample xml?JAXB - 生成示例 xml?
【发布时间】:2012-01-19 12:31:30
【问题描述】:

您知道 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 的内容吗?

【问题讨论】:

  • 我不是 100% 清楚你想要达到什么目的。是否希望将 JAXB 注释类与测试数据一起呈现为 XML?
  • 现在我只是得到空的根元素标签,我想用一些测试数据填充所有嵌套元素和属性。
  • 使用 schemagen 从您的 JAXB 注释 bean 生成 XSD,并要求您的 XML 编辑器从该 XSD 生成示例 XML。
  • 但是我有一个 xsd,我用 xjc 从中生成了类。有没有办法在 java 代码中使用 jaxb 来做到这一点?
  • 不幸的是,在你的情况下,没有办法“神奇地”得到你想要的。您将不得不编写代码....

标签: java xml testing xsd jaxb


【解决方案1】:

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

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

为了完整起见,您也可以使用 XSOM(不需要 JAXB)来做同样的事情。

这并不是说我会在上述任何方面鼓励您,除非您有足够的时间和精力来腾出...您是否可以共享 XSD,或者至少是您的工具的原因在生成示例 XML 方面似乎没有超过您的根?根据您的说明,我可能会有不同的建议...

【讨论】:

  • 对不起,我没有正确解释自己,请查看已编辑的问题
猜你喜欢
  • 2021-08-23
  • 1970-01-01
  • 2018-06-24
  • 2011-06-27
  • 1970-01-01
  • 1970-01-01
  • 2014-07-07
  • 1970-01-01
  • 2011-01-10
相关资源
最近更新 更多