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.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
  • 2
    you can't change a variables name dynamically. What you can do is use an array or arrayList – Uma Kanth Aug 27 '15 at 12:16
  • 2
    possible duplicate of [Assigning variables with dynamic names in Java](http://stackoverflow.com/questions/6729605/assigning-variables-with-dynamic-names-in-java) – Andreas Fester Aug 27 '15 at 12:17
  • possible duplicate of [How to create variables dynamically in Java?](http://stackoverflow.com/questions/19336202/how-to-create-variables-dynamically-in-java) – Andreas Fester Aug 27 '15 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. – takendarkk Aug 27 '15 at 12:19
  • userid+i is a rvalue and cannot be assigned any value – Willi Mentzel Aug 27 '15 at 12:20

1 Answers1

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

user1933888
  • 2,897
  • 3
  • 27
  • 36