Documentation

From ConfigLoader

Jump to: navigation, search

ConfigLoader requires any XML parser in CLASSPATH.

Default parser to use is XERCES. So you should add xercesImpl.jar into your classpath.
You can download latest version here.

xml-apis.jar should also be in your CLASSPATH



Import package:

import com.romanenco.configloader.*;

ConfigLoader object initialization is simple:

ConfigLoader cf = new ConfigLoader();

if you use parser other then XERCES, then you should make init:

ConfigLoader cf = new ConfigLoader(parseClass);

parserClass is string with name of SAX parser class (org.apache.xerces.parsers.SAXParser for XERCES). Class name for your parser you can get from parser's documentation.

You can load configuration from XML file or from String with XML content.

cf.LoadFromFile("myconfig.xml");

or

cf.LoadFromString(stringWithXML);

both methods may throw RuntimeException if file is not found, or in case of wrong XML format.


Main methods you may use:

   getTagValue(<PathToTag>)
getTagChildrenCount(<PathToTag>)
getTagChildrenCountWithName(TagName, <PathToTag>)
getTagAttributeValue(AttrName, <PathToTag>)

PathToTag is set of xml. For example, for xml:

<tag0>
    <tag1>
        <tag2>SOMEVALUE</tag2>
        <tag2>SOMEVALUE2</tag2>
    </tag1>
</tag0>

PathToTag will be "tag0.tag1.tag2", and tag value will be SOMEVALUE.
This path is equal to: "tag0","0","tag1","0","tag2","0"
and equals to "tag0.tag1","0","tag2","0"
So you can run getTagValue with any of these paths

cf.getTagValue("tag0.tag1.tag2");
cf.getTagValue("tag0","0","tag1","0","tag2","0");
cf.getTagValue("tag0.tag1","0","tag2","0");

to get value of tag2 you should use PathToTag like:

cf.getTagValue("tag0.tag1.tag2","1");
cf.getTagValue("tag0","0","tag1","0","tag2","1");
cf.getTagValue("tag0.tag1","0","tag2","1");


Next examples demonstartes main features.

import com.romanenco.configloader.*;

class OurExample {

    public static void main(String argv[]) {
        ConfigLoader cf = new ConfigLoader();
        cf.LoadFromFile("config.xml");

        //see examples section for next code lines
        //...

    }

}

Example 1

Configuration xml:

<root>
	<a>valuea</a>
	<b>valueb</b>
</root>

Java example:

String value = cf.getTagValue("root.a");
// value = "valuea"

value = cf.getTagValue("root.b");
// value = "valueb"

value = cf.getTagValue("root.c");
// value = null

Example 2

Configuration xml:

<root>
	<a a1="at1" a2="at2">valuea</a>
	<b id = "1">
		<c>c1</c>
		<c>c2</c>
		<c1>c21</c1>
	</b>
	<b id = "2">
		<c>c3</c>
	</b>
</root>

Java example:

String value = cf.getTagValue("root.b.c");
// value = "c1"

value = cf.getTagValue("root.a");
// value = "valuea"

value = cf.getTagValue("root","0","a");
// value = "valuea"

value = cf.getTagValue("root.b","0","c");
// value = "c1"

value = cf.getTagValue("root.b","0","c", "0");
// value = "c1"

value = cf.getTagValue("root.b","0","c","1");
// value = "c2"

value = cf.getTagValue("root.b","1","c","0");
// value = "c3"

int valueInt = cf.getTagChildrenCount("root");
// valueInt = 3;

valueInt = cf.getTagChildrenCount("root.a");
// valueInt = 0;

valueInt = cf.getTagChildrenCount("root.b");
//valueInt = 3;

valueInt = cf.getTagChildrenCount("root.b","0");
//valueInt = 3;

valueInt = cf.getTagChildrenCount("root.b","1");
// valueInt = 1;

valueInt = cf.getTagChildrenCountWithName("c","root.b");
// valueInt = 2;

value = cf.getTagAttributeValue("a1","root.a");
// value = "at1"

value = cf.getTagAttributeValue("id","root.b","1");
// value = "2"

value = cf.getTagAttributeValue("id2","root.b","1");
// value = null
Personal tools