1. 程式人生 > >JAXB+DOM4J 去除命名空間後綴

JAXB+DOM4J 去除命名空間後綴

prop uid roo ole ins lines cti lean ()

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>

後綴完美消失 工具類可直接哪裏來用

pom文件

<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>

JAXB+DOM4J 去除命名空間後綴