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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # JAXB `Unmarshaller`(解組器)示例 > 原文: [https://howtodoinjava.com/jaxb/jaxb-unmarshaller-example/](https://howtodoinjava.com/jaxb/jaxb-unmarshaller-example/) JAXB [`Unmarshaller`](https://docs.oracle.com/javase/10/docs/api/javax/xml/bind/Unmarshaller.html)接口負責管理將 XML 數據反序列化為 Java 對象的過程。 可以將對象解組到各種輸入源。 ## 1.如何將 XML 解組到對象 #### 1.1 創建解組器 通常,要創建`Unmarshaller`,可以重用此代碼。 ```java JAXBContext jaxbContext = JAXBContext.newInstance( Employee.class ); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); //Overloaded methods to unmarshal from different xml sources Employee employee = (Employee) jaxbUnmarshaller.unmarshal( xmlSource ); ``` #### 1.2 從`InputStream`解組 ```java InputStream inStream = new FileInputStream( "employee.xml" ); Employee employee = (Employee) jaxbUnmarshaller.unmarshal( inStream ); ``` #### 1.3 從 URL 解組 ```java URL url = new URL( "http://localhost:8080/employee.xml" ); Employee employee = (Employee) jaxbUnmarshaller.unmarshal( url ); ``` #### 1.4 解組字符串內容 ```java String xmlString = "..."; Employee employee = (Employee) jaxbUnmarshaller.unmarshal(new StringReader(xmlString)); ``` #### 1.5 從`org.w3c.dom.Node`解組 ```java DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(new File( "employee.xml")); //Node Employee employee = (Employee) jaxbUnmarshaller.unmarshal( document ); ``` ## 2\. JAXB 解組器屬性 當前,`Unmarshaller`上的所有 JAXB 供應器都不需要**所有屬性**。 但是,某些供應器可能支持它們自己的一組供應器特定屬性。 ## 3.解組事件回調 您可以通過在 [JAXB 注解的](https://howtodoinjava.com/jaxb/jaxb-annotations/)類中添加這些回調方法**來自定義解組操作**,例如`Employee.java`。 您需要定義兩個方法,這些方法將在`Unmarshaller`處理該類之前和之后監聽。 * `void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) {}` * `void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {}` ```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 + "]"; } // It is called immediately after the object is created and before the unmarshalling begins. // The callback provides an opportunity to initialize JavaBean properties prior to unmarshalling. void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) { System.out.println("Before Unmarshaller Callback"); } // It is called after all the properties are unmarshalled for this object, // but before this object is set to the parent object. void afterUnmarshal(Unmarshaller unmarshaller, Object parent) { System.out.println("After Unmarshaller Callback"); } } ``` ## 4\. JAXB 解組器示例 **將 XML 文件解組到 Java 對象**的示例。 ```java package com.howtodoinjava.demo; import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import com.howtodoinjava.demo.model.Employee; public class JaxbExample { public static void main(String[] args) { String fileName = "employee.xml"; 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 Before Unmarshaller Callback After Unmarshaller Callback Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]] ``` 其中`employee.xml`文件是: ```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> ``` 向我提出您的有關使用 JAXB 注解在 Java 中解組的問題。 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看