XML parser & builder

Nowadays XML files are used very often.  I  created a couple light utils for creation and parsing simple XML files. Usually I use them for configuration files.

Here are examples of the utils usage:

XMLParser

This is the XML code:

<jerry port="7770">
	<jabber hostName="localhost" port="5222">
		<ssl port="5223" keystore="./ssl/testkeys" keystorePassword="passphrase" />
		<openRegistration value="true" />
	</jabber>
</jerry>

Code:

XMLParser parser = new XMLParser();
Reader in = new InputStreamReader(ioStream);
XMLElement root = parser.parse(in);   

int portAdm = root.getIntegerAttr("port");   

XMLElement jabber = root.getChild("jabber");
if(jabber != null) {
     hostName = jabber.getStringAttr("hostName");
     if(hostName == null) {
         throw new ApplicationException("'hostName' is undefined");
     }   

     int port = jabber.getIntegerAttr("port");   

     XMLElement ssl = jabber.getChild("ssl");
     if(ssl != null) setSSL(ssl);   

     XMLElement openRegistration = jabber.getChild("openRegistration");
     if(openRegistration != null)  openReg = openRegistration.getBooleanAttr("value");
}

  

 XML Builder

Code:

XMLBuilder builder = new XMLBuilder("permissions");
XMLElement root = builder.getRoot();
root.setStringAttr("class", "LicensePermissions");
root.setBoolean("ManageLicenseUsers", true);
root.setBoolean("ManageProjects", false);
System.out.println("XML output:" + builder.build());

This code produces the following XML:

<permissions class="LicensePermissions">
        <ManageLicenseUsers>true</ManageLicenseUsers>
        <ManageProjects>false</ManageProjects>
</permissions>

Download the source code