JAXB-java对象转XML字符串

Posted sunqingwei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAXB-java对象转XML字符串相关的知识,希望对你有一定的参考价值。

工具類

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字符串的主要内容,如果未能解决你的问题,请参考以下文章

Java中Json对象与xml字符串互转

Java对象转xml报文和xml报文转Java对象帮助类

xml字符串转xml对象,xml对象转json对象

xml字符串转对象xml文件转对象

转: JaxbContext生成xml文件或java类对象转化注解

java对象怎么转json数组 跪求啊