簡體   English   中英

JAXB-生成示例XML?

[英]JAXB - Generating sample xml?

您知道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的內容,而無需手動創建所有元素和屬性以及設置值嗎?

可以做到,但請放心,沒有簡單的方法可以做到。 在這些並非那么簡單的方法中,最小的挑戰就是為您提供一組布局,您可以為這些布局硬編碼代碼以匹配該布局,並隨機生成數據。 這意味着您定義了XML的“類”。 使用某種XML編輯器,您可以定義XML的外觀。 當您對可視化感到滿意時,編寫將生成該特定類型的XML的JAXB代碼; 使用隨機生成的數據或其他適合您需求的方式。

一種“通用”方式可能是依賴於良好的JAXB知識和反射API。 雖然可行,但我稱其為瘋狂。

為了完整起見,您還可以使用XSOM(無需JAXB)執行相同操作。

這並不是說我會鼓勵您采取上述任何一種措施,除非您有足夠的時間和精力來備用...是否有可能共享XSD,或者至少是您的工具似乎不共享的原因在生成示例XML方面比您的根源還要多? 根據您的澄清,我可能有不同的建議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM