0
我有以下的代碼段:減少重複的循環代碼在XML文檔
第一種方法:
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-loops完全一樣,但方法返回值和方法參數不是。現在我不知道如何減少這個重複的代碼。
預先感謝您。
@Admint:謝謝! –