欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAXB实例入门

程序员文章站 2022-05-01 08:45:52
...
JAXB(Java Architecture for XML Binding),JDK标准规范,Java对象和XML之间的转换,和SAX/DOM不同的是无需关注XML解析细节。
  • Marshalling – 把Java对象转换成XML
  • Unmarshalling – 把XML转换成Java对象
版本:
* JDK1.6开始提供JAXB 2.0(建议使用最新版JDK)
* 低于JDK1.6的版本需要下载jaxb-api.jar和jaxb-impl.jar
* JAXB还有JDK以外的很多第三方实现,比如:EclipseLink MOXy

以下代码都去掉了Bean的Setter/Getter,具体可执行的完整工程代码,可从附件下载。

(1) 基本

Java对象 -> XML
@XmlRootElement
public class Sample {
	private int id;
	private String name;
	private boolean flag;
}

		Sample s = new Sample();
		s.setId(101);
		s.setName("basicObj2XML");
		s.setFlag(false);
		
		StringWriter w = new StringWriter();
		// JAXB是JDK封装好的解析器
		JAXB.marshal(s, new StreamResult(w)); // 输出到字符串
		System.out.println(w.toString());

引用
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sample>
    <flag>false</flag>
    <id>101</id>
    <name>basicObj2XML</name>
</sample>


转换后的XML内容可以直接输出到其他流(比如:控制台、文件等)
		Sample s = new Sample();
		s.setId(102);
		s.setName("basicObj2Output");
		s.setFlag(false);

		JAXB.marshal(s, System.out); // 输出到控制台
		JAXB.marshal(s, new File("d:\\jaxb_sample.xml")); // 输出到文件


XML -> Java对象
		String xml = "<?xml version=\"1.0\"?>" 
					+ "<sample>" 
					+ "  <id>103</id>" 
					+ "  <name>basicXML2Obj</name>" 
					+ "  <flag>false</flag>" 
					+ "</sample>";
		
		StringReader r = new StringReader(xml);
		Sample ss = JAXB.unmarshal(r, Sample.class);
		System.out.println(ss.toString());


(2) 通过JAXBContext创建解析器

创建Marshaller
		JAXBContext jaxbContext = JAXBContext.newInstance(Sample.class);
		Marshaller marshaller = jaxbContext.createMarshaller();
		marshaller.marshal(s, System.out);


创建Unmarshaller
		JAXBContext jaxbContext = JAXBContext.newInstance(Sample.class);
		Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
		StringReader r = new StringReader(xml);
		Sample sample = unmarshaller.unmarshal(new StreamSource(r), Sample.class).getValue();
		System.out.println(sample.toString());


设置Marshaller属性
marshaller.setProperty();
		JAXBContext jaxbContext = JAXBContext.newInstance(Sample.class);
		Marshaller marshaller = jaxbContext.createMarshaller();
		// 设置格式化后输出
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		// 设置字符集
		marshaller.setProperty(Marshaller.JAXB_ENCODING, Charset.defaultCharset().name());
		marshaller.marshal(s, System.out);


(3) 采用EclipseLink MOXy作为JAXB provider
1)下载导入eclipselink.jar
2)在POJO的包下新建jaxb.properties
引用
#Sun JAXB
javax.xml.bind.context.factory=com.sun.xml.internal.bind.v2.ContextFactory
#Eclipse MOXy
#javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

*** SUN的JAXB实现会在xml中添加属性standalone="yes",而EclipseLink MOXy就不会。

(4) 监听器

marshaller.setListener();

Marshaller监听器
public class MarshallListener extends Marshaller.Listener {

	public void beforeMarshal(Object source) {
		System.out.println("BEFORE_MARSHAL fired");
	}

