简体   繁体   中英

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. Right now i just get empty root element tag. Is its possible to marshal xml using JAXB and achieve something similar for testing reasons? I am a newbie in java and jaxb, any help is appreciated.

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:

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?

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; using some sort of XML editor, you define how the XML should look like. When you're happy with that visualization, write the JAXB code that would generate that particular type of XML; 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. While doable, I would call this madness.

For completeness, you could also use XSOM (no need for JAXB) to do the same.

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? I might have a different suggestion, based on your clarifications...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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