实体类:
package AjaxTest;
import javax.xml.bind.annotation.XmlElement;
public class JAXBEntity {
private String headId;
private String headName;
private String headPwd;
//在XML中的节点名称
@XmlElement(name = "JAXB_headId")
public String getHeadId() {
return headId;
}
public void setHeadId(String headId) {
this.headId = headId;
}
//在XML中的节点名称
@XmlElement(name = "JAXB_headName")
public String getHeadName() {
return headName;
}
public void setHeadName(String headName) {
this.headName = headName;
}
//在XML中的节点名称
@XmlElement(name = "JAXB_headPwd")
public String getHeadPwd() {
return headPwd;
}
public void setHeadPwd(String headPwd) {
this.headPwd = headPwd;
}
}
实体类1:
package AjaxTest;
import javax.xml.bind.annotation.XmlElement;
public class JAXBEntity1 {
private String listId;
private String listName;
private String listAddress;
//在XML中的节点名称
@XmlElement(name="body_Id")
public String getListId() {
return listId;
}
public void setListId(String listId) {
this.listId = listId;
}
//在XML中的节点名称
@XmlElement(name="body_Name")
public String getListName() {
return listName;
}
public void setListName(String listName) {
this.listName = listName;
}
//在XML中的节点名称
@XmlElement(name="body_Address")
public String getListAddress() {
return listAddress;
}
public void setListAddress(String listAddress) {
this.listAddress = listAddress;
}
}
进行对象存放类:
JAXBEntity节点位置 namespace:XML目标名称空间 defaultValue:此元素的默认值;
nillable:是否可以为空,默认false;
required=true :将 Javabean 属性映射到一个 minOccurs=”1” 的 XML 模式元素声明
package AjaxTest;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
//根节点位置 @XmlRootElement:将类或枚举类型映射到XML元素
@XmlRootElement(name = "JAXBMax")
public class JAXBEntity2 {
private JAXBEntity entity; //entity对象
private List<JAXBEntity1> listEnityt; //List<Enityt1> 存放集合对象
@XmlElement(name = "JAXBEntity",namespace="www.baidu.com",nillable=false,required=false)
public JAXBEntity getEntity() {
return entity;
}
public void setEntity(JAXBEntity reqHeader) {
this.entity = reqHeader;
}
//在XML中集合的父节点
@XmlElementWrapper(name = "JAXBList_Entity1")
@XmlElement(name = "Entity1")//在XML中集合的单个对象节点名
public List<JAXBEntity1> getListEnityt() {
return listEnityt;
}
public void setListEnityt(List<JAXBEntity1> listEnityt) {
this.listEnityt = listEnityt;
}
}
Xml和String字符的交互类:
package AjaxTest;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class JAXBXmlorString {
/**
* @param <T>
* @param xml
* @param t
* @return XML转换成对象
* @throws JAXBException
*/
public static <T> T xmlToBean(String xml, T t) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(t.getClass());
Unmarshaller um = context.createUnmarshaller();
StringReader sr = new StringReader(xml);
t = (T) um.unmarshal(sr);
return t;
}
/**
* @param smsDeliverReq
* @return 对象转换XML
* @throws JAXBException
* @throws FileNotFoundException
*/
public static <T> StringWriter beanToXml(T t) throws JAXBException, FileNotFoundException {
JAXBContext context = JAXBContext.newInstance(t.getClass());
Marshaller m = context.createMarshaller();
StringWriter sw = new StringWriter();
m.marshal(t, sw);
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(t, new FileOutputStream("src/Test.xml"));//查找XML文件的位置
return sw;
}
}
调用测试类,进行测试
package AjaxTest;
import java.io.FileNotFoundException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBException;
public class AjaxTest {
/**
* @param args
* @throws JAXBException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, JAXBException {
//调用XML和String交互类
JAXBXmlorString jxs=new JAXBXmlorString();
JAXBEntity ent = new JAXBEntity();//创建对象添加数据
ent.setHeadId("20161224");
ent.setHeadName("Demon_MF");
ent.setHeadPwd("admin");
JAXBEntity1 et1 = new JAXBEntity1();//创建对象添加数据
et1.setListId("1");
et1.setListName("巫妖王");
et1.setListAddress("寒冰王座");
JAXBEntity1 et2 = new JAXBEntity1();//创建对象添加数据
et2.setListId("2");
et2.setListName("伊利丹");
et2.setListAddress("黑暗神殿");
List<JAXBEntity1> listEntity=new ArrayList<JAXBEntity1>();//创建集合添加对象
listEntity.add(et1);
listEntity.add(et2);
JAXBEntity2 entity2= new JAXBEntity2();//调用对象存放类
entity2.setListEnityt(listEntity); //将上面的集合和对象放入相应的参数位置
entity2.setEntity(ent);
//对象转换成XML并放入String字符串中
StringWriter sw = jxs.beanToXml(entity2);
System.out.println("转换后的xml"+ sw.toString());
JAXBEntity2 AE2 =jxs.xmlToBean(sw.toString(), entity2);
System.out.println("将XML转换成对象后输出集合信息:");
for (int i = 0; i < AE2.getListEnityt().size(); i++) {
System.out.println(AE2.getListEnityt().get(i).getListId());
System.out.println(AE2.getListEnityt().get(i).getListName());
System.out.println(AE2.getListEnityt().get(i).getListAddress());
}
System.out.println("读取节点");
System.out.println(AE2.getEntity().getHeadId()+" : "+AE2.getEntity().getHeadName());
}
}
在Test.xml文件中出现一下内容
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<JAXBMax xmlns:ns2="www.baidu.com">
<ns2:JAXBEntity>
<JAXB_headId>20161224</JAXB_headId>
<JAXB_headName>Demon_MF</JAXB_headName>
<JAXB_headPwd>admin</JAXB_headPwd>
</ns2:JAXBEntity>
<JAXBList_Entity1>
<Entity1>
<body_Address>寒冰王座</body_Address>
<body_Id>1</body_Id>
<body_Name>巫妖王</body_Name>
</Entity1>
<Entity1>
<body_Address>黑暗神殿</body_Address>
<body_Id>2</body_Id>
<body_Name>伊利丹</body_Name>
</Entity1>
</JAXBList_Entity1>
</JAXBMax>