使用JAXBContext 设置xml节点属性
作者:Dawn_Bells 时间:2021-09-21 00:05:44
JAXBContext 设置xml节点属性
在使用JAXBContext将javaBean转化为xml时
会出现这样的需求:
<xml version="2.0">
....
</xml>
那么xml节点里的属性值version需要怎么设置
使用@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文件大致为:
<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 为数组元素或集合元素定义一个父节点。
如,类中有一元素为List items,若不加此注解,该元素将被映射为
<items>...</items>
<items>...</items>
这种形式,此注解可将这个元素进行包装,如:
@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>
来源:https://blog.csdn.net/Dawn_Bells/article/details/81908275
标签:JAXBContext,xml,节点,属性
0
投稿
猜你喜欢
深入学习java位运算的基础知识
2021-06-01 07:10:40
Unity 数据存储和读取的方法汇总
2021-12-12 11:39:47
C#实现获取不同对象中名称相同属性的方法
2021-11-04 19:26:32
简单谈谈java中匿名内部类构造函数
2021-09-08 18:54:10
C#设置Word文档背景的三种方法(纯色/渐变/图片背景)
2022-07-08 01:33:09
java实现简单的图书管理系统
2022-11-25 03:30:11
SpringBoot动态修改yml配置文件的方法详解
2023-06-20 16:14:34
如何让Spring Rest 接口中路径参数可选
2022-02-17 19:29:14
Android App中进行语言的切换
2022-07-07 05:15:47
Jetpack navigation组件超详细讲解
2021-07-17 08:49:58
eclipse+maven+spring mvc项目基本搭建过程
2022-12-18 03:50:52
Java 实战项目之毕业设计管理系统的实现流程
2021-12-26 00:10:10
IDEA教程创建SpringBoot前后端分离项目示例图解
2022-06-30 02:41:10
Android实现加载广告图片和倒计时的开屏布局
2023-04-29 13:54:45
Android自定义相机Camera实现手动对焦的方法示例
2022-08-23 14:45:11
必须要学会的JMM与volatile
2021-07-30 14:07:53
在service层注入mapper时报空指针的解决
2021-09-07 03:39:21
JAVA实现心跳检测(长连接)
2022-12-16 04:26:30
c#模拟银行atm机示例分享
2023-04-01 10:59:45
java实现简单猜拳小游戏
2021-09-01 07:49:46