<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 讀取 XML 到 Java 對象的示例 > 原文: [https://howtodoinjava.com/jaxb/read-xml-to-java-object/](https://howtodoinjava.com/jaxb/read-xml-to-java-object/) **讀取 XML 到對象**的 Java 示例。 可以通過某些`.xml`文件或僅通過字符串表示形式提供 XML。 然后,可以在應用中使用填充的 Java 對象進行進一步處理或工作流。 ## 1)JAXB vs SAX vs DOM Java 提供了許多讀取 XML 文件并使用 XML 內容打印,在應用中使用或在 Java 對象中填充數據以進一步在應用生命周期中使用的方法。 用于此目的的三個主要 API 是 XML 的簡單 API([`SAX`](http://www.saxproject.org/)),文檔對象模型([`DOM`](https://www.w3.org/DOM/))和 XML 綁定的 Java 架構([`JAXB`](http://jaxb.java.net/))。 * `SAX`或`DOM`解析器使用 [JAXP](https://jaxp.dev.java.net/) API 來解析 XML 文檔。 兩者都掃描文檔并在邏輯上將其分解為離散的片段(例如,節點,文本和注釋等)。 * `SAX`解析器從文檔的開頭開始,并將文檔的每個片段按找到它的順序傳遞給應用。 內存中沒有保存任何內容,因此無法進行任何內存中的操作。 * `DOM`解析器創建一個對象樹,該樹表示內存中`Document`對象中數據的內容和組織。 然后,應用可以在此處瀏覽樹以訪問所需的數據,并在適當時對其進行操作。 * 當`JAXB`將文檔解組為 Java 內容對象時。 Java 內容對象代表 XML 文檔的內容和組織,可直接用于您的程序。 > 閱讀更多:使用 [SAX 解析器](https://howtodoinjava.com/xml/how-to-parse-an-xml-using-sax-parser-and-defaulthandler/)和 [DOM 解析器](https://howtodoinjava.com/xml/java-xml-dom-parser-example-tutorial/)讀取 XML ## 2)將 XML 字符串轉換為 Java 對象 要讀取 XML,請首先獲取[`JAXBContext`](https://docs.oracle.com/javase/10/docs/api/javax/xml/bind/JAXBContext.html)。 它是 JAXB API 的入口點,并提供了解組,組組和驗證操作的方法。 現在從`JAXBContext`獲取[`Unmarshaller`](https://docs.oracle.com/javase/10/docs/api/javax/xml/bind/Unmarshaller.html)實例。 `unmarshal()`方法從指定的 XML 解組 XML 數據,然后返回結果樹。 ```java String xmlString = "<employee>" + " <department>" + " <id>101</id>" + " <name>IT</name>" + " </department>" + " <firstName>Lokesh</firstName>" + " <id>1</id>" + " <lastName>Gupta</lastName>" + "</employee>"; JAXBContext jaxbContext; try { jaxbContext = JAXBContext.newInstance(Employee.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Employee employee = (Employee) jaxbUnmarshaller.unmarshal(new StringReader(xmlString)); System.out.println(employee); } catch (JAXBException e) { e.printStackTrace(); } Output: Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]] ``` ## 3)將 XML 文件轉換為 Java 對象 從文件讀取 XML 與上面的示例非常相似。 您只需要傳遞`File`對象代替`StringReader`對象。 `File`將參考要在文件系統中讀取的`'.xml'`文件。 ```java File xmlFile = new File("employee.xml"); 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(); } Output: Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]] ``` ```java import java.io.Serializable; 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(); } public Employee(int id, String fName, String lName, Department department) { super(); this.id = id; this.firstName = fName; this.lastName = lName; this.department = department; } //Setters and Getters @Override public String toString() { return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", department="+ department + "]"; } } @XmlRootElement(name = "department") @XmlAccessorType(XmlAccessType.PROPERTY) public class Department implements Serializable { private static final long serialVersionUID = 1L; Integer id; String name; public Department() { super(); } public Department(Integer id, String name) { super(); this.id = id; this.name = name; } //Setters and Getters @Override public String toString() { return "Department [id=" + id + ", name=" + name + "]"; } } ``` 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> ``` 將我的問題放在評論部分。 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看