<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 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]] ``` 將我的問題放在評論部分。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看