<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # JAXB `Marshaller`(編組器)示例 > 原文: [https://howtodoinjava.com/jaxb/marshaller-example/](https://howtodoinjava.com/jaxb/marshaller-example/) JAXB [`Marshaller`](https://docs.oracle.com/javase/10/docs/api/javax/xml/bind/Marshaller.html)接口負責管理將 Java 內容樹即 **Java 對象轉換為 XML** 數據的過程。 可以將 XML 編組到各種輸出目標。 ## 1\. JAXB 編組對象到 XML #### 1.1 創建編組器 通常,要創建編組器,可以重用此代碼。 ```java JAXBContext jaxbContext = JAXBContext.newInstance( Employee.class ); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); Employee employeeObj = new Employee(1, "Lokesh", "Gupta", new Department(101, "IT")); //Overloaded methods to marshal to different outputs jaxbMarshaller.marshal(employeeObj); ``` #### 1.2 編組 XML 文件 ```java OutputStream os = new FileOutputStream( "employee.xml" ); jaxbMarshaller.marshal( employeeObj, os ); ``` #### 1.3 編組至 SAX `ContentHandler` 假設`MyContentHandler`是 [`org.xml.sax.ContentHandler`](https://docs.oracle.com/javase/7/docs/api/org/xml/sax/ContentHandler.html) 的實例。 ```java jaxbMarshaller.marshal( employeeObj, new MyContentHandler() ); ``` #### 1.4 編組 DOM 文件 ```java DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); jaxbMarshaller.marshal( employeeObj, doc ); ``` #### 1.5 編組并打印到控制臺 ```java m.marshal( employeeObj, new PrintWriter( System.out ) ); ``` ## 2\. JAXB `Marshaller`屬性 ```java jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); //or jaxbMarshaller.setProperty("jaxb.formatted.output", Boolean.TRUE); ``` 所有 JAXB 供應器都必須支持以下屬性集。 一些供應器可能支持其他屬性。 * **`jaxb.encoding`** - 編組 XML 數據時使用的輸出編碼。 如果未指定此屬性,則默認情況下`Marshaller`將使用“`UTF-8`”。 * **`jaxb.formatted.output`** - 值可以是`true`或`false`。 `Marshaller`是否將使用換行符和縮進來格式化所得的 XML 數據。 默認值為`false`。 * **`jaxb.schemaLocation`** – 允許客戶端應用在生成的 XML 數據中指定`xsi:schemaLocation`屬性。 * **`jaxb.noNamespaceSchemaLocation`** – 它允許客戶端應用在生成的 XML 數據中指定`xsi:noNamespaceSchemaLocation`屬性。 * **`jaxb.fragment`** – 它確定`Marshaller`是否將生成文檔級事件。 值可以是`true`或`false`。 ## 3.`Marshaller`回調方法 您可以通過 JAXB 注解類中的**自定義編組操作**,例如 `Employee.java`。 您需要定義兩個方法,這些方法將在編組程序處理該類之前和之后進行監聽。 在這些方法中,您可以執行諸如設置額外字段之類的操作。 ```java package com.howtodoinjava.demo.model; import java.io.Serializable; import javax.xml.bind.Marshaller; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "employee") @XmlAccessorType(XmlAccessType.PROPERTY) public class Employee implements Serializable { private static final long serialVersionUID = 1L; private Integer id; private String firstName; private String lastName; private Department department; public Employee() { super(); } //Setters and Getters @Override public String toString() { return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", department=" + department + "]"; } // Invoked by Marshaller after it has created an instance of this object. boolean beforeMarshal(Marshaller marshaller) { System.out.println("Before Marshaller Callback"); return true; } // Invoked by Marshaller after it has marshalled all properties of this object. void afterMarshal(Marshaller marshaller) { System.out.println("After Marshaller Callback"); } } ``` ## 4\. JAXB 編組示例 **將 Java 對象編組為 XML 字符串**的示例。 ```java package com.howtodoinjava.demo; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import com.howtodoinjava.demo.model.Department; import com.howtodoinjava.demo.model.Employee; public class JaxbExample { public static void main(String[] args) { Employee employee = new Employee(1, "Lokesh", "Gupta", new Department(101, "IT")); jaxbObjectToXML(employee); } private static void jaxbObjectToXML(Employee employee) { try { JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // To format XML //Print XML String to Console jaxbMarshaller.marshal(employee, System.out); } catch (JAXBException e) { e.printStackTrace(); } } } ``` 程序輸出。 ```java Before Marshaller Callback After Marshaller Callback <?xml version="1.0" encoding="UTF-8"?> <employee> <department> <id>101</id> <name>IT</name> </department> <firstName>Lokesh</firstName> <id>1</id> <lastName>Gupta</lastName> </employee> ``` 將我的問題放在評論部分。 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看