	public void afterMarshal(Object source) {
		System.out.println("AFTER_MARSHAL fired");
	}

}

		JAXBContext jaxbContext = JAXBContext.newInstance(Sample.class);
		Marshaller marshaller = jaxbContext.createMarshaller();
		// 设置监听器(!!!Sun JAXB实现有Bug会被执行两次,而Eclipse MOXy能正确执行一次!!!)
		marshaller.setListener(new MarshallListener());
		marshaller.marshal(s, System.out);


Unmarshaller监听器
public class UnmarshallListener extends Unmarshaller.Listener {

	@Override
	public void beforeUnmarshal(Object target, Object parent) {
		System.out.println("BEFORE_UNMARSHAL fired");
	}

	@Override
	public void afterUnmarshal(Object target, Object parent) {
		System.out.println("AFTER_UNMARSHAL fired");
	}

}

		JAXBContext jaxbContext = JAXBContext.newInstance(Sample.class);
		Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
		// 设置监听器
		unmarshaller.setListener(new UnmarshallListener());
		StringReader r = new StringReader(xml);
		Sample sample = unmarshaller.unmarshal(new StreamSource(r), Sample.class).getValue();
		System.out.println(sample.toString());


(5) 注解

设置内部标签顺序(默认是按标签字母排序)
@XmlType(propOrder={"id", "name", "flag"})
@XmlRootElement
@XmlType(propOrder={"id", "name", "flag"})
public class SampleOrder {
	private int id;
	private String name;
	private boolean flag;
}

引用
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sampleOrder>
    <id>501</id>
    <name>marshallerTagOrder</name>
    <flag>false</flag>
</sampleOrder>


把字段值作为属性(默认都是标签)
@XmlAttribute
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SampleAttribute {
	@XmlAttribute
	private int id;
	private String name;
	private boolean flag;
}

引用
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sampleAttribute id="502">
    <name>SampleAttribute</name>
    <flag>false</flag>
</sampleAttribute>


不输出某个字段值
@XmlTransient
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SampleTransient {
	private int id;
	private String name;
	@XmlTransient
	private boolean flag;
}

引用
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sampleTransient>
    <id>503</id>
    <name>SampleTransient</name>
</sampleTransient>


空值处理

@XmlElement(nillable = true)

		SampleEmpty s = new SampleEmpty();
		s.setId(504);
		s.setName("SampleEmpty");
		s.setFlag(false);
		s.setName1(null);
		s.setName2("");
		s.setName3(null);

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SampleEmpty {
	private int id;
	private String name;
	private boolean flag;
	private String name1;
	private String name2;
	@XmlElement(nillable = true)
	private String name3;
}

引用
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sampleEmpty>
    <id>504</id>
    <name>SampleEmpty</name>
    <flag>false</flag>
    <name2></name2>
    <name3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</sampleEmpty>


变更输出标签属性名称

@XmlRootElement(name="foobar-tag")
@XmlAttribute(name="foobar-id")
@XmlElement(name="foobar-value")


@XmlRootElement(name="foobar-tag")
@XmlAccessorType(XmlAccessType.FIELD)
public class SampleName {
	@XmlAttribute(name="foobar-id")
	private int id;
	@XmlElement(name="foobar-value")
	private String name;
	private boolean flag;
}

引用
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foobar-tag foobar-id="505">
    <foobar-value>SampleName</foobar-value>
    <flag>false</flag>
</foobar-tag>


Bean类型
@XmlRootElement
public class SampleBeanNested {
	private int id;
	private String name;
	private boolean flag;
	private People people;
}

public class People {
	private int id;
	private String name;
}

引用
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sampleBeanNested>
    <flag>false</flag>
    <id>506</id>
    <name>SampleBeanNested</name>
    <people>
        <id>100</id>
        <name>People</name>
    </people>
</sampleBeanNested>


List类型

【字符串】默认

@XmlRootElement
public class SampleStringList1 {
	private int id;
	private String name;
	private boolean flag;
	private List<String> emailAddresses;
}

