Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8107407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:45:37+00:00 2026-06-06T00:45:37+00:00

I am in the proces of creating a helper method that will allow me

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:45:39+00:00Added an answer on June 6, 2026 at 12:45 am

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that a process is creating a mutex in shared memory and locking it
I'm in the process of creating a C# application which will monitor changes made
I am in the process of creating a dynamic database where user will be
I am developing a library that uses one or more helper executable in the
for that past couple of weeks I have been creating generic script that is
I am in the process of creating an XML Schema and one of my
I'm in the process of creating an android app that makes extensive use of
I'm in the process of creating a method to generate a unique id, of
I am creating a dropdown that slides down when hovered over. This is some
I'm in process creating a form in Plone/PloneFormGen. This form has Multi-Select field that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.