Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So I have come a long way since my first question, kind of feel like I know what I'm doing.

Anyway I have this method here that creates XML elements/values using JAXB. It is set up like:

addXML(String...xml);

which is used like

javabean.addXML("tagname","tagvalue", "tagname2", "tagvalue2", etc..);

Now, often many of these "tag values" are numerical. Further, they may be allocated to varialbes, in which case it would be nice to just type in the variable name in place of "tagvalue" -- but as of now, that first requires a conversion to String.

So, my question, is it possible to create a method that takes a VARIABLE amount of string/double pairs?

i.e.

value1 = 1.0;
value2 = 3.5403;
javabean.addXML("tagname", 1.0, "tagname2", value2,  etc...);

Would I need to define some sort of custom arraylist that alternates between strings and doubles and have a variable amount of those lists as arguments?

Thank you,

Ten

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

Create your own Tag class with attributes for the tag name and the tag value. Then use Tag as the type of your varargs argument:

addXML(Tag... tags);

and you can call your method like this:

javabean.addXML(new Tag("tagname", 1.0), new Tag("tagname2", value2));
share|improve this answer
    
Did as you said. Made a tag class with overloaded constructors for int, string, and double values, and a returnable class ArrayList<Object> to store them. so addXML(Tag...tags) can check and create JAXBElements with the proper arguments. Thank you for the suggestion! –  tenwest Aug 3 '13 at 0:17
add comment

Two options:

  1. Declare your method as addXml(Object ...), then validate that the inputs are properly-alternating strings and doubles in the method itself at runtime. This allows you to write a function that looks exactly like your example, but won't let Java statically detect any errors in method invocation.

  2. Define a class that pairs up strings and numbers, and use that. This requires callers to do a little more typing, but allows the typechecker to ensure that the method is used correctly.

share|improve this answer
add comment

It's better to not have to handle calls with many parameters. It is better to allow the user to call the method more than once and add the tag/value pairs internally.

So the method call would be like:

javabean.addDoubleElement(String tag, double value);

Note that addXML is not very descriptive. Beware of names that are too general. Also be sure that you don't unnecessarily mix encoding and types. addXML is OK for a class that performs encoding, but not so good for a class that describes a data type.

share|improve this answer
    
addXML is not the actual name, just for demo/question asking purposes :) –  tenwest Aug 2 '13 at 20:27
    
Ah, that's OK then... –  owlstead Aug 2 '13 at 20:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.