标签:org return tin super prope ace json-lib throws att
package cst.spmBase.util;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.sax.SAXSource;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLFilterImpl;
import org.xml.sax.helpers.XMLReaderFactory;
public class XmlUtil {
/**
* java对象转XML字符串
* @param obj
* @return
*/
public static String toXML(Object obj) {
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// //编码格式
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化生成的xml串
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);// 是否省略xm头声明信息
StringWriter out = new StringWriter();
OutputFormat format = new OutputFormat();
format.setIndent(true);
format.setNewlines(true);
format.setNewLineAfterDeclaration(false);
XMLWriter writer = new XMLWriter(out, format);
XMLFilterImpl nsfFilter = new XMLFilterImpl() {
private boolean ignoreNamespace = false;
private String rootNamespace = null;
private boolean isRootElement = true;
@Override
public void startDocument() throws SAXException {
super.startDocument();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
if (this.ignoreNamespace) uri = "";
if (this.isRootElement) this.isRootElement = false;
else if (!uri.equals("") && !localName.contains("xmlns")) localName = localName + " xmlns=\"" + uri + "\"";
super.startElement(uri, localName, localName, atts);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (this.ignoreNamespace) uri = "";
super.endElement(uri, localName, localName);
}
@Override
public void startPrefixMapping(String prefix, String url) throws SAXException {
if (this.rootNamespace != null) url = this.rootNamespace;
if (!this.ignoreNamespace) super.startPrefixMapping("", url);
}
};
nsfFilter.setContentHandler(writer);
marshaller.marshal(obj, nsfFilter);
return out.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<CEB622Message xmlns="http://www.chinaport.gov.cn/ceb" 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>
</CEB622Message>
后缀完美消失 工具类可直接哪里来用
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
标签:org return tin super prope ace json-lib throws att
原文地址:https://www.cnblogs.com/sunqingwei/p/9517526.html