多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# JAXB 将 Java 对象写入 XML 的示例 > 原文: [https://howtodoinjava.com/jaxb/write-object-to-xml/](https://howtodoinjava.com/jaxb/write-object-to-xml/) **将 Java 对象写入 XML** 的 Java 示例。 Java 对象字段中存储的信息可以写入 **XML 文件**,也可以写入 **XML 字符串**。 ## 1)将 Java 对象转换为 XML 字符串 要将 Java 对象写入 XML `String`,请首先获取`JAXBContext`。 它是 JAXB API 的入口点,并提供了解组,组组和验证操作的方法。 现在从`JAXBContext`获取`Marshaller`实例。 `marshal()`方法将 Java 对象编组为 XML。 现在可以将此 XML 写入不同的输出,例如,字符串,文件或流。 ```java package com.howtodoinjava.demo; import java.io.File; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class JaxbExample { public static void main(String[] args) { //Java object. We will convert it to XML. Employee employee = new Employee(1, "Lokesh", "Gupta", new Department(101, "IT")); //Method which uses JAXB to convert object to XML jaxbObjectToXML(employee); } private static void jaxbObjectToXML(Employee employee) { try { //Create JAXB Context JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class); //Create Marshaller Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); //Required formatting?? jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); //Print XML String to Console StringWriter sw = new StringWriter(); //Write XML to StringWriter jaxbMarshaller.marshal(employee, sw); //Verify XML Content String xmlContent = sw.toString(); System.out.println( xmlContent ); } catch (JAXBException e) { e.printStackTrace(); } } } ``` 程序输出。 ```java <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <employee> <department> <id>101</id> <name>IT</name> </department> <firstName>Lokesh</firstName> <id>1</id> <lastName>Gupta</lastName> </employee> ``` ## 2)将 Java 对象转换为 XML 文件 将 XML 写入文件与上面的示例非常相似。 您只需要提供要在其中写入文件的 XML 文件位置即可。 ```java package com.howtodoinjava.demo; import java.io.File; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class JaxbExample { public static void main(String[] args) { //Java object. We will convert it to XML. Employee employee = new Employee(1, "Lokesh", "Gupta", new Department(101, "IT")); //Method which uses JAXB to convert object to XML jaxbObjectToXML(employee); } private static void jaxbObjectToXML(Employee employee) { try { //Create JAXB Context JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class); //Create Marshaller Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); //Required formatting?? jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); //Store XML to File File file = new File("employee.xml"); //Writes XML file to file-system jaxbMarshaller.marshal(employee, file); } catch (JAXBException e) { e.printStackTrace(); } } } ``` 程序输出: ```java <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <employee> <department> <id>101</id> <name>IT</name> </department> <firstName>Lokesh</firstName> <id>1</id> <lastName>Gupta</lastName> </employee> ``` ## 3)读取 XML 的示例 如果要再次从文件读取 XML 到 Java 对象,请使用此方法。 ```java String fileName = "employee.xml"; //Call method which read the XML file above jaxbXmlFileToObject(fileName); private static void jaxbXmlFileToObject(String fileName) { File xmlFile = new File(fileName); JAXBContext jaxbContext; try { jaxbContext = JAXBContext.newInstance(Employee.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Employee employee = (Employee) jaxbUnmarshaller.unmarshal(xmlFile); System.out.println(employee); } catch (JAXBException e) { e.printStackTrace(); } } ``` 程序输出: ```java Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]] ``` 将我的问题放在评论部分。 学习愉快!