Java and XML

L

Lucia

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
 
J

Johannes Koch

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
 
M

Michael Borgwardt

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?
 
S

Steve Slatcher

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
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top