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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 沒有`@XmlRootElement`的 JAXB 編組 – 缺少`@XmlRootElement`錯誤 > 原文: [https://howtodoinjava.com/jaxb/marshal-without-xmlrootelement/](https://howtodoinjava.com/jaxb/marshal-without-xmlrootelement/) 很多時候,我們將需要**編組 Java 對象,而不使用 [JAXB 注解](https://howtodoinjava.com/jaxb/jaxb-annotations/)**,例如[`@XmlRootElement`](https://howtodoinjava.com/jaxb/xmlrootelement-annotation/),并且我們不允許在源代碼中進行任何更改。 當我們使用遺留代碼或某些我們沒有源代碼的客戶端 jar 時,可能會發生這種情況。 可能還有許多其他情況,但是想法是*我們無法使用 JAXB 注解*修改模型類。 這可能是**將 Java 對象轉換為 xml 而沒有 jaxb** 的示例。 ## 1.不帶`@XmlRootElement`的編組問題 在這種情況下,如果我們嘗試將[將 Java 對象直接編組為 XML](https://howtodoinjava.com/jaxb/write-object-to-xml/),則將得到類似的錯誤。 ```java javax.xml.bind.MarshalException - with linked exception: [com.sun.istack.internal.SAXException2: unable to marshal type "com.howtodoinjava.demo.model.Employee" as an element because it is missing an @XmlRootElement annotation] at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:311) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:236) at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:95) at com.howtodoinjava.demo.JaxbExample.jaxbObjectToXML(JaxbExample.java:45) at com.howtodoinjava.demo.JaxbExample.main(JaxbExample.java:17) Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "com.howtodoinjava.demo.model.Employee" as an element because it is missing an @XmlRootElement annotation at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:234) at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:323) at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:479) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308) ... 4 more ``` 其中`Employee.java`類如下。 它沒有任何`@XmlRootElement`這樣的 JAXB 注解。 ```java package com.howtodoinjava.demo.model; import java.io.Serializable; 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; } //Getters and Setters @Override public String toString() { return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", department=" + department + "]"; } } ``` ## 2.不帶`@XmlRootElement`注解的編組的解決方案 缺少`@XmlRootElement`注解時,JAXB 無法為`Employee`對象構建[`JAXBElement`](https://docs.oracle.com/javaee/7/api/javax/xml/bind/JAXBElement.html)實例。 這就是我們必須幫助 JAXB 手動構建它的地方。 #### 2.1 語法 ```java /** * @param name Java binding of xml element tag name * @param declaredType Java binding of xml element declaration's type * @param value Java instance representing xml element's value */ JAXBElement<T> elem = new JAXBElement(QName name, Class<T> declaredType, T value ); ``` 例如: ```java JAXBElement<Employee> jaxbElement = new JAXBElement<Employee>( new QName("", "employee"), Employee.class, employeeObj ); ``` #### 2.2 不帶`@XmlRootElement`的 JAXB 編組示例 現在,讓我們看看該編組代碼的工作原理。 ```java package com.howtodoinjava.demo; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.namespace.QName; 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 employeeObj) { try { JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); // To format XML jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); //If we DO NOT have JAXB annotated class JAXBElement<Employee> jaxbElement = new JAXBElement<Employee>( new QName("", "employee"), Employee.class, employeeObj); jaxbMarshaller.marshal(jaxbElement, System.out); //If we have JAXB annotated class //jaxbMarshaller.marshal(employeeObj, System.out); } 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> ``` 請向我提供關于沒有`@XmlRootElement`的 *JAXB 編組*示例的問題。 學習愉快! 參考: [`XmlRootElement` Java 文檔](https://docs.oracle.com/javase/7/docs/api/javax/xml/bind/annotation/XmlRootElement.html)
                  <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>

                              哎呀哎呀视频在线观看