My favorites | Sign in
Project Hosting will be READ-ONLY Thursday at 3:00pm UTC for up to 3 hours for network maintenance.
Project Home Downloads Wiki Issues Source
Project Information
Members

XPathProxy is a means of generating proxy instances in java of annotated interfaces to access Dom Elements via XPath.

It allows you to access XML like this...

<?xml version="1.0" encoding="UTF-8"?>
<s:Test xmlns:s="http://www.example.com/xml">
  <s:someString>Hello</s:someString>
  <s:anotherString sneaky='boo'>World</s:anotherString>
  <s:booleanExists/>
  <s:booleanTrue>true</s:booleanTrue>
  <s:booleanFalse>false</s:booleanFalse>
  <s:someDouble>123.456</s:someDouble>
  <s:yes>YES</s:yes>
</s:Test>

With an interface like this... (note that the YesNo below is a java5 Enum type)

@NamespaceMapping(prefix = "s", uri = "http://www.example.com/xml")
public interface Sample {

    @XPathMethod("/s:Test/s:someString")
    String getHello();

    @XPathMethod("/s:Test/s:anotherString[@sneaky=$0]")
    String getWorld(String sneaky);

    @XPathMethod("/s:Test/s:booleanExists")
    Boolean booleanExists();

    @XPathMethod("/s:Test/s:doesNotExist")
    Boolean booleanDoesNotExist();

    @XPathMethod("/s:Test/s:booleanTrue")
    Boolean booleanTrueValue();

    @XPathMethod("/s:Test/s:booleanFalse")
    Boolean booleanFalseValue();

    @XPathMethod("/s:Test/s:yes")
    YesNo yesNo();

    @XPathMethod("/s:Test/s:someDouble")
    Double doubleValue();
}

Instantiate the proxy with code like this...

XPathProxyFactory<Sample> factory = new XPathProxyFactory<Sample>(Sample.class);
factory.registerConverter(new EnumConverter(YesNo.class));
Sample proxy = factory.createProxy(requestElement);
System.out.println(proxy.getHello());

The above would print "Hello" as that is the result of the xpath query annotated on that method.

What XPathProxy does is essentially wrap a given DOM Node with an annotated interface for easy access.

Powered by Google Project Hosting