简体   繁体   中英

How do I make a static recursive method return a HashMap?

I am in the proces of creating a helper method that will allow me to traverse an XML document and put each element in a data HashMap and then return it. Here is the method I've been working on :

private static HashMap<String, String> traverseNodes( Node n, HashMap<String,String> data ) 
{
    NodeList children = n.getChildNodes();
    if( children != null ) {
      for( int i = 0; i < children.getLength(); i++ ) {
        Node childNode = children.item( i );

        String nodeName = childNode.getNodeName();
        if(childNode instanceof Element)
        {
            data.put(nodeName, getStringByTag(nodeName, (Element)childNode));
            Log.d("traversal", childNode.getNodeName() + " was saved in hashmap");
        }


        else
            Log.d("traversal", childNode.getNodeName() + " Is not an Element type");

        System.out.println( "node name = " + childNode.getNodeName() );
        traverseNodes( childNode, data );
      }
    }

    return data;

}

I tried running the example and although I get messages saying that "childNode.getNodeName() + "was saved in hashmap". but the HashMap returned is empty.

What am I doing wrong?

EDIT! Bonus Question : I edited the code to reflect the changes suggested. It seems, however, that the method itself doesn't save the values of my XML Document. Are there any problems with the logic in the method ?

You're creating a new map on each call. You need to pass the existing map into the recursive call. It should probably look like this:

private static Map<String, String> traverseNodes(Node n) {
    Map<String, String> map = new HashMap<String, String>();
    traverseNodesRecurse(n, map);
    return map;
}

private static void traverseNodesRecurse(Node n, Map<String, String> map) {
    // Logic as per question
    // Recursive call (in the loop, etc)
    traverseNodes(childNode, map);

    // No need for a return statement
}

我会说将HashMap保留在方法之外并将其作为参数传递 -

private static HashMap<String, String> traverseNodes( Node n, HashMap<String, String> data  ) 

I think you need something like that

private static HashMap<String, String> traverseNodes( Node n ) {
    HashMap<String, String> data = new HashMap<String, String>();
    traverseNodes( n, data );
    return data;
}

private static void traverseNodes( Node n, HashMap<String, String> data ) {
    // Here is your recursive code that do data.put(key, value)
}
private static HashMap<String, String> data = new HashMap<String, String>();

private static traverseNodes( Node n) 
{
    NodeList children = n.getChildNodes();
    if( children != null ) {
      for( int i = 0; i < children.getLength(); i++ ) {


        Node childNode = children.item( i );

        if(childNode instanceof Element)
        {

            String nodeName = childNode.getNodeName();
            data.put(nodeName, getStringByTag(nodeName, (Element)childNode));
            Log.d("traversal", childNode.getNodeName() + " was saved in hashmap");
        }


        else
            Log.d("traversal", childNode.getNodeName() + " Is not an Element type");

        System.out.println( "node name = " + childNode.getNodeName() );
        traverseNodes( childNode);
      }
    }

    return data;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM