简体   繁体   English

根据现有的XSD模式将对象序列化为XML

[英]Serialize object to XML based on existing XSD schema

I have to create an XML document that would be based on an certain XML Schema Document. 我必须创建一个基于某个XML Schema Document的XML文档。 Since my data is a DataSet , I need to find the best way to start off. 由于我的数据是DataSet ,因此我需要找到最佳的开始方法。

I have couple of different ideas how to start: 我有几个不同的想法如何开始:

  • manually create nodes, elements, attributes that would match XSD 手动创建与XSD匹配的节点,元素,属性
  • transform DataSet into a class that would match the schema document and serialize it DataSet转换为与架构文档匹配并进行序列化的类
  • something else? 还有什么吗

Is this a right way to get a XML output from DataSet to match XSD schema? 这是从DataSet获取XML输出以匹配XSD模式的正确方法吗?

May be you should give XMLBeans a try... It's a diverse framework for playing around with compiled XSD schemas. 也许您应该尝试一下XMLBeans。这是一个用于处理已编译XSD架构的多样化框架。 Compiled in this context means, you create JAVA classes from your XSD-files. 在这种情况下编译意味着您可以从XSD文件创建JAVA类。

Compilation example (as can be seen here ) scomp -out purchaseorder.jar purchaseorder.xsd 汇编例子(如可以看到这里scomp -out purchaseorder.jar purchaseorder.xsd

With this jar in your classpath you could create new a priori valid instances of your schema with something like: 在您的类路径中使用此jar时,您可以使用以下内容创建新的模式的先验有效实例:

public PurchaseOrderDocument createPO() {
    PurchaseOrderDocument newPODoc = PurchaseOrderDocument.Factory.newInstance();
    PurchaseOrder newPO = newPODoc.addNewPurchaseOrder();
    Customer newCustomer = newPO.addNewCustomer();
    newCustomer.setName("Doris Kravitz");
    newCustomer.setAddress("Bellflower, CA");
    return newPODoc;
}

You can find the whole example at: XMLBeans Tutorial under the heading "Creating New XML Instances from Schema". 您可以在以下位置找到整个示例: XMLBeans教程 ,标题为“从模式创建新的XML实例”。

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

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