jaxb实现java对象与xml之间转换_Jaxb实现Java对象与XML之间的转换

[JAXB(Java Architecture for XML Binding ) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方

Jaxb核心代码:

package ie.swing.xml.parse;

import ie.swing.xml.model.Components;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.OutputStream;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.JAXBElement;

import javax.xml.bind.JAXBException;

import javax.xml.bind.Marshaller;

import javax.xml.bind.Unmarshaller;

/**

* 组件配置文件解析

*

* @author lile

*

*/

public class ComponentAnalysis {

/**

根据schema文件生成Java类的命令:

xjc.bat -classpath . ie/swing/config/component-1.0.xsd

*/

/**

* XML to Java

* @param packageName 包名, 如 ie.swing.xml.model

* @param xmlPath 要解析的xml文件路径 如 "src/ie/swing/config/component.xml"

* @return rootObject 根对象

* @throws JAXBException

* @throws FileNotFoundException

*/

public T xml2Java(String packageName, String xmlPath) throws JAXBException, FileNotFoundException {

JAXBContext context = JAXBContext.newInstance(packageName);

Unmarshaller unmarshaller = context.createUnmarshaller();

File file = new File(xmlPath);

@SuppressWarnings("unchecked")

JAXBElement rootElement = (JAXBElement) unmarshaller.unmarshal(file);

T rootObject = rootElement.getValue();

return rootObject;

}

/**

* Java to XML

* @param rootObject 根对象, Xml 根标签对应的对象

* @param out 输出流,该方法将xml数据写入该输出流

* @throws JAXBException

*/

public void java2XML(T rootObject, OutputStream out) throws JAXBException {

JAXBContext context = JAXBContext.newInstance(rootObject.getClass());

Marshaller marshaller = context.createMarshaller();

marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8"); // 编码格式

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // 是否格式化生成的xml串

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false); // 是否省略xml头信息

marshaller.marshal(rootObject, out);

}

/**

* 测试运行

* @param args

* @throws FileNotFoundException

* @throws JAXBException

*/

public static void main(String[] args) throws FileNotFoundException, JAXBException {

ComponentAnalysis ca = new ComponentAnalysis();

//将Xml解析成Java对象

Components cps = ca.xml2Java("ie.swing.xml.model", "src/ie/swing/config/component.xml");

//将解析出的Java对象cps解析回Xml并输出到System.out

ca.java2XML(cps, System.out);

}

}

示例xsd schema文件 component-1.0.xsd:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

targetNamespace="http://www.ie168.org/component-1.0"

xmlns:tns="http://www.ie168.org/component-1.0"

elementFormDefault="qualified">

[利用Marshaller和unMarshaller可在java的object对象和xml之间实现转换首先创建一个简单的Boy对象[java] view plaincopyprint@XmlRootElement(name=Root)  @XmlAccessorTy

示例xml文件: component.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:ie="http://www.ie168.org/component-1.0"

xsi:schemaLocation="http://www.ie168.org/component-1.0 component-1.0.xsd">

运行之前先用JDK自带的命令:

xjc.bat -classpath . 包名/component-1.0.xsd

根据schema文件生成对应的Java类[使用jaxb xml与对象javabean之间转换,对象生成soap报文xml,soap报文xml转换为对象]

你可能感兴趣的:(jaxb实现java对象与xml之间转换_Jaxb实现Java对象与XML之间的转换)