This source code is used to compare the node and values of two xmls given the starting node.
Many Java applications use JUnits to test units of java code. Lets say you need to unit test a method that builds xmls. You will have a standard response xml, which you will need to compare with the actual response xml,i.e, the output of the method being tested. This method compares the nodes and node values of both the responses and returns false if unequal and true if equal. This works for xmls with multiple child nodes, upto any level.Also, because the method takes start node as input, it can also compare part of the xml instead of the entire xml.
The source code is currently compatible with Jdk 1.4.2
Limitations The method retruns a true if xmls are equal and false if they are not. It does not return the reason why the xmls are not equal. However, you can find that from the System.out.println() which prints out the reasons why it deemed the xmls unequal.You could use a customized exception which throws the error message and that message can be caught and printed out from the calling method.
Limitations The method retruns a true if xmls are equal and false if they are not. It does not return the reason why the xmls are not equal. However, you can find that from the System.out.println() which prints out the reasons why it deemed the xmls unequal.You could use a customized exception which throws the error message and that message can be caught and printed out from the calling method.
import org.apache.xerces.xni.XNIException; import org.apache.xerces.xni.parser.XMLParseException; import org.w3c.dom.*; // For XML testing /** * This method compares two elements starting from start node passed as parameter. * Comparison includes tag name and value both * @param response * @param standardResponse * @param startNode * @return boolean - true indicates successful comparison, false indicates that elements did not match * @throws Exception (could be any userdefined exception) */ public static boolean startCompareRequestResponse(Element response, Element standardResponse, String startNode) throws Exception { try { NodeList lst = standardResponse.getElementsByTagName(startNode); Element standardRootNode = (Element)lst.item(0); lst = response.getElementsByTagName(startNode); Element rootNode = (Element)lst.item(0); if (!compareRequestResponse(rootNode, standardRootNode)) { return false; } return true; } catch (Exception ex) { throw new Exception(startNode + " Tag Not found" +ex); } } /** * This method is a recursive method called from startCompareRequestResponse * @param response * @param standardResponse * @return boolean - true indicates successful comparison, false indicates that elements did not match * @throws Exception (could be any userdefined exception) */ public static boolean compareRequestResponse(Element response, Element standardResponse) { for (int i = 0; i < response.getChildNodes().getLength(); i++) { try { if (response.getChildNodes().item(i).getNodeName().equals( standardResponse.getChildNodes().item(i).getNodeName())) { String rootText = getTextValueByTag(response, response.getChildNodes().item(i).getNodeName()).trim(); String standardRootText = getTextValueByTag( standardResponse, standardResponse.getChildNodes().item(i).getNodeName()).trim(); if (!rootText.equals(standardRootText)) { System.out.print(response.getChildNodes().item(i). getNodeName() + " Node Values Not Same"); return false; } } else { System.out.print("Node Names Are Not Same " + response.getChildNodes().item(i). getNodeName() + ", " + standardResponse.getChildNodes().item(i). getNodeName()); return false; } } catch (XMLParseException xmEx) { if (response.getChildNodes().item(i).getNodeName().equals( standardResponse.getChildNodes().item(i).getNodeName())) { compareRequestResponse( (Element) response.getChildNodes().item(i), (Element) standardResponse.getChildNodes(). item(i)); } else { System.out.print("Node Names Are Not Same " + response.getChildNodes().item(i). getNodeName() + ", " + standardResponse.getChildNodes().item(i). getNodeName()); return false; } } } return true; } /** * This method is a method called from compareRequestResponse * @param Node node * @param String tag * @return String- value of the tag * @throws XMLParseException */ public static String getTextValueByTag(Node node, String tag) throws XMLParseException{ Element element = (Element) node; NodeList lst = element.getElementsByTagName(tag); element = (Element) lst.item(0); lst = element.getChildNodes(); Node tempNode = (Node) lst.item(0); if (tempNode == null) { return ""; } String result = tempNode.getNodeValue(); if (result == null) { System.out.print("Element value was null"); throw new XMLParseException(null,"Element value was null"); } return result; }