I have an XML file containing a bunch of simulation settings (partial example below). I would like to load these settings into a Java class, so that the settings are available later without having to write cumbersome DOM/XPath jargon (and import the associated packages) every time I (or another programmer who isn't fluent in DOM/XPath) want to access a specific setting.
Right now I set up a number of sub-classes that represent each level of information in the XML tree, and "manually" parse out the info to all these sub-classes. The result is, for example, that if I want to get Direction number 3, I can write:
settings.setup.directions[3]
I guess this works ok, but it sure feels rigid.
Is there a smarter way of doing this? Should we just stick to the DOM and skip this conversion business? (Please no!)
Note that I am not looking for instructions on how to load an XML file -- I know how to load it into a DOM document and parse it with XPath.
<?xml version="1.0"?>
<Settings>
    <Identity>
        <JobNumber>1234567</JobNumber>
        <SimulationName>MyTest</SimulationName>
    </Identity>
    <PreProcessing >
        <Tolerance>0.01</Tolerance>
    </PreProcessing >
    <PreprocessedInputData>
        <PreChewedThing></PreChewedThing>
        <OtherThing></OtherThing>
    </PreprocessedInputData>
    <Setup>
        <DomainExtent>
            <XMin>260</XMin>
            <XMax>290</XMax>
            <YMin>523</YMin>
            <YMax>565</YMax>
        </DomainExtent>
        <Directions>
            <Direction Index = "1">0</Direction>
            <Direction Index = "2">10</Direction>
            <Direction Index = "3">20</Direction>
            <Direction Index = "4">30</Direction>
        </Directions>
    </Setup>
</Settings>
                You can use JAXB for this purpose, it is meant to bind XML to Java classes. There is a useful guide on http://jaxb.java.net/guide/ and a tutorial on http://jaxb.java.net/tutorial/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With