Java and XML

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lucia

    #1

    Java and XML

    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

    #2
    Re: Java and XML

    Lucia wrote:[color=blue]
    > 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?[/color]

    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.)

    Comment

    • Michael Borgwardt

      #3
      Re: Java and XML

      Lucia wrote:[color=blue]
      > 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?[/color]

      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?

      Comment

      • Steve Slatcher

        #4
        Re: Java and XML

        Lucia wrote:[color=blue]
        > 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?[/color]

        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.



        Comment

        Working...