Introduction

A simple example

Imagine you are using an XML document like this:

<configuration>
   <file>/path/to/your/project/news.txt</file>
   <entries>32</entries>
   <launchOnStartup>true</launchOnStartup>
</configuration>

You need to extract the three pieces of information "file", "entries" and "launchOnStartup" from the document and want them to be represented in useful data types.

This can be done quite easily using XJConf by creating a second XML-document, that specifies how the XML document should be treated:

<defines>
  <tag name="file" type="java.io.File"/>
  <tag name="entries" type="java.lang.Integer"/>
  <tag name="launchOnStartup" type="java.lang.Boolean"/>
</defines>

Now use XJConf to read the tag definitions and then load the original XML-document:

import net.schst.XJConf.*;

DefinitionParser tagParser = new DefinitionParser();
NamespaceDefinitions defs = tagParser.parse("xml/defines.xml");
XmlReader conf = new XmlReader();
conf.setTagDefinitions(defs);

conf.parse("xml/conf.xml");

Now you can easily access any of the configuration values:

File foo = (File)conf.getConfigValue("file");
Integer entries = (Integer)conf.getConfigValue("entries");

XJConf provides a lot more features as shown in this example, you can use namespaces, attributes, setter methods, collections and many more. The following pages will explain all these features in detail.