欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用JAXBContext 設(shè)置xml節(jié)點屬性

 更新時間:2021年08月10日 10:35:32   作者:Dawn_Bells  
這篇文章主要介紹了使用JAXBContext 設(shè)置xml節(jié)點屬性的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

JAXBContext 設(shè)置xml節(jié)點屬性

在使用JAXBContext將javaBean轉(zhuǎn)化為xml時

會出現(xiàn)這樣的需求:

<xml version="2.0">
    ....
</xml>

那么xml節(jié)點里的屬性值version需要怎么設(shè)置

使用@XmlAttribute標(biāo)簽即可,如下代碼。

@XmlRootElement(name = "Xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class RequestBean{
 
    @XmlAttribute(name = "version") //設(shè)置節(jié)點屬性
    private String version; 
    private Body body;         
    @XmlElement(name = "sign")  //設(shè)置子節(jié)點
    private String sign; 
    //省略封裝
}
 
@XmlRootElement(name = "Body")
@XmlAccessorType(XmlAccessType.FIELD)
public class Body{
    ...
}

最終得到的xml文件大致為:

<Xml version="2.0">
    <sign>111111</sign>
    <Body>
 <Amount>111</Amount>
 <Fee>fee</Fee>
 <PayerName>payname</PayerName>
 <AccountType>accountType</AccountType>
    </Body>
</Xml>

JAXBContext解析XML集合對象

@XmlElementWrapper 為數(shù)組元素或集合元素定義一個父節(jié)點。

如,類中有一元素為List items,若不加此注解,該元素將被映射為

    <items>...</items>
    <items>...</items>

這種形式,此注解可將這個元素進(jìn)行包裝,如:

    @XmlElementWrapper(name="items")
    @XmlElement(name="item")
    public List items;

將會生成這樣的XML樣式:

    <items>
        <item>...</item>
        <item>...</item>
    </items>

Demo如下:

實體類一(定義一個被包含的子項Item):

package org.ywzn.po; 
import javax.xml.bind.annotation.XmlAttribute; 
public class Item { 
 private String infoType; 
 private String nodeId; 
 private String resultCode; 
 private String resultString; 
 public Item() {
  super();
 }
 
 public Item(String infoType, String nodeId, String resultCode,
   String resultString) {
  super();
  this.infoType = infoType;
  this.nodeId = nodeId;
  this.resultCode = resultCode;
  this.resultString = resultString;
 }
 
 @XmlAttribute(name = "InfoType")
 public String getInfoType() {
  return infoType;
 }
 
 public void setInfoType(String infoType) {
  this.infoType = infoType;
 }
 
 public String getNodeId() {
  return nodeId;
 }
 
 public void setNodeId(String nodeId) {
  this.nodeId = nodeId;
 }
 
 public String getResultCode() {
  return resultCode;
 }
 
 public void setResultCode(String resultCode) {
  this.resultCode = resultCode;
 }
 
 public String getResultString() {
  return resultString;
 }
 
 public void setResultString(String resultString) {
  this.resultString = resultString;
 } 
}

實體類二(定義一個有List集合的實體類):

package org.ywzn.po; 
import java.util.List; 
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement
public class Message { 
 private String version; 
 private Head head; 
 private List<Item> items; 
 @XmlAttribute(name="version")
 public String getVersion() {
  return version;
 }
 
 public void setVersion(String version) {
  this.version = version;
 }
 
 @XmlElement(name="Head")
 public Head getHead() {
  return head;
 }
 
 public void setHead(Head head) {
  this.head = head;
 }
 
 public List<Item> getItems() {
  return items;
 }
 
 @XmlElementWrapper(name="Items")
 @XmlElement(name="Item")
 public void setItems(List<Item> items) {
  this.items = items;
 }  
}

測試方法:

package org.ywzn.main; 
import java.util.ArrayList;
import java.util.List; 
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller; 
import org.ywzn.po.Head;
import org.ywzn.po.Item;
import org.ywzn.po.Message;
import org.ywzn.po.Parameters;
import org.ywzn.po.Room;
 
