[英]Two different schemas and JAXB Marshaller
Lets suppose that we have XML consnensual with Schema and Java class with some common fields: 让我们假设我们对XML和Schema以及Java类有一些共同的看法:
<objectFromSchema1>
<element1/>
<commonElement1/>
<commonElement2/>
<element2/>
</objectFromSchema1>
public class X {
private String element1;
private String commonElement1;
private String commonElement2;
private String element2;
}
Is a nice way to unmarschall such kind of XML to Java object ? 将此类XML解组到Java对象的一种好方法吗? It means: convert all consensual fields and set null on rest.
这意味着:转换所有共识字段,并在休息时将其设置为null。
The answer is "yes". 答案是“是”。 This is the way JAXB works.
这就是JAXB的工作方式。 Take a look on basic JAXB tutorial, eg https://jaxb.java.net/tutorial/
看一下基本的JAXB教程,例如https://jaxb.java.net/tutorial/
http://docs.oracle.com/javase/tutorial/jaxb/intro/ http://docs.oracle.com/javase/tutorial/jaxb/intro/
http://www.vogella.com/tutorials/JAXB/article.html http://www.vogella.com/tutorials/JAXB/article.html
"YES" “是”
If you have an xsd you also could generate automatically these classes by <artifactId>maven-jaxb2-plugin</artifactId>
maven plugin. 如果您有xsd,还可以通过
<artifactId>maven-jaxb2-plugin</artifactId>
maven插件自动生成这些类。
An example of your Class 您班上的一个例子
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "objectFromSchema1", propOrder = {
})
@XmlRootElement(name = "objectFromSchema1")
public class ObjectFromSchema1
implements Serializable
{
private final static long serialVersionUID = 12343L;
protected String element1;
protected String element2;
protected String commonElement1;
protected String commonElement2;
public String getElement1() {
return element1;
}
public void setElement1(String element1) {
this.element1 = element1;
}
public String getElement2() {
return element2;
}
public void setElement2(String element2) {
this.element2 = element2;
}
public String getCommonElement1() {
return commonElement1;
}
public void setCommonElement1(String commonElement1) {
this.commonElement1 = commonElement1;
}
public String getCommonElement2() {
return commonElement2;
}
public void setCommonElement2(String commonElement2) {
this.commonElement2 = commonElement2;
}
}
Main method to use it 主要使用方法
public static void main(String[] args) throws JAXBException {
final JAXBContext context = JAXBContext.newInstance(ObjectFromSchema1.class);
final Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
final ObjectFromSchema1 objectFromSchema1 = new ObjectFromSchema1();
objectFromSchema1.setCommonElement1("commonElement1");
objectFromSchema1.setCommonElement2("commonElement2");
objectFromSchema1.setElement1("element1");
objectFromSchema1.setElement2("element2");
m.marshal(objectFromSchema1, System.out);
}
output 产量
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<objectFromSchema1>
<element1>element1</element1>
<element2>element2</element2>
<commonElement1>commonElement1</commonElement1>
<commonElement2>commonElement2</commonElement2>
</objectFromSchema1>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.