JAXB+DOM4J 去除命名空间后缀

Posted sunqingwei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAXB+DOM4J 去除命名空间后缀相关的知识,希望对你有一定的参考价值。

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 去除命名空间后缀的主要内容,如果未能解决你的问题,请参考以下文章

JAXB:需要所有元素的命名空间前缀

JAXB 编组:更改应声明命名空间的元素

使用命名空间和前缀的 JAXB 解组

JAXB:如何在解组 XML 文档期间忽略命名空间?

如何获取JAXB对象的命名空间

JAXB:为啥在生成的 xml 文档中未使用定义的命名空间前缀?