public class Java2XMLMany { 
 public static void main(String[] args) {
  // <?xml version='1.0' encoding='UTF-8' standalone='no' ?> <Message
  // version='1.0'><Header Time='2015-05-22 10:34:27' MessageType='LOGIN'
  // BusinessId='B730EB39-CEFF-4a38-B633-D8936EB8AEF7' SessionId='936'
  // /><Parameters> <items Sum='1' Offset='1' Cur='1'> <item
  // Infotype='LogUser'> <PassWord
  // >D41D8CD98F00B204E9800998ECF8427E</PassWord></item> </items>
  // </Parameters> </Message>
 
  // TODO Auto-generated method stub
  Item item = new Item("LOGIN","789", "xxx","xxx");
  List<Item> list = new ArrayList<Item>();
  list.add(item);
  Head head = new Head("2015-05-21 16:46:14", "LOGIN", "8D24CE2B-5");
  Parameters parameters = new Parameters(list);
  // Message message = new Message(head,"1.0",parameters);
  Message message = new Message();
  message.setVersion("1.0");
  message.setHead(head);
  message.setItems(list);
  try {
   JAXBContext context = JAXBContext.newInstance(Message.class);
   Marshaller createMarshaller = context.createMarshaller();
   createMarshaller
     .setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化生成的xml串
 
   createMarshaller.marshal(message, System.out);
  } catch (JAXBException e) {
   e.printStackTrace();
  }
 }
 
}

輸出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<message version="1.0">
    <Head businessId="8D24CE2B-5" MessageType="LOGIN" Time="2015-05-21 16:46:14"/>
    <Items>
        <Item InfoType="LOGIN">
            <nodeId>789</nodeId>
            <resultCode>xxx</resultCode>
            <resultString>xxx</resultString>
        </Item>
    </Items>
</message>

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • spring啟動錯誤Singleton bean creation not allowed while the singletons of this factory are indestruction

    spring啟動錯誤Singleton bean creation not al

    本文主要介紹了spring啟動錯誤Singleton bean creation not allowed while the singletons of this factory are indestruction,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • java springmvc亂碼解決歸納整理詳解

    java springmvc亂碼解決歸納整理詳解

    本篇文章介紹了java 中spring mvc 解決亂碼的問題方法實例,需要的朋友可以參考下
    2017-04-04
  • SpringMVC Validator驗證示例

    SpringMVC Validator驗證示例

    SpringMVC服務(wù)器驗證一種是有兩種方式,一種是基于Validator接口,一種是使用Annotaion JSR-303標(biāo)準(zhǔn)的驗證,本篇文章主要介紹,有興趣的可以了解一下。
    2017-01-01
  • IntelliJ?IDEA?2020.2?全家桶及以下版本激活工具大全【喜訊】

    IntelliJ?IDEA?2020.2?全家桶及以下版本激活工具大全【喜訊】

    這篇文章主要介紹了IntelliJ?IDEA?2020.2?全家桶及以下版本激活工具大全【喜訊】,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • JavaWeb入門:HttpResponse和HttpRequest詳解

    JavaWeb入門:HttpResponse和HttpRequest詳解

    這篇文章主要介紹了Django的HttpRequest和HttpResponse對象,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2021-07-07
  • Java?System#exit無法退出程序的問題及解決

    Java?System#exit無法退出程序的問題及解決

    這篇文章主要介紹了Java?System#exit無法退出程序的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • java異常與錯誤處理基本知識

    java異常與錯誤處理基本知識

    本文內(nèi)容是java的異常與錯誤處理基本知識
    2013-11-11
  • Mac OS上安裝Tomcat服務(wù)器的簡單步驟

    Mac OS上安裝Tomcat服務(wù)器的簡單步驟

    這篇文章主要介紹了Mac OS上安裝Tomcat服務(wù)器的簡單步驟,包括簡單的啟動命令和查看Tomcat信息的方法,需要的朋友可以參考下
    2015-11-11
  • Spring?Boot?整合?Reactor實例詳解

    Spring?Boot?整合?Reactor實例詳解

    這篇文章主要為大家介紹了Spring?Boot?整合?Reactor實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • 解決springboot application.properties server.port配置問題

    解決springboot application.properties server.port配置問題

    這篇文章主要介紹了解決springboot application.properties server.port配置問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08

最新評論