0
    NodeList nList2 = doc.getElementsByTagName("dep");
    Map<String, List<Map<String, String>>> depMap = new HashMap<String, List<Map<String, String>>>();
    for (int temp = 0; temp < nList2.getLength(); temp++) {
      Element el = (Element)nList2.item(temp);
      String type=el.getAttribute("type");
      Node nNode = nList2.item(temp);
      if (nNode.getNodeType() == Node.ELEMENT_NODE) {
        Element eElement = (Element) nNode;
        List<Map<String,String>> depList = new ArrayList<Map<String,String>>();
        String governor = getTagValue("governor", eElement);
        String dependent = getTagValue("dependent", eElement);
        Map<String, String> govdepmap = new HashMap<String, String>();
        govdepmap.put(governor, dependent);
        depList.add(govdepmap);
        List<Map<String,String>> flist = new ArrayList<Map<String,String>>();
        flist.add(govdepmap);
        depMap.put(type, flist);
      }
    }

我的 XML 文件中有以下结构:

   <basic-dependencies>
              <dep type="nsubj">
                <governor idx="4">going</governor>
                <dependent idx="1">I</dependent>
              </dep>

现在我想存储每个“州长”和“依赖”标签的“idx”属性。我应该更改或添加什么代码?

4

1 回答 1

0

您可以执行以下操作:

    NodeList liste = eElement.getElementsByTagName("governor");
    NodeList liste2 = eElement.getElementsByTagName("dependent");

如果一位州长总是有一位家属,您可以这样做:

    for(int i=0; i<liste.getLength(); i++){
        Element e = (Element)liste.item(i);
        Element e2 = (Element)liste2.item(i);
        govToIdxMap.put(governor,e.getAttribute("idx"));
        depToIdxMap.put(dependent,e2.getAttribute("idx"));
    }
    List<Map<String,String>> listMap = new ArrayList<>();
    listMap.add(govToIdxMap);
    listMap.add(depToIdxMap);
    Map<String,List<Map<String,String>>> final = new Map<String,List<Map<String,String>>>();
    final.put(governor,listMap);
于 2012-07-05T09:26:40.457 回答