0

我在检索 teststep 的语言本地化时遇到了一些问题。我在这里创建了 XML 文档,我只想<EN>Common Test</EN> <DE>Allgemeiner Test</DE>返回元素值或仅返回DE or EN. 如果我有一个NodeListoutTESTSTEP并且我得到了子节点,我也会从元素中获取语言元素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";
  }
4

1 回答 1

1

试试这个方法。

public ArrayList<String> readXML(String xmlString, String elementName,
        String textContent) {
    ArrayList<String> resultValues = new ArrayList<String>();
    try {
        boolean elementNameFlag = false, nodenameFlag = false;

        DocumentBuilder dbFactory = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();

        InputSource is = new InputSource();

        is.setCharacterStream(new StringReader(xmlString));

        Document doc = dbFactory.parse(is);

        NodeList list = doc.getElementsByTagName("*");

        String rootNodeName = doc.getDocumentElement().getNodeName();

        if (elementName == rootNodeName || textContent == rootNodeName) {
            gl.writeWarning("elementName or nodeName should not be root name");
        } else {

            for (int i = 0; i < list.getLength(); i++) {

                Element element = (Element) list.item(i);
                if (elementName == element.getNodeName()) {
                    elementNameFlag = true;
                }

                if (textContent == element.getNodeName()) {
                    nodenameFlag = true;
                }
            }


            if (elementNameFlag == nodenameFlag) {

                doc.getDocumentElement().normalize();

                NodeList nList = doc.getElementsByTagName(elementName);

                for (int temp = 0; temp < nList.getLength(); temp++) {
                    Node nNode = nList.item(temp);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element eElement = (Element) nNode;

                        resultValues
                                .add(getTagValue(textContent, eElement));
                    }
                }
            } else {

            }
        }
    } catch (Exception e) {

        e.printStackTrace();
    }
    return resultValues;
}// readXML()

打电话

objectname.readXML(xmlString,"TESTSTEP","EN")
于 2013-05-23T09:48:38.353 回答