WMB入门之十四:在JavaCompute节点中删除输入XML中特定属性及自定义NameSpace节点后再输出
代码:
输入:
输出:
import
java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import com.ibm.broker.javacompute.MbJavaComputeNode;
import com.ibm.broker.plugin.MbElement;
import com.ibm.broker.plugin.MbException;
import com.ibm.broker.plugin.MbMessage;
import com.ibm.broker.plugin.MbMessageAssembly;
import com.ibm.broker.plugin.MbOutputTerminal;
public class subflow_JavaCompute extends MbJavaComputeNode {
public void evaluate(MbMessageAssembly inAssembly) throws MbException {
MbOutputTerminal out = getOutputTerminal( " out " );
MbOutputTerminal alt = getOutputTerminal( " alternate " );
MbMessage inMessage = inAssembly.getMessage();
MbMessage outMessage = new MbMessage(inMessage);
MbMessageAssembly outAssembly = new MbMessageAssembly(inAssembly,outMessage);
MbElement document = outMessage.getRootElement();
MbElement root = document.getLastChild().getFirstChild();
try {
Set < String > selfDefNSSet = new HashSet < String > ();// 用来存储自定义的名称空间名
traverseAndRemoveNils(root,selfDefNSSet);// 遍历节点删除特定属性
if (selfDefNSSet.size() > 0 ){
traverseAndRemoveSelfDefNSNode(root,selfDefNSSet);// 遍历节点删除自定义名称空间节点
}
}
catch (Exception ex){
ex.printStackTrace();
}
out.propagate(outAssembly);
}
private void traverseAndRemoveNils(MbElement elm,Set < String > selfDefNSSet) throws Exception{
// get all the attributes of current element
List < MbElement > attrSet = (List < MbElement > )elm.evaluateXPath( " @* " );
for (MbElement attr:attrSet){
String name = attr.getName().toString();
String value = attr.getValue().toString();
if (isNillAttr(name,value)){
attr.detach();
}
if (isSelfDefNS(name,value)){
String[] arr = name.split( " : " );
selfDefNSSet.add(arr[ 1 ]);
attr.detach();
}
}
// get all sub elements of current element
List < MbElement > nodeset = (List < MbElement > )elm.evaluateXPath( " * " );
for (MbElement childElm:nodeset){
// recursion
traverseAndRemoveNils(childElm,selfDefNSSet);
}
}
// 判断是否特定属性,a:nil,b:nil都算
private boolean isNillAttr(String name,String value){
String patternStr = " (\\w+)(:nil) " ;
boolean result1 = Pattern.matches(patternStr, name);
boolean result2 = " true " .equals(value);
return result1 && result2;
}
// 判断是否自定义名称空间
private boolean isSelfDefNS(String name,String value){
String patternStr = " (xmlns:)(\\w+) " ;
boolean result = Pattern.matches(patternStr, name);
return result;
}
private void traverseAndRemoveSelfDefNSNode(MbElement elm,Set < String > selfDefNSSet) throws Exception{
String name = elm.getName().toString();
if (isSelfDefNSNode(name,selfDefNSSet)){
elm.detach();
}
// get all sub elements of current element
List < MbElement > nodeset = (List < MbElement > )elm.evaluateXPath( " * " );
for (MbElement childElm:nodeset){
// recursion
traverseAndRemoveSelfDefNSNode(childElm,selfDefNSSet);
}
}
// 判读节点是否自定义名称空间的限定名
private boolean isSelfDefNSNode(String name,Set < String > selfDefNSSet){
String[] arr = name.split( " : " );
if (arr.length == 2 ){
String ns = arr[ 0 ];
return selfDefNSSet.contains(ns);
} else {
return false ;
}
}
}
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import com.ibm.broker.javacompute.MbJavaComputeNode;
import com.ibm.broker.plugin.MbElement;
import com.ibm.broker.plugin.MbException;
import com.ibm.broker.plugin.MbMessage;
import com.ibm.broker.plugin.MbMessageAssembly;
import com.ibm.broker.plugin.MbOutputTerminal;
public class subflow_JavaCompute extends MbJavaComputeNode {
public void evaluate(MbMessageAssembly inAssembly) throws MbException {
MbOutputTerminal out = getOutputTerminal( " out " );
MbOutputTerminal alt = getOutputTerminal( " alternate " );
MbMessage inMessage = inAssembly.getMessage();
MbMessage outMessage = new MbMessage(inMessage);
MbMessageAssembly outAssembly = new MbMessageAssembly(inAssembly,outMessage);
MbElement document = outMessage.getRootElement();
MbElement root = document.getLastChild().getFirstChild();
try {
Set < String > selfDefNSSet = new HashSet < String > ();// 用来存储自定义的名称空间名
traverseAndRemoveNils(root,selfDefNSSet);// 遍历节点删除特定属性
if (selfDefNSSet.size() > 0 ){
traverseAndRemoveSelfDefNSNode(root,selfDefNSSet);// 遍历节点删除自定义名称空间节点
}
}
catch (Exception ex){
ex.printStackTrace();
}
out.propagate(outAssembly);
}
private void traverseAndRemoveNils(MbElement elm,Set < String > selfDefNSSet) throws Exception{
// get all the attributes of current element
List < MbElement > attrSet = (List < MbElement > )elm.evaluateXPath( " @* " );
for (MbElement attr:attrSet){
String name = attr.getName().toString();
String value = attr.getValue().toString();
if (isNillAttr(name,value)){
attr.detach();
}
if (isSelfDefNS(name,value)){
String[] arr = name.split( " : " );
selfDefNSSet.add(arr[ 1 ]);
attr.detach();
}
}
// get all sub elements of current element
List < MbElement > nodeset = (List < MbElement > )elm.evaluateXPath( " * " );
for (MbElement childElm:nodeset){
// recursion
traverseAndRemoveNils(childElm,selfDefNSSet);
}
}
// 判断是否特定属性,a:nil,b:nil都算
private boolean isNillAttr(String name,String value){
String patternStr = " (\\w+)(:nil) " ;
boolean result1 = Pattern.matches(patternStr, name);
boolean result2 = " true " .equals(value);
return result1 && result2;
}
// 判断是否自定义名称空间
private boolean isSelfDefNS(String name,String value){
String patternStr = " (xmlns:)(\\w+) " ;
boolean result = Pattern.matches(patternStr, name);
return result;
}
private void traverseAndRemoveSelfDefNSNode(MbElement elm,Set < String > selfDefNSSet) throws Exception{
String name = elm.getName().toString();
if (isSelfDefNSNode(name,selfDefNSSet)){
elm.detach();
}
// get all sub elements of current element
List < MbElement > nodeset = (List < MbElement > )elm.evaluateXPath( " * " );
for (MbElement childElm:nodeset){
// recursion
traverseAndRemoveSelfDefNSNode(childElm,selfDefNSSet);
}
}
// 判读节点是否自定义名称空间的限定名
private boolean isSelfDefNSNode(String name,Set < String > selfDefNSSet){
String[] arr = name.split( " : " );
if (arr.length == 2 ){
String ns = arr[ 0 ];
return selfDefNSSet.contains(ns);
} else {
return false ;
}
}
}
输入:
<contactInfo
xmlns:ns1="http://tbccorp.com/Services/CustomerMgt/v1/"><homePhoneNumber as:b="c"><key xsi:nil="true"/><value></value></homePhoneNumber><workPhoneNumber>
<ns1:a>1644</ns1:a><key xsi:nil="true"/><value
ns1:nil="true">5</value></workPhoneNumber>
<ns1:b>1949</ns1:b><mobilePhoneNumber
xmlns:ns1="http://tbccorp.com/Services/CustomerMgt/v1/"><key
xsi:nil="true"/><value>3159867097</value></mobilePhoneNumber></contactInfo>
输出:
<contactInfo><homePhoneNumber as:b="c"><key/><value/></homePhoneNumber><workPhoneNumber><key/><value>5</value></workPhoneNumber><mobilePhoneNumber><key/><value>3159867097</value></mobilePhoneNumber></contactInfo>