Java and XML

Discussion in 'XML' started by Lucia, Oct 22, 2003.

  1. Lucia

    Lucia Guest

    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
    Lucia, Oct 22, 2003
    #1
    1. Advertising

  2. Lucia wrote:
    > 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
    --
    Johannes Koch
    In te domine speravi; non confundar in aeternum.
    (Te Deum, 4th cent.)
    Johannes Koch, Oct 22, 2003
    #2
    1. Advertising

  3. Lucia wrote:
    > 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?
    Michael Borgwardt, Oct 22, 2003
    #3
  4. Lucia wrote:
    > 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.
    Steve Slatcher, Oct 22, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page