摘要: JAXB 作为JDK的一部分,能便捷地将Java对象与XML进行相互转换,本教程从实际案例出发来讲解JAXB 2 的那些事儿。完整版目录
Marshaller数据源
JAXB 支持将数据编组marshal
到不同数据源,为此,Marshaller 提供了8个重载的方法:
public void marshal( Object jaxbElement, javax.xml.transform.Result result );
public void marshal( Object jaxbElement, java.io.OutputStream os );
public void marshal( Object jaxbElement, File output );
public void marshal( Object jaxbElement, java.io.Writer writer );
public void marshal( Object jaxbElement, org.xml.sax.ContentHandler handler );
public void marshal( Object jaxbElement, org.w3c.dom.Node node );
public void marshal( Object jaxbElement, javax.xml.stream.XMLStreamWriter writer );
public void marshal( Object jaxbElement, javax.xml.stream.XMLEventWriter writer );
接下来的小节中将逐一演示在它们在项目中的使用场景。
在开始之前,先熟悉一下之前的初始化代码,用来创建相关实例。
private static JAXBContext context;
private static One one;
@BeforeClass
public static void init() throws JAXBException {
// JAXBContext 是线程安全的
context = JAXBContext.newInstance(One.class);
// 初始化全局的 Java bean
one = new One();
one.setName("Test one");
}
还需要注意的是,演示代码处于如下环境:
- Win 7
- Java 8
OutputStream
在之前的演示中,为了能直观显示结果,我将结果直接输出到控制台。System.out
是一种OutputStream
。
public void test1() throws JAXBException {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(one, System.out);// 直接输出到控制台
}
File
这里我将结果输出到文件中,"D://test1.xml"
不必手动创建,如果文件存在,将覆盖其中的内容。最后几行代码是用来测试数据是否成功输出到文件,与真正的编码没有必然联系。
public void test2() throws JAXBException, SAXException {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
File file = new File("D://test1.xml");// 将结果输出到文件
marshaller.marshal(one, file);// 将自动创建test1.xml文件,并输出内容
// 测试File中数据
try (FileInputStream stream = new FileInputStream(new File("D://test1.xml"));){
System.out.println(stream.read());// 测试File中数据的字节数
} catch (Exception e) {
// TODO: handle exception
}
}
也可以使用下面的方式,在项目的根目录下创建文件。刷新项目后,能在根目录下发现文件file.xml
。
public void test2_2() throws Exception {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
FileOutputStream os = new FileOutputStream("file.xml");// 将结果输出到OutputStream
marshaller.marshal(one, os);// 这是使用到 FileOutputStream
}
Writer
演示 Writer
使用到了 StringWriter
, 将结果传递到 Writer
以后,就可以接下来的操作。
public void test3() throws JAXBException {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter writer = new StringWriter();
marshaller.marshal(one, writer);// 将结果赋值到Writer
System.out.println(writer.toString());// 测试Writer中数据
}
Result
这里使用到了 JAXBResult
,在使用 Result
的时候,一定要注意手动添加 start/endDocument
。
public void test4() throws JAXBException, SAXException {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
JAXBResult result = new JAXBResult(context);
// 需要手动添加 start/endDocument
result.getHandler().startDocument();
marshaller.marshal(one, result);// 将结果赋值到 Result
result.getHandler().endDocument();
System.out.println(result.getResult());// 测试result中数据
}
ContentHandler
下面代码演示了 ContentHandler
的使用场景。
public void test5() throws JAXBException, SAXException, TransformerException {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
ContentHandler handler = new XMLFilterImpl();
handler.startDocument();
marshaller.marshal(one, handler);
handler.endDocument();
}
Node
下面代码演示了 Node
的使用场景,Node
使用了 Document
。最后的代码演示了 Transformer
的使用场景。
public void test7() throws Exception {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//创建 Document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();
marshaller.marshal(one, document);
// 创建 TransformerFactory
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(System.out); //测试输出
t.transform(source, result);
}
XMLStreamWriter
XMLStreamWriter
使用的场景不是太多,下面代码演示了该情形。注释的代码和marshal
的结果相同。
@Test
@Ignore
public void test7() throws JAXBException, XMLStreamException {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
XMLOutputFactory factory = XMLOutputFactory.newInstance();
StringWriter sw = new StringWriter();
XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
marshaller.marshal(one, writer);
/* writer.writeStartDocument("UTF-8", "1.0");
writer.writeStartElement("one");
writer.writeStartElement("name");
writer.writeCharacters("Test one");
writer.writeEndElement();
writer.writeEndElement();
writer.flush();
sw.flush();*/
System.out.println(sw.toString()); //测试输出
}
XMLEventWriter
XMLEventWriter
使用的场景不是太多,下面代码演示了该情形。
@Test
public void test8() throws JAXBException, XMLStreamException {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
XMLOutputFactory factory = XMLOutputFactory.newInstance();
StringWriter sw = new StringWriter();
XMLEventWriter writer = factory.createXMLEventWriter(sw);
marshaller.marshal(one, writer);
System.out.println(sw.toString());//测试输出
}
下节预览
这一节基于各个输出场景对marshal
做出了比较全面的展示。下一节将着重介绍各种不同的数据结构,输出方式主要是OutputStream
中的 System.out
。