Commit 36fb2644 by chengzhuoshen

使用正则表达式的方式去掉命名空间前缀

parent 0058d1ce
......@@ -42,11 +42,6 @@ public class Mx2ElementCreatorManager {
public Map<String, Object> mx2ElementMaps(String xmlStr) throws SwiftException {
xmlStr = handlingXml(xmlStr);
AbstractMX abstractMX = AbstractMX.parse(xmlStr);
//如果存在命名空間則去掉命名空間 重新parse
if (StringUtil.isEmpty(abstractMX.getAppHdr().from())) {
xmlStr = XmlUtil.clearXmlNamespace(xmlStr);
abstractMX = AbstractMX.parse(xmlStr);
}
String messageType = (abstractMX.getMxId().getBusinessProcess().name()
+ "."
+ abstractMX.getMxId().getFunctionality()
......@@ -92,9 +87,9 @@ public class Mx2ElementCreatorManager {
flag = false;
}
if (flag) { //如果是有效xml报文,直接返回
return xmlStr;
return XmlUtil.clearXmlNamespacePrefix(xmlStr);
} else {
return SwiftTransfer.fetchXmlFromMixMessage(xmlStr);
return XmlUtil.clearXmlNamespacePrefix(SwiftTransfer.fetchXmlFromMixMessage(xmlStr));
}
}
}
......@@ -40,7 +40,7 @@ public class Field59Generate extends AbstractMx2MtTagsGenerate {
int addressLineCount = getXmlNodeCounts(bodyHdrParentElementName, document, "CdtTrfTxInf.Cdtr.PstlAdr.AdrLine");
String addressLine1 = "";
if (addressLineCount > 0) {
addressLine1 = getXmlNodeValue(bodyHdrParentElementName, document, "CdtTrfTxInf.Dbtr.PstlAdr.AdrLine(0)");
addressLine1 = getXmlNodeValue(bodyHdrParentElementName, document, "CdtTrfTxInf.Cdtr.PstlAdr.AdrLine(0)");
}
String partyName = getXmlNodeValue(bodyHdrParentElementName, document, "CdtTrfTxInf.Cdtr.Nm");
String value = "";
......
......@@ -11,6 +11,8 @@ import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class XmlUtil {
......@@ -188,39 +190,16 @@ public class XmlUtil {
}
public static String clearXmlNamespace(String xmlStr) {
try {
SAXReader reader = new SAXReader();
Document document = reader.read(new ByteArrayInputStream(xmlStr.getBytes()));
document.accept(new NameSpaceCleaner());
String xml = document.asXML();
return xml.replaceAll("xmlns:([\\w]*)", "xmlns");
} catch (DocumentException e) {
throw new SwiftException(e.getMessage());
}
}
private static class NameSpaceCleaner extends VisitorSupport {
public void visit(Document document) {
((DefaultElement) document.getRootElement())
.setNamespace(Namespace.NO_NAMESPACE);
document.getRootElement().additionalNamespaces().clear();
}
public void visit(Namespace namespace) {
namespace.detach();
}
public void visit(Attribute node) {
if (node.toString().contains("xmlns")
|| node.toString().contains("xsi:")) {
node.detach();
}
}
public void visit(Element node) {
if (node instanceof DefaultElement) {
((DefaultElement) node).setNamespace(Namespace.NO_NAMESPACE);
}
public static String clearXmlNamespacePrefix(String xmlStr) {
String regex = "[AppHdr|Document][\\s]*xmlns:([\\w]*)=";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(xmlStr);
String prefix = "";
while (m.find()) {
prefix = m.group(1);
xmlStr = xmlStr.replace("<" + prefix + ":", "<").replace("</" + prefix + ":", "</");
}
xmlStr = xmlStr.replaceAll("xmlns:([\\w]*)", "xmlns");
return xmlStr;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment