<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 – 編組和解組對象列表或集合 > 原文: [https://howtodoinjava.com/jaxb/jaxb-exmaple-marshalling-and-unmarshalling-list-or-set-of-objects/](https://howtodoinjava.com/jaxb/jaxb-exmaple-marshalling-and-unmarshalling-list-or-set-of-objects/) 我們知道 [JAXB(用于 XML 綁定的 Java 架構)](https://en.wikipedia.org/wiki/Java_Architecture_for_XML_Binding "jaxb")允許 Java 開發人員將 Java 類映射到 XML 表示形式。 JAXB 提供了兩個主要功能:將 Java 對象編組為 XML 的能力和相反的功能,即將 XML 解組為 Java 對象的能力。 JAXB 主要用于實現 Web 服務或任何其他此類客戶端接口的應用,在該應用中,數據需要以 XML 格式(而不是 HTML 格式)傳輸,而對于可視客戶端(如 Web 瀏覽器)來說,HTML 格式是默認格式。 一個很好的例子是 facebook API。 Facebook 已通過 [RESTful Web 服務](//howtodoinjava.com/restful-web-service/ "resteasy tutorials")的形式通過一些開放的端點公開了其服務,您在其中單擊 URL 并發布了一些參數,而 API 以 xml 格式返回數據。 現在由您決定如何使用這些數據。 在本文中,我以**編組和解組**對象的集合為例。 Java 中的這些[集合](//howtodoinjava.com/category/java/collections/ "Java Collections")可以是:`List`和`Set`實現,例如 `ArrayList`或`HashSet`。 ## 1)JAXB Maven 依賴關系 要運行 JAXB 示例,我們需要添加運行時 [maven](//howtodoinjava.com/maven/ "maven tutorials") 依賴項,如下所示: ```java <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.2.8-b01</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.2-promoted-b65</version> </dependency> ``` ## 2)模型類 我創建了一個模型類“`Employee`”,它具有一些公開字段。 我想構建可以解析一組`employees`的代碼。 請注意,JAXB 在最重要的類上需要 [`@XmlRootElement`](https://docs.oracle.com/javaee/5/api/javax/xml/bind/annotation/XmlRootElement.html "XmlRootElement") 注解,我們將對其進行編組或解組。 `ArrayList`類是集合框架的一部分,它沒有任何 JAXB 注解。 因此,我們需要另外一個類別“`Employees`”,該類別將代表一組雇員。 現在,在該類中,我們可以添加任何我們喜歡的注解。 ```java @XmlRootElement(name = "employee") @XmlAccessorType (XmlAccessType.FIELD) public class Employee { private Integer id; private String firstName; private String lastName; private double income; //Getters and Setters } ``` ```java import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "employees") @XmlAccessorType (XmlAccessType.FIELD) public class Employees { @XmlElement(name = "employee") private List<Employee> employees = null; public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } } ``` ## 3)將列表編組為 XML 的示例 編組是“將 Java 對象轉換為 xml 表示形式”。 在下面的示例代碼中,我首先在控制臺中寫入`employees`列表,然后在文件中寫入。 ```java //Initialize the employees list static Employees employees = new Employees(); static { employees.setEmployees(new ArrayList<Employee>()); //Create two employees Employee emp1 = new Employee(); emp1.setId(1); emp1.setFirstName("Lokesh"); emp1.setLastName("Gupta"); emp1.setIncome(100.0); Employee emp2 = new Employee(); emp2.setId(2); emp2.setFirstName("John"); emp2.setLastName("Mclane"); emp2.setIncome(200.0); //Add the employees in list employees.getEmployees().add(emp1); employees.getEmployees().add(emp2); } private static void marshalingExample() throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(Employees.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //Marshal the employees list in console jaxbMarshaller.marshal(employees, System.out); //Marshal the employees list in file jaxbMarshaller.marshal(employees, new File("c:/temp/employees.xml")); } ``` 上面代碼的輸出是: ![JAXB marshalling example output](https://img.kancloud.cn/a6/37/a637c256bb3282769ffef7ed4997454a_575x232.png) JAXB 編組示例輸出 ## 4)解組 XML 到列表的示例 解組是**將 xml 轉換回 Java 對象**的過程。 讓我們看一下`Employees`類的示例。 ```java private static void unMarshalingExample() throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(Employees.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); //We had written this file in marshalling example Employees emps = (Employees) jaxbUnmarshaller.unmarshal( new File("c:/temp/employees.xml") ); for(Employee emp : emps.getEmployees()) { System.out.println(emp.getId()); System.out.println(emp.getFirstName()); } } Output: 1 Lokesh 2 John ``` ## 5\. 源代碼下載 要下載以上示例的源代碼,請點擊以下鏈接。 [下載源碼](https://docs.google.com/file/d/0B7yo2HclmjI4Q0xnUmRNWV9WMjg/edit?usp=sharing "example source code jaxb arraylist") 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看