1

我正在尝试使用以下代码解析 xml,我能够解析它并读取值。但我不确定如何修改代码以读取节点“INFO”,我已经转义了该节点并直接使用 PARAM如下:

XML

<?xml version="1.0" encoding="UTF-8"?>
<myxml>
  <id>23343</id>
  <INFO>
    <PARAM>
      <PARAM_NAME>data</PARAM_NAME>
      <PARAM_VALUE>345</PARAM_VALUE>
    </PARAM>
    <PARAM>
      <PARAM_NAME>CdataID</PARAM_NAME>
      <PARAM_VALUE>6788</PARAM_VALUE>
    </PARAM>
  </INFO>
</myxml>

Java代码是:

      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
      Document doc = dBuilder.parse(fXmlFile);
      doc.getDocumentElement().normalize();

      NodeList nList = doc.getElementsByTagName("PARAM");

      for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
          Element eElement = (Element) nNode;
          if (getTagValue("PARAM_NAME",eElement).equals("data")) {
            String type = getTagValue("PARAM_VALUE", eElement);
          } 
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private static String getTagValue(String sTag, Element eElement) {
    NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
    Node nValue = (Node) nlList.item(0);
    return nValue.getNodeValue();
  }
}

我想添加INFO节点,如何修改上面的代码?

4

0 回答 0