Comfortable XPath accessor
Three weeks ago I blogged about a "Groovy way" to create XML documents in Java. This article now is about a convenient way to access XML without requiring more than a single class (downloadable here) on top of the JRE's own XML library functions.In fact I wrote this class before I even looked for an easy way to create XML myself, because at the time I just had to parse some XML and extract the values in an easy to use fashion.I believe it is easiest to show an example of how to use the class. Consider the following very simple Java class:
import java.math.BigDecimal;
/**
* Simple value object for a contact.
*/
public class Contact {
public String firstname;
public String lastname;
public boolean withAccount;
public Integer numberOfCalls;
public BigDecimal amountDue;
} Usually the fields would not be public of course, but for the sake of the example, just imagine the getters and setters being there ;-)Now consider getting an XML string back from an external sy…
/**
* Simple value object for a contact.
*/
public class Contact {
public String firstname;
public String lastname;
public boolean withAccount;
public Integer numberOfCalls;
public BigDecimal amountDue;
} Usually the fields would not be public of course, but for the sake of the example, just imagine the getters and setters being there ;-)Now consider getting an XML string back from an external sy…