码迷,mamicode.com
首页 > 编程语言 > 详细

JAXB-java对象转XML字符串

时间:2018-08-22 16:55:27      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:static   app   tom   repr   this   orm   string   row   ext   

工具類

public class Ben2XmlUtil {

/**
* 将对象直接转换成String类型的 XML输出
*
* @param obj
* @return
*/
public static String convertToXml(Object obj) {
// 创建输出流
StringWriter sw = new StringWriter();
try {
// 利用jdk中自带的转换类实现
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
// 格式化xml输出的格式
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
// 将对象转换成输出流形式的xml
marshaller.marshal(obj, sw);
} catch (JAXBException e) {
e.printStackTrace();
}
return sw.toString();
}

 

//自定义命名空间前缀
public static class PreferredMapper extends NamespacePrefixMapper {
@Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
return "os2";
}
}

 

public static void main(String[] args) throws JAXBException, FileNotFoundException {

CEB622Message customer = new CEB622Message();
book books = new book();
customer.getInventoryReturn().add(books);

 //将java对象转换为XML字符串
   String xmlStr = convertToXml(customer);
   System.out.println(xmlStr);

//自定義命名空間以及後綴 继承了 NamespacePrefixMapper 

   JAXBContext context = JAXBContext.newInstance(customer.getClass());
   Marshaller m = context.createMarshaller();
   NamespacePrefixMapper mapper = new PreferredMapper();
   m.setProperty("com.sun.xml.bind.namespacePrefixMapper", mapper);
   m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
   m.marshal(customer, System.out);

}

}

 

实体

package cst.goodsList.entity;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.*;

@XmlRootElement(name = "CEB622Message")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "name")
public class CEB622Message {

private List<book> InventoryReturn;

@XmlAttribute
private String guid = "123";

public String getGuid() {
return guid;
}

public void setGuid(String guid) {
this.guid = guid;
}

public CEB622Message() {
InventoryReturn = new ArrayList<>();
}

public List<book> getInventoryReturn() {
return InventoryReturn;
}

public void setInventoryReturn(List<book> inventoryReturn) {
InventoryReturn = inventoryReturn;
}
}

实体二//放入真正字段

package cst.goodsList.entity;

public class book {

private String guid ="AA";
private String customsCode ="BB";
private String ebpCode = "CC";
private String ebcCode = "DD";
private String agentCode ="EE";
private String copNo ="FF";
private String preNo ="GG";
private String returnStatus= "HH";
private String returnTime= "JJ";
private String returnInfo="QQ";
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public String getCustomsCode() {
return customsCode;
}
public void setCustomsCode(String customsCode) {
this.customsCode = customsCode;
}
public String getEbpCode() {
return ebpCode;
}
public void setEbpCode(String ebpCode) {
this.ebpCode = ebpCode;
}
public String getEbcCode() {
return ebcCode;
}
public void setEbcCode(String ebcCode) {
this.ebcCode = ebcCode;
}
public String getAgentCode() {
return agentCode;
}
public void setAgentCode(String agentCode) {
this.agentCode = agentCode;
}
public String getCopNo() {
return copNo;
}
public void setCopNo(String copNo) {
this.copNo = copNo;
}
public String getPreNo() {
return preNo;
}
public void setPreNo(String preNo) {
this.preNo = preNo;
}
public String getReturnStatus() {
return returnStatus;
}
public void setReturnStatus(String returnStatus) {
this.returnStatus = returnStatus;
}
public String getReturnTime() {
return returnTime;
}
public void setReturnTime(String returnTime) {
this.returnTime = returnTime;
}
public String getReturnInfo() {
return returnInfo;
}
public void setReturnInfo(String returnInfo) {
this.returnInfo = returnInfo;
}
}

 

反序列话效果

<?xml version="1.0" encoding="UTF-8"?>
<os2:CEB622Message os2:xmlns="com.sun.xml.bind.namespacePrefixMapper" guid="123">
<InventoryReturn>
<agentCode>EE</agentCode>
<copNo>FF</copNo>
<customsCode>BB</customsCode>
<ebcCode>DD</ebcCode>
<ebpCode>CC</ebpCode>
<guid>AA</guid>
<preNo>GG</preNo>
<returnInfo>QQ</returnInfo>
<returnStatus>HH</returnStatus>
<returnTime>JJ</returnTime>
</InventoryReturn>
<os2:/CEB622Message>

------------------------------------------------------代码完毕---------------------------------------------------------

 

JAXB-java对象转XML字符串

标签:static   app   tom   repr   this   orm   string   row   ext   

原文地址:https://www.cnblogs.com/sunqingwei/p/9517512.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!