Skip to content

A distillation of the popular Apache XML Digester, but with a leaner more expressive API

Notifications You must be signed in to change notification settings

matyb/xml-ninja

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 

Repository files navigation

XmlNinja v.01 pre-alpha

A simple library that makes reading xml a breeze. This class was inspired by the Apache Commons XMLDigester, but
with a more compact and expressive API. For instance:
<config>
	<databases>
		<database id="test1">
			<url>jdbc:mysql://localhost:3306/test1 </url>
		</database>
		<database url="jdbc:mysql://localhost:3306/test2 ">
			<id>test2</id>
		</database>
	</databases>
</config>

when parsed - will send values for corresponding elements/attribues to a method you define. One option for defining this is with an annotation:
@CallFor(path="/config/databases/database", args={id, url}) 
public void addDataBase(String id, String url){ ...

if you go the annotation route, invoke the mapFromAnnotations method before you invoke parse - that will set the expectations from any instance of a class containing methods annotated with @CallFor to participate as a handler when parse is invoked.

or if you wish to have an imported/unmodified class's method invoked instead:
new XmlNinja(this).addMethodCall("/config/databases/database", "addDatabase", "id", "url");
                                              ^                       ^         ^     ^
                                            path                method name    arg1  arg2

reception can even be split accross handlers - which do not need to implement any interface or extend from any type!
new XmlNinja().addMethodCall("/config/databases/database", "addDatabase", this, "id", "url")
              .addMethodCall("/config/meh", "addMeh", new MehHandler(), "meh");
			  
When parsing the xml snippet above the XmlNinja will call back to you with pre parsed and ordered (as defined in the expectation, not the xml) strings for each declared arg. So given the snippet above, the addDatabase call will get hit with:
public void addDataBase(String id, String url){
	System.out.println(id);
	System.out.println(url);
}

The output would be:
test1
jdbc:mysql://localhost:3306/test1
test2
jdbc:mysql://localhost:3306/test2

Complete examples of useage can be found in the test folder.

Issues/Gotchas:
-@see the XmlNinjaHierarchicalDataStructureTest - royally inconsistent xml is still a problem :(
-handles POX (plain ole xml) almost exclusively. CDATA & namespaces are for a future release.

Happy Xml'ing!

About

A distillation of the popular Apache XML Digester, but with a leaner more expressive API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages