1

Here I need to create a XML for soap request. It may have multiple userid tags like below.

<userid>123</userid>
<userid>456</userid>
...

Below is my code to add that tags into XML.

SOAPElement userid1 = example.addChildElement("userid");
SOAPElement userid2 = example.addChildElement("userid");
userid1.addTextNode("123");
userid2.addTextNode("456");

The above code works for two userids but not more then that so below is the java code to add tags and values to XML.

for(int i = 0; i < userids.length; i++){
    SOAPElement userid+i = example.addChildElement("userid");
    userid+i.addTextNode(userids[i]);
}

Here the issue is SOAPElement userid+i = example.addChildElement("userid"); is not working.

5
  • 2
    you can't change a variables name dynamically. What you can do is use an array or arrayList Commented Aug 27, 2015 at 12:16
  • 2
    possible duplicate of Assigning variables with dynamic names in Java Commented Aug 27, 2015 at 12:17
  • possible duplicate of How to create variables dynamically in Java? Commented Aug 27, 2015 at 12:18
  • 1
    Maybe because if you google this question title the first link is to a duplicate of this question where OP could get the answer. Maybe because OP has shown 0 effort to solve the problem. Maybe because of the extremely detailed problem statement of "[this line of code] is not working" I'm trying to figure out why there was ever an upvote. Commented Aug 27, 2015 at 12:19
  • userid+i is a rvalue and cannot be assigned any value Commented Aug 27, 2015 at 12:20

1 Answer 1

4
SOAPElement[] userid = new SOAPElement[userids.length]
for(int i=0; i<userids.length; i++){
        userid[i] = example.addChildElement("userid");
        userid[i].addTextNode(userids[i]);
    }

'userid+i' is not an acceptable java variable name (identifier) so you must be getting a compile time error like i cannot be resolved to a variable.

Better approach is to use an array of values, you can use an array of SOAPElement objects as I have listed above or other (Like List) Implementations of java Collections

Also read valid java identifier rules

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

1 Comment

Tried, for loop first line is saying, Can not covert SOAPElement to SOAPElement[].

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.