xml-matchers - Hamcrest Matchers for XML

I recently published a small, open source project that I’ve been messing around with for probably close to two years now but never got around to cleaning up and publishing. It’s called xml-matchers and is a collection of Hamcrest matchers for matching against XML documents. I primarily created it for testing but I’m sure you could find other uses for it. The project is hosted at Google Code: http://code.google.com/p/xml-matchers/.

The matchers allow you to do things like:

Source xml = ...
assertThat(xml, hasXPath("/mountains/mountain"));
 
Node xml = ...
assertThat(
       the(xml),
        hasXPath("count(/mountains/mountain)", 
                returningANumber(), 
                greaterThanOrEqualTo(2d)));
 
 
String xml = "<mountains><mountain>K2</mountain></mountains>";
String xmlWithSpaceWrappingText = "<mountains><mountain>\n\tK2\n\t </mountain></mountains>";
assertThat(the(xml), isEquivalentTo(the(xmlWithSpaceWrappingText)));
 
 
Schema schema = w3cXmlSchemaFromClasspath("org/xmlmatchers/validation/example.xsd");
String xml = "<person private=\"true\"><name>Dave</name></person>";
assertThat(the(xml), conformsTo(schema));

Check out the project site for more examples and tutorials. I’m planning on adding some more documentation soon and might write some blog posts to demonstrate using xml-matchers for driving the development of XSL transforms using TDD. Stay tuned…

The project is still at a pre-1.0 version as of now because I’d like to add a couple more small features before a 1.0 release. I’d also like to get some feedback on usability from people as well. I’ve been personally using it for a while and have found it very useful, especially when doing web service and integration work.

I haven’t gotten around to trying to get it in Maven Central yet so if you are a Maven user you will have to download it and add to your own repository(s). I’ll give the process for publishing an artifact to Maven Central a try soon.

If you do any work with XML, give xml-matchers a try. Provide your feedback or suggestions at the project site.


91 Responses to “xml-matchers - Hamcrest Matchers for XML”

Leave a Reply