我在检索 teststep 的语言本地化时遇到了一些问题。我在这里创建了 XML 文档,我只想<EN>Common Test</EN> <DE>Allgemeiner Test</DE>
返回元素值或仅返回DE or EN
. 如果我有一个NodeList
outTESTSTEP
并且我得到了子节点,我也会从元素中获取语言元素TEST
,但我只想从元素中获取一个语言TESTSTEP
元素。我怎样才能做到这一点DOM
?
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<DSCT2C>
<TESTSTEPS APPLICATION="UGS">
<TESTSTEP ORDER_NUMBER="0">
<EN>Common Test</EN>
<DE>Allgemeiner Test</DE>
<TEST ORDER_NUMBER="0" NEED_FUNCTION="true" FUNCTION_NAME="functionName">
<DESCRIPTION>
<DE>Hallo</DE>
<EN>Hello</EN>
</DESCRIPTION>
<FILES>
<FILE>test.prt</FILE>
<FILE>test.drw</FILE>
</FILES>
</TEST>
<TEST ORDER_NUMBER="1" NEED_FUNCTION="false">
<DESCRIPTION>
<DE>Hallo2</DE>
<EN>Hello2</EN>
</DESCRIPTION>
<FILES>
</FILES>
</TEST>
</TESTSTEP>
</TESTSTEPS>
</DSCT2C>
编辑:我自己找到了一个解决方案,而且很简单;):
public String getTestStepDescription(String langCode, Integer orderNumber)
{
NodeList list = documentTest.getElementsByTagName(XmlElements.TESTSTEP.toString());
for (int i = 0; i < list.getLength(); i++)
{
Element e = (Element) list.item(i);
if (e.getAttribute(XmlAttributes.ORDER_NUMBER.toString()).equals(orderNumber.toString()))
{
NodeList neu = e.getElementsByTagName(langCode.toUpperCase());
for(int j = 0; j < neu.getLength(); j++)
{
Element e2 = (Element) neu.item(j);
if(e2.getParentNode().getNodeName().equals(XmlElements.TESTSTEP.toString()))
{
return neu.item(j).getFirstChild().getNodeValue();
}
}
}
}
return "ERROR_NO_TESTSTEP_DESC";
}