xpath-helper
XPath helpers based on XOM
Maven
<dependency>
<groupId>com.itelg</groupId>
<artifactId>xpath-helper</artifactId>
<version>0.6.0</version>
</dependency>
Supported methods
getNodes() : Nodes
getNodeList() : List<Node>
hasNodes() : boolean
hasNode() : boolean
getFirstNode() : Node
getFirstElement() : Element
getLastNode() : Node
getLastElement() : Element
getString() : String
getNullableString() : String
getDouble() : Double
getPDouble() : double
getInteger() : Integer
getInt() : int
getLong() : Long
getPLong() : long
getBoolean() : Boolean
getBool() : boolean
getZonedDateTime() : ZonedDateTime
getLocalDateTime() : LocalDateTime
getLocalDate() : LocalDate
getEnum() : Enum<E>
AbstractParser-Example
public static void main(String[] args) throws Exception
{
String xml = "<data><stringValue>Test</stringValue>"
+ "<doubleValue>1.12</doubleValue>"
+ "<testEnum>VALUE</testEnum></data>";
TestClass testClass = new TestParser().parse(xml);
System.out.println(testClass);
}
public static class TestParser extends AbstractParser<TestClass>
{
@Override
protected TestClass doParse(Element rootElement) throws Exception
{
TestClass test = new TestClass();
test.stringValue = XPathHelper.getString("stringValue", rootElement);
test.doubleValue = XPathHelper.getDouble("doubleValue", rootElement);
test.testEnum = XPathHelper.getEnum("testEnum", TestClass.TestEnum.class, rootElement);
return test;
}
}
public static class TestClass
{
private String stringValue;
private Double doubleValue;
private TestEnum testEnum;
@Override
public String toString()
{
return "TestClass [stringValue=" + stringValue + ", doubleValue=" + doubleValue + ", testEnum=" + testEnum + "]";
}
private enum TestEnum
{
VALUE;
}
}
Plain-Example
public static void main(String[] args) throws Exception
{
String xml = "<data><stringValue>Test</stringValue>"
+ "<doubleValue>1.12</doubleValue></data>";
Element rootElement = DocumentHelper.getRootElement(xml);
System.out.println(XPathHelper.getString("stringValue", rootElement));
System.out.println(XPathHelper.getDouble("doubleValue", rootElement));
}
Build & Release
Build
mvn clean package
Release
mvn clean deploy