JAXB序列化和反序列化XML源码(可直接使用)

用法参考

 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();
    }
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java中的XML序列化是将Java对象转换为XML格式的过程。XML序列化可以用于数据持久化、数据传输等场景。Java提供了多种方式来实现XML序列化,包括JAXB、XStream、Dom4j等。 JAXB是Java Architecture for XML Binding的缩写,是Java SE 6及以上版本中自带的一种XML数据绑定技术。JAXB通过注解或XML配置文件来描述Java类与XML之间的映射关系,从而实现Java对象到XML序列化反序列化。以下是一个使用JAXB进行XML序列化的示例代码: ```java // 定义一个Java类 @XmlRootElement public class Person { private String name; private int age; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } // 序列化Java对象到XML public static void serializeToXml(Person person, String xmlFilePath) throws JAXBException { JAXBContext context = JAXBContext.newInstance(Person.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(person, new File(xmlFilePath)); } // 反序列化XML到Java对象 public static Person deserializeFromXml(String xmlFilePath) throws JAXBException { JAXBContext context = JAXBContext.newInstance(Person.class); Unmarshaller unmarshaller = context.createUnmarshaller(); return (Person) unmarshaller.unmarshal(new File(xmlFilePath)); } ``` 使用JAXB进行XML序列化需要注意以下几点: 1. Java类需要使用@XmlRootElement注解标注为根元素。 2. Java类的属性需要使用@XmlAttribute或@XmlElement注解标注为XML属性或元素。 3. 序列化时需要创建JAXBContext和Marshaller,反序列化时需要创建JAXBContext和Unmarshaller。 4. 序列化时可以设置Marshaller的属性,如是否格式化输出。 除了JAXB外,XStream和Dom4j也是常用的XML序列化工具。XStream是一款简单易用的XML序列化框架,通过注解或代码配置来完成Java类与XML之间的映射关系。Dom4j是一款基于Java的XML解析器和生成器,可以快速方便地操作XML文档。无论使用哪种工具,XML序列化都是Java开发中常用的技术之一。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值