Java and XML

Lucia (archive)

Usenet Archive
Joined
Aug 7, 2003
Messages
12
Reaction score
0
Hello,

I'd like store all constants in my java-Application in an XML-file. e.g. <constant> <name>con</name> <type>long</type> <value>10</value> </constant>

After starting the application, the java class should be able to create a variable with the name, the type and the value.

In this example, the java class should call the line at runtime:
long con = 10;

Has anyone an idea?

Thanks

Lucia
 

Johannes Koch

Usenet Archive
Joined
Nov 14, 2000
Messages
801
Reaction score
0
Lucia said:
Hello,

I'd like store all constants in my java-Application in an XML-file. e.g. <constant> <name>con</name> <type>long</type> <value>10</value> </constant>

After starting the application, the java class should be able to create a variable with the name, the type and the value.

In this example, the java class should call the line at runtime:
long con = 10;

Has anyone an idea?

Does the XMLEncoder/XMLDecoder in the java.beans package help you?

f'up2 cjlp
 

Michael Borgwardt

Usenet Archive
Joined
Jan 21, 2003
Messages
1,189
Reaction score
0
Lucia said:
I'd like store all constants in my java-Application in an XML-file. e.g. <constant> <name>con</name> <type>long</type> <value>10</value> </constant>

After starting the application, the java class should be able to create a variable with the name, the type and the value.

In this example, the java class should call the line at runtime:
long con = 10;

Has anyone an idea?

In full generality: impossible. You could set the values via the reflection API, but you'd still have the names and types fixed in the java classes. You could generate the classes via a custom class loader, but then you couldn't refer to the variables except via reflection, which would be a really dumb and unnecessarily complex way to access config data.

All in all, it's simply a very bad idea: lots and lots of complexity for practically no gain. Why not use a .properties file?
 

Steve Slatcher

Usenet Archive
Joined
Jan 4, 2000
Messages
229
Reaction score
0
Lucia said:
Hello,

I'd like store all constants in my java-Application in an XML-file. e.g. <constant> <name>con</name> <type>long</type> <value>10</value> </constant>

After starting the application, the java class should be able to create a variable with the name, the type and the value.

In this example, the java class should call the line at runtime:
long con = 10;

Has anyone an idea?

It wouldn't work exactly as you describe, but you could achieve a similar effect with JAXB. Using JAXB you can easily convert an XML file into a configuration object, and then you could get your "constants" from that object.
 

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
548,042
Messages
3,104,223
Members
248,131
Latest member
Tonypshaw1

Latest Threads

Top