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

  • SEARCH
  • Home
  • 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 7445961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:10:57+00:00 2026-05-29T12:10:57+00:00

Here is an example of a parsed xml file I’m working with that tabs

  • 0

Here is an example of a parsed xml file I’m working with that tabs it into a tree form

commandList

  assign
    variable
      #text[a]
    expression-int
      #text[1]

  assign
    variable
      #text[b]
    expression-int
      #text[2]

  assign
    variable
      #text[c]
    expression-operation
      operator
        #text[OP_SET]
      arguments
        expression-variable
          variable
            #text[a]
        expression-variable
          variable
            #text[b]

  assign
    variable
      #text[d]
    expression-operation
      operator
        #text[OP_SET]
      arguments
        expression-operation
          operator
            #text[OP_TUPLE]
          arguments
            expression-int
              #text[1]
            expression-int
              #text[2]
        expression-operation
          operator
            #text[OP_TUPLE]
          arguments
            expression-int
              #text[3]
            expression-int
              #text[4]

I hope this input isn’t difficult to understand. Here is what it looks like normally when not parsed from an XML file:

a := 1;
b := 2;
c := {1,2};
d := {(1,2),(3,4)};

etc…

All of the assignment pairs (that is, a value and a variable) are to be stored in a hashmap so that the value can be looked up by it’s variable and used in later expressions. I’m to use a recursive descent evaluator (I think?) to solve down the expressions according to the grammar.

I’ve googled all sorts of things for the past 24 hours now and have seen a lot of tree evaluators for basic arithmetic (e.g. 2 + 3 * 8, etc) but haven’t been able to see how that would work for my specific tree.

Code I’ve written so far goes as low as finding the the variable names (a,b,c,d,e etc) but I can’t begin to think of how to code the recursion which will provide the right values for the hash map.

public void evaluate(Node node){
    HashMap<String, String> assignments = new HashMap<String, String>();
    NodeList assignment = node.getChildNodes();
    for (int i=0; i < assignment.getLength(); i++){ //1 to 13
        Node assign = assignment.item(i);
        Node variable = this.getChild(assign, 0);
        Node varValNode = this.getChild(variable, 0);
        String varVal = varValNode.getNodeValue();

        Node expression = this.getChild(assign, 1);

The document, node and nodelist classes for my tree are unusual in that they don’t allow a ‘getChild’ method which I think would save a lot of time. Does anybody know why this is?

Really random problem here and I hope it made sense. Please ask me to elaborate on anything that is unclear and I will try the best that I can. I’m not looking for anyone to solve the problem for me but merely instruct me on how to decide how to code this recursive algorithm.

EDIT:
Also, the second ‘input’ I put above was actually the output. It should have been this:

a := 1;
b := 2;
c := @set(a,b);
d := @set(@tuple(1,2),@tuple(3,4));
  • 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-05-29T12:10:57+00:00Added an answer on May 29, 2026 at 12:10 pm

    Assuming that all your values are of integer type, you should create a HashMap<string,Integer> to store variable values, and pass it to your evaluate method:

    public static void main(String[] args) {
        NodeList commandList = ... // get your XML from somewhere
        Map<string,Integer> vars = new HashMap<string,Integer>();
        for (Node node : commandList) {
            evaluate(node, vars);
        }
        // At this point, vars contains values of all variables assigned in commands
        // from the command list
    }
    

    The evaluation should become relatively straightforward:

    private static Integer evaluate(Node node, Map<string,Integer> vars) {
        if (node is an assignment) {
            String varName = ... // get variable name from node
            Node expr = ... // get the node representing expression being assigned
            Integer value = evaluate(expr, vars);
            vars.put(varName, value);
            return value;
        }
        if (node is a variable reference) {
            String varName = ... // get variable name from node
            return vars.get(varName);
        }
        if (node is an integer constant) {
            String constVal = ... // Get the representation from XML
            return Integer.decode(constVal);
        }
        if (node is a binary expression) {
            Node lhs = ... // Get the left-hand side expression from the node
            Node rhs = ... // Get the right-hand side expression from the node
            Integer lhsVal = evaluate(lhs, vars); 
            Integer rhsVal = evaluate(rhs, vars);
            if (operator is plus) {
                return new Integer(((int)lhsVal) + ((int)rhsVal));
            }
            if (operator is minus) {
                return new Integer(((int)lhsVal) - ((int)rhsVal));
            }
            if (operator is multiply) {
                return new Integer(((int)lhsVal) * ((int)rhsVal));
            }
            if (operator is divide) {
                return new Integer(((int)lhsVal) / ((int)rhsVal));
            }
            // ... and so on
        }
        // ... and so on
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this XML file that I am parsing using the parser here and
This is example of my XML file <head> <cat>some title here... <info>some info here
I'm using tinyxml to parse xml files, and I've found that error handling here
Where does one access the data from a parsed xml file when using libxml?
I'm going to cut straight to the question, I have a XML file that
I have an XML file which describes the data-structure that I can exchange on
I have parsed many differend xml to check, if - for example - a
I want to load xml file into spring bean for further processing. I know
I am having difficulty trying to return values from an XML file. Here is
I am trying to get a xml file with data into Flex application. There

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.