0

I have the following code sections:

First method:

public ArrayList<String> getTestFilesForOrderNumber(Integer testStepOrderNumber, Integer testOrderNumber,
      String version)
  {
    NodeList list = documentTest.getElementsByTagName(XmlElements.TESTSTEPS);
    ArrayList<String> files = new ArrayList<>();
    for (int i = 0; i < list.getLength(); i++)
    {
      Element e = (Element) list.item(i);
      if (e.getAttribute(XmlAttributes.VERSION).equalsIgnoreCase(version))
      {
        list = e.getElementsByTagName(XmlElements.TESTSTEP);
        for (int l = 0; l < list.getLength(); l++)
        {
          e = (Element) list.item(l);
          if (e.getAttribute(XmlAttributes.ORDER_NUMBER).equals(testStepOrderNumber.toString()))
          {
            NodeList testsList = e.getElementsByTagName(XmlElements.TEST);
            for (int j = 0; j < testsList.getLength(); j++)
            {
              Element e2 = (Element) testsList.item(j);
              if (e2.getAttribute(XmlAttributes.ORDER_NUMBER).equals(testOrderNumber.toString()))
              {
                NodeList fileList = e2.getElementsByTagName(XmlElements.FILE);
                for (int k = 0; k < fileList.getLength(); k++)
                {
                  Element e3 = (Element) fileList.item(k);
                  if (e3.getParentNode().getNodeName().equals(XmlElements.FILES))
                  {
                    files.add(e3.getFirstChild().getNodeValue());
                  }
                }
              }
            }
          }
        }
      }
    }

    return files;
  }

Second method:

public String getTestDescription(String langCode, Integer testStepOrderNumber, Integer testOrderNumber, String version)
  {
    NodeList list = documentTest.getElementsByTagName(XmlElements.TESTSTEPS);
    for (int i = 0; i < list.getLength(); i++)
    {
      Element e = (Element) list.item(i);
      if (e.getAttribute(XmlAttributes.VERSION).equalsIgnoreCase(version))
      {
        list = e.getElementsByTagName(XmlElements.TESTSTEP);
        for (int l = 0; l < list.getLength(); l++)
        {
          e = (Element) list.item(l);
          if (e.getAttribute(XmlAttributes.ORDER_NUMBER).equals(testStepOrderNumber.toString()))
          {
            NodeList testsList = e.getElementsByTagName(XmlElements.TEST);
            for (int j = 0; j < testsList.getLength(); j++)
            {
              Element e2 = (Element) testsList.item(j);
              if (e2.getAttribute(XmlAttributes.ORDER_NUMBER).equals(testOrderNumber.toString()))
              {
                NodeList testList = e2.getElementsByTagName(langCode.toUpperCase());
                for (int k = 0; k < testList.getLength(); k++)
                {
                  Element e3 = (Element) testList.item(k);
                  if (e3.getParentNode().getNodeName().equals(XmlElements.DESCRIPTION))
                  {
                    return testList.item(k).getFirstChild().getNodeValue();
                  }
                }
              }
            }
          }
        }
      }
    }

    return "ERROR_NO_TEST_DESC";
  }

Now, as you can see, the first for-loops are exactly the same, but the method return value and method parameters aren't. Now I don't know how to reduce this duplicated code.

Thank you in advance.

1 Answer 1

2

It seems using XPath might help. Please take a look at tutorial at w3schools and some usage examples in java

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.