app开发者平台在数字化时代的重要性与发展趋势解析
1416
2022-12-20
使用JAXBContext 设置xml节点属性
JAXBContext 设置xml节点属性
在使用JAXBContext将javaBean转化为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文件大致为:
JAXBContext解析XML集合对象
@XmlElementWrapper 为数组元素或集合元素定义一个父节点。
如,类中有一元素为List items,若不加此注解,该元素将被映射为
这种形式,此注解可将这个元素进行包装,如:
@XmlElementWrapper(name="items")
@XmlElement(name="item")
public List items;
将会生成这样的XML样式:
Demo如下:
实体类一(定义一个被包含的子项Item):
package org.ywzn.po;
import javax.xml.bind.annotation.XmlAttribute;
public class Item {
private String infoType;
private String nodeId;
private Stringhttp:// 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
@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
return items;
}
@XmlElementWrapper(name="Items")
@XmlElement(name="Item")
public void setItems(List
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) {
// // version='1.0'> // BusinessId='B730EB39-CEFF-4a38-B633-D8936EB8AEF7' SessionId='936' // /> // Infotype='LogUser'> // >D41D8CD98F00B204E9800998ECF8427E //
// version='1.0'> // BusinessId='B730EB39-CEFF-4a38-B633-D8936EB8AEF7' SessionId='936' // /> // Infotype='LogUser'> // >D41D8CD98F00B204E9800998ECF8427E //
// BusinessId='B730EB39-CEFF-4a38-B633-D8936EB8AEF7' SessionId='936'
// /> // Infotype='LogUser'> // >D41D8CD98F00B204E9800998ECF8427E
// Infotype='LogUser'> // >D41D8CD98F00B204E9800998ECF8427E
// >D41D8CD98F00B204E9800998ECF8427E
//
// TODO Auto-generated method stub
Item item = new Item("LOGIN","789", "xxx","xxx");
List
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();
}
}
}
输出:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~