Commit 36fb2644 by chengzhuoshen

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

parent 0058d1ce
...@@ -42,11 +42,6 @@ public class Mx2ElementCreatorManager { ...@@ -42,11 +42,6 @@ public class Mx2ElementCreatorManager {
public Map<String, Object> mx2ElementMaps(String xmlStr) throws SwiftException { public Map<String, Object> mx2ElementMaps(String xmlStr) throws SwiftException {
xmlStr = handlingXml(xmlStr); xmlStr = handlingXml(xmlStr);
AbstractMX abstractMX = AbstractMX.parse(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() String messageType = (abstractMX.getMxId().getBusinessProcess().name()
+ "." + "."
+ abstractMX.getMxId().getFunctionality() + abstractMX.getMxId().getFunctionality()
...@@ -92,9 +87,9 @@ public class Mx2ElementCreatorManager { ...@@ -92,9 +87,9 @@ public class Mx2ElementCreatorManager {
flag = false; flag = false;
} }
if (flag) { //如果是有效xml报文,直接返回 if (flag) { //如果是有效xml报文,直接返回
return xmlStr; return XmlUtil.clearXmlNamespacePrefix(xmlStr);
} else { } else {
return SwiftTransfer.fetchXmlFromMixMessage(xmlStr); return XmlUtil.clearXmlNamespacePrefix(SwiftTransfer.fetchXmlFromMixMessage(xmlStr));
} }
} }
} }
...@@ -40,7 +40,7 @@ public class Field59Generate extends AbstractMx2MtTagsGenerate { ...@@ -40,7 +40,7 @@ public class Field59Generate extends AbstractMx2MtTagsGenerate {
int addressLineCount = getXmlNodeCounts(bodyHdrParentElementName, document, "CdtTrfTxInf.Cdtr.PstlAdr.AdrLine"); int addressLineCount = getXmlNodeCounts(bodyHdrParentElementName, document, "CdtTrfTxInf.Cdtr.PstlAdr.AdrLine");
String addressLine1 = ""; String addressLine1 = "";
if (addressLineCount > 0) { 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 partyName = getXmlNodeValue(bodyHdrParentElementName, document, "CdtTrfTxInf.Cdtr.Nm");
String value = ""; String value = "";
......
...@@ -11,6 +11,8 @@ import java.net.URL; ...@@ -11,6 +11,8 @@ import java.net.URL;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class XmlUtil { public class XmlUtil {
...@@ -188,39 +190,16 @@ public class XmlUtil { ...@@ -188,39 +190,16 @@ public class XmlUtil {
} }
public static String clearXmlNamespace(String xmlStr) { public static String clearXmlNamespacePrefix(String xmlStr) {
try { String regex = "[AppHdr|Document][\\s]*xmlns:([\\w]*)=";
SAXReader reader = new SAXReader(); Pattern p = Pattern.compile(regex);
Document document = reader.read(new ByteArrayInputStream(xmlStr.getBytes())); Matcher m = p.matcher(xmlStr);
document.accept(new NameSpaceCleaner()); String prefix = "";
String xml = document.asXML(); while (m.find()) {
return xml.replaceAll("xmlns:([\\w]*)", "xmlns"); prefix = m.group(1);
} catch (DocumentException e) { xmlStr = xmlStr.replace("<" + prefix + ":", "<").replace("</" + prefix + ":", "</");
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);
}
} }
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