How to Iterate over class variables of a class

M

MarkB

Hi

Im writing a new app and want to streamline the way i read and write
configurations to XML ini file.

In the past ive written lines and lines of code to write out
configuration which is ok, but a pain when you add new config
variables and forget to add them to the writeConfig methods.

I figured if i could iterate through a list of CLASS variables, then i
could write out the xml config based on the variable name and then add
the variable value.

E.g. (I know this is not valid java...just want to demonstrate my
thinking)

Class foobar(){

private String configVal1 = "Y";
private String configVal2 = "Hello";


public void writeConfig(String configFile){

Iterator it = new Iterator(of class variables)

do (it.hasNext())

if (it is type String){
Write "<" + it.variableName + ">" + it.variableName.Value +
"</" + it.variableName + ">";
}
}
}
}


Can this be done???? Ive tried search the web but there doesnt seem
to be much on it??

Cheers
 
G

GArlington

Hi

Im writing a new app and want to streamline the way i read and write
configurations to XML ini file.

In the past ive written lines and lines of code to write out
configuration which is ok, but a pain when you add new config
variables and forget to add them to the writeConfig methods.

I figured if i could iterate through a list of CLASS variables, then i
could write out the xml config based on the variable name and then add
the variable value.

E.g.  (I know this is not valid java...just want to demonstrate my
thinking)

Class foobar(){

 private String configVal1 = "Y";
 private String configVal2 = "Hello";

 public void writeConfig(String configFile){

 Iterator it = new Iterator(of class variables)

 do (it.hasNext())

     if (it is type String){
         Write "<" + it.variableName + ">" + it.variableName.Value +
"</" + it.variableName + ">";
     }
 }

}
}

Can this be done????   Ive tried search the web but there doesnt seem
to be much on it??

Cheers

Try searching for "reflection" and XML related Java classes, I suspect
that most programmers have done something similar at one point of
time, so search source code sites too...
 
J

James Prickett

Hi

Im writing a new app and want to streamline the way i read and write
configurations to XML ini file.

In the past ive written lines and lines of code to write out
configuration which is ok, but a pain when you add new config
variables and forget to add them to the writeConfig methods.

I figured if i could iterate through a list of CLASS variables, then i
could write out the xml config based on the variable name and then add
the variable value.

E.g.  (I know this is not valid java...just want to demonstrate my
thinking)

Class foobar(){

 private String configVal1 = "Y";
 private String configVal2 = "Hello";

 public void writeConfig(String configFile){

 Iterator it = new Iterator(of class variables)

 do (it.hasNext())

     if (it is type String){
         Write "<" + it.variableName + ">" + it.variableName.Value +
"</" + it.variableName + ">";
     }
 }

}
}

Can this be done????   Ive tried search the web but there doesnt seem
to be much on it??

Cheers

there is a class in apache commons lang called ToStringBuilder that
uses reflection to generate a nice toString of all your bean getters.

-j
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top