业务中可能需要在xml中多层节点,可以通过新建bean类生成多节点
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "request")
@XmlAccessorType(XmlAccessType.FIELD)
public class Goods {
private String goodsId;
private String goodsName;
@XmlElement(name = "goods")
private Goods goods;
public Goods() {
}
public Goods(String goodsId, String goodsName, Goods goods) {
this.goodsId = goodsId;
this.goodsName = goodsName;
this.goods = goods;
}
public String getGoodsId() {
return goodsId;
}
public void setGoodsId(String goodsId) {
this.goodsId = goodsId;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
}
转换xml 类
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 JaxbUtil {
public static String converToXml(Object obj,String encoding){
String result=null;
try {
JAXBContext context=JAXBContext.newInstance(obj.getClass());
Marshaller marshaller=context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
StringWriter writer=new StringWriter();
marshaller.marshal(obj, writer);
result=writer.toString().replace("standalone=\"yes\"", "");
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* JavaBean转换成xml没有报文头信息
* @param xml
* @param c
* @return
*/
public static String converToXmlmeitou(Object obj,String encoding){
String result=null;
try {
JAXBContext context=JAXBContext.newInstance(obj.getClass());
Marshaller marshaller=context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
// 去掉生成xml的默认报文头
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
StringWriter writer=new StringWriter();
marshaller.marshal(obj, writer);
result=writer.toString();
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* xml转换成JavaBean
* @param xml
* @param c
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T converyToJavaBean(String xml, Class<T> c) {
T t = null;
try {
JAXBContext context = JAXBContext.newInstance(c);
Unmarshaller unmarshaller = context.createUnmarshaller();
t = (T) unmarshaller.unmarshal(new StringReader(xml));
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
}
运行:
public static void main(String[] args) {
Goods goods = new Goods();
Goods goods1 = new Goods();
goods1.setGoodsId("12233");
goods1.setGoodsName("六味地黄玩");
goods.setGoods(goods1);
String s = converToXml(goods, "utf-8");
System.out.println(s);
}
<?xml version="1.0" encoding="utf-8" ?>
<request>
<goods>
<goodsId>12233</goodsId>
<goodsName>六味地黄玩</goodsName>
</goods>
</request>