您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關如何使用JAXBContext設置xml節點屬性的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
會出現這樣的需求:
<xml version="2.0"> .... </xml>
使用@XmlAttribute標簽即可,如下代碼。
@XmlRootElement(name = "Xml") @XmlAccessorType(XmlAccessType.FIELD) public class RequestBean{ @XmlAttribute(name = "version") //設置節點屬性 private String version; private Body body; @XmlElement(name = "sign") //設置子節點 private String sign; //省略封裝 } @XmlRootElement(name = "Body") @XmlAccessorType(XmlAccessType.FIELD) public class Body{ ... }
<Xml version="2.0"> <sign>111111</sign> <Body> <Amount>111</Amount> <Fee>fee</Fee> <PayerName>payname</PayerName> <AccountType>accountType</AccountType> </Body> </Xml>
@XmlElementWrapper 為數組元素或集合元素定義一個父節點。
如,類中有一元素為List items,若不加此注解,該元素將被映射為
<items>...</items> <items>...</items>
這種形式,此注解可將這個元素進行包裝,如:
@XmlElementWrapper(name="items") @XmlElement(name="item") public List items;
將會生成這樣的XML樣式:
<items> <item>...</item> <item>...</item> </items>
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; } }
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>
感謝各位的閱讀!關于“如何使用JAXBContext設置xml節點屬性”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。