减少XML文档上的重复循环代码

时间:2013-10-04 07:32:00

标签: java code-duplication

我有以下代码部分:

第一种方法:

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;
  }

第二种方法:

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";
  }

现在,正如您所看到的,第一个for循环完全相同,但方法返回值和方法参数不是。现在我不知道如何减少这些重复的代码。

提前谢谢。

1 个答案:

答案 0 :(得分:2)

似乎使用XPath可能有所帮助。请查看tutorial at w3schools和一些usage examples in java