XMLSubstitution is a feature developed in Java which substitutes the values of one XML into matching nodes of other XML.Many Java applications use JUnits to test units of code. Testing of applications requires the user to create XML requests and responses. Consider a situation where a server needs to be simulated in order to test the behavior of an application.
In this case, various XML responses can be picked up from a template and the values in the response can be substituted from the request.
XMLSubstitution reduces the effort of creating values for each node and can substitute values in the XML up to any level of child nodes. It can be utilised in all environments and frameworks. This entire feature comprises of 107 lines of code with 3 java methods.
Limitations
XMLSubstitution does not validate the structure of the XML. The level at which the destination tag occurs in the request XML is not verified. The value of the first matching tag is copied in all the matching tags of the response irrespective of the level on which it occurs. User needs to be sure that there are no 2 tags with same names and different values.
In this case, various XML responses can be picked up from a template and the values in the response can be substituted from the request.
XMLSubstitution reduces the effort of creating values for each node and can substitute values in the XML up to any level of child nodes. It can be utilised in all environments and frameworks. This entire feature comprises of 107 lines of code with 3 java methods.
Limitations
XMLSubstitution does not validate the structure of the XML. The level at which the destination tag occurs in the request XML is not verified. The value of the first matching tag is copied in all the matching tags of the response irrespective of the level on which it occurs. User needs to be sure that there are no 2 tags with same names and different values.
import org.apache.xerces.xni.parser.XMLParseException; import org.w3c.dom.*; /** * This method iterates through each tag of response element and * checks if the tag is present in request element and if present replaces the * value of the tag in the response element with the value of corresponding tag * in the request element * @param request Element * @param response Element * @throws XMLParseException */ public static void replaceResponseValuesFromRequest(Element request, Element response) throws XMLParseException { ArrayList arrTag = new ArrayList(); NodeList nodeList = response.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE && node.hasChildNodes()) { NodeList childNodeList = node.getChildNodes(); if (childNodeList.item(0).getNodeType() == Node.ELEMENT_NODE && node.hasChildNodes()) { arrTag.add(node.getNodeName()); } else { try { if(request.getElementsByTagName(node.getNodeName()).getLength() > 0) { replaceNodeValue(request, response, node.getNodeName()); } } catch (XMLParseException xe) { AppParam.getLog().info(node.getNodeName() + " Not found"); } } } } for (int j = 0; j < arrTag.size(); j++) { String tmpTag = (String) arrTag.get(j); NodeList lst = response.getElementsByTagName(tmpTag); if (lst.getLength() == 0) { AppParam.getLog().error("Element " + tmpTag + " not found"); throw new XMLParseException("Element " + tmpTag + " not found"); } Node arrNode = (Element)lst.item(0); if (arrNode.hasChildNodes()) { NodeList tempNodeList = arrNode.getChildNodes(); for (int k = 0; k < tempNodeList.getLength(); k++) { Node tempNode = tempNodeList.item(k); if (tempNode.getNodeType() == tempNode.ELEMENT_NODE) { if (tempNode.hasChildNodes()) { NodeList childNodeList = tempNode.getChildNodes(); if (childNodeList.getLength() > 0) { if(childNodeList.getLength() == 1){ if(childNodeList.item(0).hasChildNodes()){ arrTag.add(tempNode.getNodeName()); } else { replaceNodeValue(request, response, tempNode.getNodeName()); } } if (childNodeList.getLength() > 1){ arrTag.add(tempNode.getNodeName()); } } else { try { if(request.getElementsByTagName(tempNode.getNodeName()).getLength() > 0) { replaceNodeValue(request, response, tempNode.getNodeName()); } } catch (XMLParseException xe) { AppParam.getLog().info(tempNode.getNodeName() + " Not found"); } } } } } } } } /** * This method replaces the value of nodeName Element of response with the * value of nodeName Element of request * @param request Element * @param response Element * @param nodeName String * @throws XMLParseException */ public static void replaceNodeValue(Element request, Element response, String nodeName) throws XMLParseException { Element tmpReqElement = null; try{ NodeList lst = request.getElementsByTagName(nodeName); if (lst.getLength() == 0) { AppParam.getLog().error("Element " + nodeName + " not found"); throw new XMLParseException("Element " + nodeName + " not found"); } tmpReqElement = ((Element)lst.item(0)); }catch(XMLParseException xme){ AppParam.getLog().info(âNodeList Emptyâ); } if( tmpReqElement != null && !DataDefunkifier.isEmpty(tmpReqElement.getChildNodes())) { String reqValue = tmpReqElement.getChildNodes().item(0).getNodeValue(); NodeList childNodeList = tmpReqElement.getChildNodes(); if (childNodeList.item(0).getNodeType() != Node.TEXT_NODE) { AppParam.getLog().error("Received empty response"); throw new XMLParseException("Received empty response", true); } NodeList lst = response.getElementsByTagName(nodeName); if (lst.getLength() == 0) { AppParam.getLog().error("Element " + nodeName + " not found"); throw new XMLParseException("Element " + nodeName + " not found"); } Element tmpResElement = ((Element)lst.item(0)); if( tmpResElement != null ) { setOwnNodeValue(tmpResElement, reqValue); } } } public static void setOwnNodeValue(Node element, String value) throws XMLParseException { NodeList lst = element.getChildNodes(); if (lst.getLength() == 0) { AppParam.getLog().error("Child Element Tag NOT Found"); throw new XMLParseException("Child Element Tag NOT Found"); } lst.item(0).setNodeValue(value); }