引用
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sampleStringList1>
    <emailAddresses>a1.rensanning@gmail.com</emailAddresses>
    <emailAddresses>a2.rensanning@gmail.com</emailAddresses>
    <emailAddresses>a3.rensanning@gmail.com</emailAddresses>
    <flag>false</flag>
    <id>507</id>
    <name>SampleStringList1</name>
</sampleStringList1>


@XmlElementWrapper

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SampleStringList2 {
	private int id;
	private String name;
	private boolean flag;
	@XmlElementWrapper(name="email-addresses")
	@XmlElement(name="email-address")
	private List<String> emailAddresses;
}

引用
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sampleStringList2>
    <id>508</id>
    <name>SampleStringList2</name>
    <flag>false</flag>
    <email-addresses>
        <email-address>b1.rensanning@gmail.com</email-address>
        <email-address>b2.rensanning@gmail.com</email-address>
        <email-address>b3.rensanning@gmail.com</email-address>
    </email-addresses>
</sampleStringList2>


@XmlList

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SampleStringList3 {
	private int id;
	private String name;
	private boolean flag;
	@XmlList
	private List<String> emailAddresses;
}

引用
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sampleStringList3>
    <id>509</id>
    <name>SampleStringList3</name>
    <flag>false</flag>
    <emailAddresses>c1.rensanning@gmail.com c2.rensanning@gmail.com c3.rensanning@gmail.com</emailAddresses>
</sampleStringList3>


【Bean】默认

@XmlRootElement
public class SampleBeanList1 {
	private int id;
	private String name;
	private boolean flag;
	private List<People> peoples;
}

引用
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sampleBeanList1>
    <flag>false</flag>
    <id>510</id>
    <name>SampleBeanList1</name>
    <peoples>
        <id>100</id>
        <name>People1</name>
    </peoples>
    <peoples>
        <id>200</id>
        <name>People2</name>
    </peoples>
    <peoples>
        <id>300</id>
        <name>People3</name>
    </peoples>
</sampleBeanList1>


@XmlElementWrapper

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SampleBeanList2 {
	private int id;
	private String name;
	private boolean flag;
	@XmlElementWrapper(name="list")
	@XmlElement(name="people")
	private List<People> peoples;
}

引用
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sampleBeanList2>
    <id>511</id>
    <name>SampleBeanList2</name>
    <flag>false</flag>
    <list>
        <people>
            <id>100</id>
            <name>People1</name>
        </people>
        <people>
            <id>200</id>
            <name>People2</name>
        </people>
        <people>
            <id>300</id>
            <name>People3</name>
        </people>
    </list>
</sampleBeanList2>


@XmlElements

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SampleBeanList3 {
	private int id;
	private String name;
	private boolean flag;
	@XmlElementWrapper(name="list")
	@XmlElements( { 
    	@XmlElement(name = "man", type = Man.class),
		@XmlElement(name = "woman", type = Woman.class) 
	})
	private List<People> peoples;
}

引用
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sampleBeanList3>
    <id>512</id>
    <name>SampleBeanLis3</name>
    <flag>false</flag>
    <list>
        <man>
            <id>100</id>
            <name>Man</name>
            <attr1>Attr1</attr1>
        </man>
        <woman>
            <id>200</id>
            <name>Woman</name>
            <attr2>Attr2</attr2>
        </woman>
    </list>
</sampleBeanList3>


枚举类型

@XmlEnum、@XmlEnumValue

@XmlEnum(Integer.class)
@XmlType
public enum BloodType {
    @XmlEnumValue("1") A,
    @XmlEnumValue("2") B,
    @XmlEnumValue("3") O,
    @XmlEnumValue("4") AB
}

@XmlRootElement
public class SampleEnum {
	private int id;
	private String name;
	private boolean flag;
	private BloodType bloodType;
}

引用
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sampleEnum>
    <bloodType>2</bloodType>
    <flag>false</flag>
    <id>513</id>
    <name>SampleEnum</name>
