一、注解的使用:
@XmlAccessorType
@XmlAccessorType(XmlAccessType.FIELD)经常这样用
@XmlType
@XmlRootElement
属性为name,namespace
@XmlSeeAlso
指示JAXB在绑定这个类时也绑定其他类。
@XmlSchema(package-info.java)
@javax.xml.bind.annotation.XmlSchema(namespace = "http://docs.oasis-open.org/tosca/ns/2011/12", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) package org.eclipse.winery.model.tosca;
@XmlSchemaType
是为了在局部覆盖@XmlSchema注解
@XmlElement
属性为name,namespace
@XmlElements(对应xs:choice)
@XmlAnyElement(对应xs:any)
@XmlAttribute
@XmlAnyAttribute(对应xs:anyAttribute)
@XmlValue
@XmlID
XmlID用于指定一个类中的某个属性为其唯一标识,对应的属性类型必须是String,且在一个类中只能有一个属性使用@XmlID注解;
@XmlIDRef
@XmlJavaTypeAdapter
XmlJavaTypeAdapter可以用于在进行Java对象和XML相互转换时做一些适配工作,比如需要把java.util.Date转换为XML的字符串形式的yyyy-MM-dd格式。使用XmlJavaTypeAdapter时需要通过value属性指定一个XmlAdapter类,表示对应的适配器类。XmlAdapter是一个抽象类。其中定义了两个方法,marshal和unmarshal,marshal方法用于适配从Java到XML,unmarshal方法用于适配从XML到Java。
@XmlMixed
@XmlEnum
@XmlEnumValue
@XmlRegistry
@XmlElementDecl
二、利用插件把xsd转换成javaBean的class文件
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>1.5</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>xjc</goal> </goals> <configuration> <schemaDirectory>${project.basedir}/src/main/resources</schemaDirectory> <!--<schemaFiles>TOSCA-v1.0-aai.xsd</schemaFiles>--> <!--httpproxy>my.com.cn:80</httpproxy--> <packageName>org.my.test</packageName> </configuration> </execution> </executions> </plugin>
三、jaxb的常用情况
@XmlElement与List<Object> 适用于名称确定并且名称相同的多个Element
@XmlElements与List<Object> 适用于名称确定但名称不相同的多个Element
@XmlAnyElement与List<Object> 适用于不确定名称的多个Element
@XmlAnyElement与Object 适用于不确定名称的一个Element
@XmlAnyAttribute与Map<QName,String> 适用于多余的不确定的属性
其它:参考文献
https://www.iteye.com/blogs/subjects/jaxb
jaxb注解:https://www.cnblogs.com/writeLessDoMore/p/6927666.html