2012-01-19 66 views
2

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

编辑。 规范根元素类:

 @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

它对我来说不是100%清楚你想达到什么目的。你是否希望你的JAXB注释类被渲染为带有测试数据的XML? –

+0

现在我只是得到空的根元素标记,我想用一些测试数据填充所有嵌套的元素和属性。 – bunnyjesse112

+2

使用schemagen从JAXB带注释的bean生成XSD,并要求XML编辑器从此XSD生成示例XML。 –

回答

1

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

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

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

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

+0

对不起,我没有正确解释自己,请看编辑的问题 – bunnyjesse112