JAXB序列化和反序列化XML源码(可直接使用)
2013-01-20 18:47
1026 查看
用法参考:
Document document = (Document) JAXBTool.unmarshalXml("D:\\temp\\LightRequirmentDocument1.3.xml", Document.class);
JAXBTool.marshalToXml(Document.class, document, "D:\\temp\\test.xml");
源码:
package owngeftable.Tools;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class JAXBTool
{
public static Object unmarshalXml(String path, Class className)
{
Object result = null;
try
{
File file = new File(path);
JAXBContext jaxbContext = JAXBContext.newInstance(className);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
result = jaxbUnmarshaller.unmarshal(file);
}
catch (JAXBException e)
{
e.printStackTrace();
}
return result;
}
public static void marshalToXml(Class className, Object root,String filePath)
{
try
{
File file = new File(filePath);
JAXBContext jaxbContext = JAXBContext.newInstance(className);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(root, file);
}
catch (JAXBException e)
{
e.printStackTrace();
}
}
}
Document document = (Document) JAXBTool.unmarshalXml("D:\\temp\\LightRequirmentDocument1.3.xml", Document.class);
JAXBTool.marshalToXml(Document.class, document, "D:\\temp\\test.xml");
源码:
package owngeftable.Tools;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class JAXBTool
{
public static Object unmarshalXml(String path, Class className)
{
Object result = null;
try
{
File file = new File(path);
JAXBContext jaxbContext = JAXBContext.newInstance(className);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
result = jaxbUnmarshaller.unmarshal(file);
}
catch (JAXBException e)
{
e.printStackTrace();
}
return result;
}
public static void marshalToXml(Class className, Object root,String filePath)
{
try
{
File file = new File(filePath);
JAXBContext jaxbContext = JAXBContext.newInstance(className);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(root, file);
}
catch (JAXBException e)
{
e.printStackTrace();
}
}
}
相关文章推荐
- 使用Newtonsoft.Json序列化和反序列化对象(源码)
- java 使用xom对象数据序列化为xml、反序列化、Preferences相关操作小案例
- JAXB主要用来实现对象和XML之间的序列化和反序列化
- 使用工具类,轻松实现XML序列化、反序列化
- 使用JAXB 将java继承的类序列化xml
- 在web环境中使用JAXB技术对XML文件进行序列化和反序列化时如何避免乱码
- 使用XML 序列化和反序列化类对象的方法
- java序列化/反序列化之xml、protobuf、protostuff 的比较与使用例子
- java 序列化反序列化xml【使用XStream类库】
- Xml序列化和反序列化对象-使用MemoryStream-实践
- xml和对象直接的序列化和反序列化
- 使用XStream序列化、反序列化XML数据时遇到的各种问题
- java序列化/反序列化之xml、protobuf、protostuff 的比较与使用例子
- 使用JAXB时,xml与java对象互转以及初始情况下直接由模板xml生成javabean
- 使用XStream序列化、反序列化XML数…
- 使用JAXB类库实现XML与JAVA对象的互转
- 使用层序遍历序列化和反序列化二叉树
- 直接复制与使用序列化克隆时间比较
- c# XML和实体类之间相互转换(序列化和反序列化)
- XML序列化与反序列化 整理文档