</sampleEnum>


(6) 适配器

@XmlJavaTypeAdapter

public class DateAdapter extends XmlAdapter<String, Date> {

	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");

	public Date unmarshal(String v) throws Exception {
		return dateFormat.parse(v);
	}

	public String marshal(Date v) throws Exception {
		return dateFormat.format(v);
	}

}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SampleDateAdapter {
	private int id;
	private String name;
	private boolean flag;
	@XmlJavaTypeAdapter(DateAdapter.class)
	private Date date;
}

引用
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sampleDateAdapter>
    <id>601</id>
    <name>SampleDateAdapter</name>
    <flag>false</flag>
    <date>2017/06/09 09:52:26</date>
</sampleDateAdapter>


(7) Schema检验

SampleSchema.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
     
    <xs:element name="SampleSchema">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"  />
                <xs:element name="flag" type="xs:boolean" />
            </xs:sequence>
            <xs:attribute name="id" type="xs:int" />
        </xs:complexType>
    </xs:element> 
    
</xs:schema>


marshaller.setSchema();

@XmlRootElement(name="SampleSchema")
@XmlAccessorType(XmlAccessType.FIELD)
public class SampleSchema {
	@XmlAttribute
	private int id;
	private String name;
	private boolean flag;
}

		SampleSchema s = new SampleSchema();
		s.setId(701);
		s.setName("SampleSchema");
		s.setFlag(false);
		
		ClassLoader classLoader = new Main().getClass().getClassLoader();
        File schemaFile = new File(classLoader.getResource("SampleSchema.xsd").getFile());
		
		SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		Schema schema = sf.newSchema(schemaFile);

		JAXBContext jaxbContext = JAXBContext.newInstance(SampleSchema.class);
		Marshaller marshaller = jaxbContext.createMarshaller();
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		// 设置Schema
		marshaller.setSchema(schema);
		marshaller.marshal(s, System.out);

引用
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SampleSchema id="701">
    <name>SampleSchema</name>
    <flag>false</flag>
</SampleSchema>


validator.validate(source);

		SampleSchema s = new SampleSchema();
		s.setId(702);
		s.setName("marshallerValidator");
		s.setFlag(false);

		ClassLoader classLoader = new Main().getClass().getClassLoader();
        File schemaFile = new File(classLoader.getResource("SampleSchema.xsd").getFile());
        
		SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		Schema schema = sf.newSchema(schemaFile);

		JAXBContext jaxbContext = JAXBContext.newInstance(SampleSchema.class);
		
		// 校验XML
		JAXBSource source = new JAXBSource(jaxbContext, s);
        Validator validator = schema.newValidator();
        validator.setErrorHandler(new CustomValidationErrorHandler());
        validator.validate(source);

		Marshaller marshaller = jaxbContext.createMarshaller();
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		marshaller.marshal(s, System.out);


(8) 捕获事件

marshaller.setEventHandler();

public class CustomEventHandler implements ValidationEventHandler {

	@Override
	public boolean handleEvent(ValidationEvent event) {
		System.out.println(event.getSeverity());

		if (event.getSeverity() == ValidationEvent.ERROR
				|| event.getSeverity() == ValidationEvent.FATAL_ERROR) {
			ValidationEventLocator locator = event.getLocator();
			System.out.println("Line:Col[" + locator.getLineNumber() + ":" + locator.getColumnNumber() + "]");
			throw new RuntimeException(event.getMessage(), event.getLinkedException());
		}
		return true;
	}

}

		JAXBContext jaxbContext = JAXBContext.newInstance(Sample.class);
		Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
		// 设置事件处理器
		unmarshaller.setEventHandler(new CustomEventHandler());
		StringReader r = new StringReader(xml);
		Sample sample = unmarshaller.unmarshal(new StreamSource(r), Sample.class).getValue();
		System.out.println(sample.toString());