1

I am making an API kind of thing for school for a custom XML writer. I have:

public Document CreateDocument(int loops, int attr, String data[], String dataattr[][][]) {
    Document BetterDoc = DocumentHelper.createDocument();
    Element root = BetterDoc.addElement("root");
    for (int i = 0; i < loops; i++) {
        Element(Object) data[i] = root.addElement(data[i])
        for (int i2 = 0; i < attr; i++) {
            .addAtribute(dataattr[i][i2][0], dataattr[i][i2][1])
        };
    }

    return BetterDoc;
}

The line that I want help with is:

Element(Object) data[i] = root.addElement(data[i])

I want to create an element with the same name of the data[i].

I am using the dom4j XML .jar in this, by the way.

I have heard of something called a hashmap and if this is the correct method, would someone please explain how to use it.

2

2 Answers 2

3

No. Simply you can't do that. You can't create/access a variable dynamically with it's name. With Reflection you can access but you can't create.

I guess, a map can do the task here just like

map.put(data[i],root.addElement(data[i]);

Above is just an example code to throw some light.

Sign up to request clarification or add additional context in comments.

2 Comments

rightyo thanks alot is there any other method you would suggest to do it?
@Piemansam5 update my post with some possible solution. Is it what you are looking for ?
0

You can't create dynamic variable unlike Groovy, PHP or Javascript, but you can create an array or reuse an existing variable:

With an existing variable:

public Document CreateDocument(int loops, int attr, String data[], String dataattr[][][]) {
    Document BetterDoc = DocumentHelper.createDocument();
    Element root = BetterDoc.addElement("root");
    for (int i = 0; i < loops; i++) {
        Element _data = root.addElement(data[i]);
        for (int i2 = 0; i < attr; i++) {
            _data.addAtribute(dataattr[i][i2][0], dataattr[i][i2][1])
        };
    }    
    return BetterDoc;
}

With an array:

public Document CreateDocument(int loops, int attr, String data[], String dataattr[][][]) {
    Document BetterDoc = DocumentHelper.createDocument();
    Element root = BetterDoc.addElement("root");
    Element[] _data = new Element[loops];
    for (int i = 0; i < loops; i++) {
        _data[i] = root.addElement(data[i]);
        for (int i2 = 0; i < attr; i++) {
            _data[i].addAtribute(dataattr[i][i2][0], dataattr[i][i2][1])
        };
    }    
    return BetterDoc;
}

You can replace array with an ArrayList if you prefer.

2 Comments

Thanks alot that is what i wanted haha youre a legend xoxo
Notice that the first example was updated (compile error, data already defined) and unless you really need the _data array, use the first solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.