如何用jaxb xsd 生成java类

如何用jaxb xsd 生成java类,第1张

#0. 准备蚂扰要转换的xml文件,在Project视界中,右击这个xml文件,在d出的菜单上选择“Generate XSD schema from XML File...”, 按罩物搭默物拿认设置生成xsd文件。

将xsd 文件移至#1配置段的configuration/sources/source指定的路径下.

xsd就是xml schema,可以利用以下方法生成xml:

Schema文件定义如下:<o:p></o:p>

<!----><o:p></o:p>

<xs:schema xmlns="http://mydomain.com/myApp"xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://mydomain.com/myApp" elementFormDefault="qualified"attributeFormDefault="unqualified"><o:p></o:p>

<xs:element name="e1"><o:p></o:p>

<xs:complexType><o:p></o:p>

<xs:sequence><o:p></o:p>

<xs:element name="e11" type="xs:int"/前雹><o:p></o:p>

<xs:element name="e12"><o:p></o:p>

<xs:complexType><o:p></o:p>

<xs:attribute name="e121" type="xs:string" use="required"/><o:p></o:p>

<xs:attribute name="e122" type="xs:long"/><o:p></o:p>

xs:complexType><o:p></o:p>

xs:element><o:p></o:p>

xs:sequence><o:p>团御</o:p>

xs:complexType><o:p></o:p>

xs:element><o:p></o:p>

xs:schema><o:p></o:p>

<o:p></o:p>

解析代码如下:

import org.apache.xmlbeans.*

import com.mydomain.myApp.E1Document

public class OutputXml {

/**

* 其中e11=11 e121=”e121”

* @param args

*/

public static void main(String args[]) {

E1Document e1Document

E1Document.E1 E1Element

E1Document.E1.E12 E1e2Element

XmlOptions xmlOptions

e1Document = E1Document.Factory.newInstance()

E1Element = e1Document.addNewE1()

E1e2Element=E1Element.addNewE12()

E1Element.setE11(11)

E1e2Element.setE121("e121")

/**

* 如果需要设置setE122的值塌悔岩请打开,由于xsd文件定义的元素不是必须的.

*/

//E1e2Element.setE122()

xmlOptions = new XmlOptions()

xmlOptions.setSavePrettyPrint()

xmlOptions.setSavePrettyPrintIndent(4)

String xmlStr = e1Document.xmlText(xmlOptions)

System.out.println("XML Instance Document is : \n" + xmlStr )

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12146654.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存