Loading settings from XML file into a Java class
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&开发者_JAVA技巧gt;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 have flexibility over the layout of the XML file, and don't especially need to use a settings class with JAXB, try Commons Configuration:
Creating an XML settings file is as easy as:
XMLConfiguration configCreate = new XMLConfiguration();
configCreate.setFileName("settings.xml");
configCreate.addProperty("somesetting", "somevalue");
configCreate.save();
Reading from the XML settings file:
XMLConfiguration configRead = new XMLConfiguration("settings.xml");
String settingValue = configRead.getString("somesetting");
See http://commons.apache.org/configuration/
In my opinion, the best and simpliest way is using Java and XPath. This is an example:
<settings>
<type>jdbc-mysql</tipus>
<usr>usr</usr>
<pass>pass</pass>
<url>jdbc:mysql://192.168.1.123:3306/notifications_db</url>
<schema>notificacions_db</schema>
<date_format>yyyy-MM-dd HH:mm:ss</date_format>
<prefix_package>false</prefix_package>
<use_ssl>false</use_ssl>
<auto_reconnect>true</auto_reconnect></settings>
Java main class Example:
public static void main(String[] args) {
XPath xpath = XPathFactory.newInstance().newXPath();
String xpathExpression = "/settings";
InputSource inputSource = new InputSource("basedao-settings.xml");
try {
NodeList lstRoot = (NodeList) xpath.compile(xpathExpression).evaluate(inputSource, XPathConstants.NODESET);
NodeList lstChilds = lstRoot.item(0).getChildNodes();
for (int i = 0; i < lstChilds.getLength(); i++) {
System.out.println(lstChilds.item(i).getLocalName());
System.out.println(lstChilds.item(i).getTextContent());
}
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
精彩评论