xpath-utils

Provides some utility-methods to simplify the use of XPath

License

License

Categories

Categories

IDE Development Tools
GroupId

GroupId

com.avides.xpath
ArtifactId

ArtifactId

xpath-utils
Last Version

Last Version

2.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

xpath-utils
Provides some utility-methods to simplify the use of XPath
Project URL

Project URL

https://github.com/avides/xpath-utils
Project Organization

Project Organization

AVIDES Media AG
Source Code Management

Source Code Management

https://github.com/avides/xpath-utils

Download xpath-utils

How to add to project

<!-- https://jarcasting.com/artifacts/com.avides.xpath/xpath-utils/ -->
<dependency>
    <groupId>com.avides.xpath</groupId>
    <artifactId>xpath-utils</artifactId>
    <version>2.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.avides.xpath/xpath-utils/
implementation 'com.avides.xpath:xpath-utils:2.1.0'
// https://jarcasting.com/artifacts/com.avides.xpath/xpath-utils/
implementation ("com.avides.xpath:xpath-utils:2.1.0")
'com.avides.xpath:xpath-utils:jar:2.1.0'
<dependency org="com.avides.xpath" name="xpath-utils" rev="2.1.0">
  <artifact name="xpath-utils" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.avides.xpath', module='xpath-utils', version='2.1.0')
)
libraryDependencies += "com.avides.xpath" % "xpath-utils" % "2.1.0"
[com.avides.xpath/xpath-utils "2.1.0"]

Dependencies

compile (1)

Group / Artifact Type Version
xom : xom jar 1.3.4

provided (1)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.25

test (3)

Group / Artifact Type Version
junit : junit jar 4.12
org.assertj : assertj-core jar 3.14.0
org.jacoco : org.jacoco.agent jar 0.8.5

Project Modules

There are no modules declared in this project.

xpath-utils

Maven Central Build Nightly build Coverage report Quality Gate Technical dept

Maven

<dependency>
    <groupId>com.avides.xpath</groupId>
    <artifactId>xpath-utils</artifactId>
    <version>2.1.0</version>
</dependency>

Available methods

XPathUtils.getRootElement(InputStream inputStream);
XPathUtils.getRootElement(Reader reader);
XPathUtils.getRootElement(File file);
XPathUtils.getRootElement(String xml);

XPathUtils.queryBool(Node root, String xPath);
XPathUtils.queryBoolean(Node root, String xPath);
XPathUtils.queryChar(Node root, String xPath);
XPathUtils.queryCharacter(Node root, String xPath);
XPathUtils.queryDouble(Node root, String xPath);
XPathUtils.queryElementList(Node root, String xPath);
XPathUtils.queryEnum(Node root, String xPath, Class<E> enumClass);
XPathUtils.queryFirst(Node root, String xPath);
XPathUtils.queryFirst(Node root, String xPath, Class<? extends Function<String, T>> converterClass);
XPathUtils.queryFirstElement(Node root, String xPath);
XPathUtils.queryFirstNode(Node root, String xPath);
XPathUtils.queryFloat(Node root, String xPath);
XPathUtils.queryInt(Node root, String xPath);
XPathUtils.queryInteger(Node root, String xPath);
XPathUtils.queryList(Node root, String xPath);
XPathUtils.queryList(Node root, String xPath, Class<? extends Function<String, T>> converterClass);
XPathUtils.queryList(Node root, String xPath, Class<? extends Function<String, T>> converterClass, Class<T> subType);
XPathUtils.queryTypedList(Node root, String xPath, Class<T> subType);
XPathUtils.queryLocalDate(Node root, String xPath);
XPathUtils.queryLocalDateTime(Node root, String xPath);
XPathUtils.queryLocalTime(Node root, String xPath);
XPathUtils.queryLong(Node root, String xPath);
XPathUtils.queryMap(Node root, String, String, String);
XPathUtils.queryMap(Node root, String entryXPath, String keySubXPath, String valueSubXPath, Class<? extends Function<String, K>> keyConverterClass, Class<? extends Function<String, V>> valueConverterClass);
XPathUtils.queryMap(Node root, String entryXPath, String keySubXPath, String valueSubXPath, Class<? extends Function<String, K>> keyConverterClass, Class<? extends Function<String, V>> valueConverterClass, Class<K> keySubType, Class<V> valueSubType)
XPathUtils.queryTypedMap(Node root, String entryXPath, String keySubXPath, String valueSubXPath, Class<K> keySubType, Class<V> valueSubType)
XPathUtils.queryKeyTypedMap(Node root, String entryXPath, String keySubXPath, String valueSubXPath, Class<K> keySubType, Class<? extends Function<String, V>> valueConverterClass)
XPathUtils.queryKeyTypedMap(Node root, String entryXPath, String keySubXPath, String valueSubXPath, Class<K> keySubType)
XPathUtils.queryValueTypedMap(Node root, String entryXPath, String keySubXPath, String valueSubXPath, Class<? extends Function<String, K>> keyConverterClass, Class<V> valueSubType)
XPathUtils.queryValueTypedMap(Node root, String entryXPath, String keySubXPath, String valueSubXPath, Class<V> valueSubType)
XPathUtils.queryNodeList(Node root, String xPath);
XPathUtils.queryNodeMap(Node root, String entryXPath, String keySubXPath, String valueSubXPath)
XPathUtils.queryNodes(Node root, String xPath);
XPathUtils.queryPrimitiveBoolean(Node root, String xPath);
XPathUtils.queryPrimitiveCharacter(Node root, String xPath);
XPathUtils.queryPrimitiveDouble(Node root, String xPath);
XPathUtils.queryPrimitiveFloat(Node root, String xPath);
XPathUtils.queryPrimitiveInteger(Node root, String xPath);
XPathUtils.queryPrimitiveLong(Node root, String xPath);
XPathUtils.queryPrimitiveShort(Node root, String xPath);
XPathUtils.queryShort(Node root, String xPath);
XPathUtils.queryZonedDateTime(Node root, String xPath);

XPathUtils.hasNode(Node root, String xPath);
XPathUtils.hasNodes(Nodes nodes);

XPathUtils.each(Node root, String xPath);

// marshalling-methods:
XPathUtils.fromRoot(Element root, Class<T> type);
XPathUtils.fromInputStream(InputStream inputStream, Class<T> type);
XPathUtils.fromReader(Reader reader, Class<T> type);
XPathUtils.fromFile(File file, Class<T> type);
XPathUtils.fromXml(String xml, Class<T> type);

XPathUtils.registerConverterInstance(Function<String, T> converter);
XPathUtils.unregisterConverterInstance(Class<? extends Function<String, ?>> converterClass)
XPathUtils.clearConverterInstances()

XPathUtils.registerDefaultConverterInstanceToType(Class<?> type, Function<String, ?> converter)
XPathUtils.unregisterDefaultConverterInstanceToType(Class<?> type)
XPathUtils.resetDefaultConverterInstancesToType()

Examples

<xml>
    <anyStringValue>anyValue</anyStringValue>
    <anyIntegerValue>123</anyIntegerValue>
    <stringList>
        <value>listValue1</value>
        <value>listValue2</value>
        <value>listValue3</value>
    </stringList>
    <integerList>
        <value>10</value>
        <value>11</value>
        <value>12</value>
    </integerList>
    <stringMap>
        <entry>
            <string>key1</string>
            <string>value1</string>
        </entry>
        <entry>
            <string>key2</string>
            <string>value2</string>
        </entry>
    </stringMap>
    <integerMap>
        <entry>
            <string>key1</string>
            <string>100</string>
        </entry>
        <entry>
            <string>key2</string>
            <string>200</string>
        </entry>
    </integerMap>
</xml>
Simple example
Element root = XPathUtils.getRootElementFromXml(xml);

String stringValue = XPathUtils.queryFirst(root, "anyStringValue");
Integer integerValue = XPathUtils.queryFirst(root, "anyIntegerValue", ToIntegerConverter.class);
List<String> stringList = XPathUtils.queryList(root, "stringList/value");
List<Integer> integerList = XPathUtils.queryList(root, "integerList/value");
Map<String, String> stringMap = XPathUtils.queryMap(root, "stringMap/entry", "string[1]", "string[2]");
Map<String, Integer> integerMap = XPathUtils.queryMap(root, "integerMap/entry", "string[1]", "string[2]", NoneConverter.class, ToIntegerConverter.class);

// should output 'anyValue - 123':
System.out.println(stringValue + " - " + integerValue);

// should output '[value1, value2, value3]':
System.out.println(stringList);

// should output '[10, 11, 12]':
System.out.println(integerList);

// should output '{key1=value1, key2=value2}':
System.out.println(stringMap);

// should output '{key1=100, key2=200}':
System.out.println(integerMap);
Simple marshalling-example
class AnyObject
{
    @XPathFirst("anyStringValue")
    String stringValue;

    // All known default-converters are already registered for conversion
    // to default-field-types, so the converterClass could be left here,
    // but is set for illustration:
    @XPathFirst(value = "anyIntegerValue", converterClass = ToIntegerConverter.class)
    Integer integerValue;

    @XPathList("stringList/value")
    List<String> stringList;

    @XPathList(value = "integerList/value", converterClass = ToIntegerConverter.class)
    List<Integer> integerList;

    @XPathMap(entryXPath = "stringMap/entry", keySubXPath = "string[1]", valueSubXPath = "string[2]")
    Map<String, String> stringMap;

    @XPathMap(entryXPath = "integerMap/entry", keySubXPath = "string[1]", valueSubXPath = "string[2]", valueConverterClass = ToIntegerConverter.class)
    Map<String, Integer> integerMap;
}

AnyObject anyObject = XPathUtils.fromXml(xml, AnyObject.class);

// should output 'anyValue - 123':
System.out.println(anyObject.stringValue + " - " + anyObject.integerValue);

// should output '[value1, value2, value3]':
System.out.println(anyObject.stringList);

// should output '[10, 11, 12]':
System.out.println(anyObject.integerList);

// should output '{key1=value1, key2=value2}':
System.out.println(anyObject.stringMap);

// should output '{key1=100, key2=200}':
System.out.println(anyObject.integerMap);

Using Converter with arguments

ToLocalDateConverter converter = new ToLocalDateConverter(DateTimeFormatter.ofPattern("dd.MM.yyyy"));
XPathUtils.registerConverterInstance(converter);

Register a default-converter for a special field-type

ToLocalDateConverter converter = new ToLocalDateConverter(DateTimeFormatter.ofPattern("dd.MM.yyyy"));
XPathUrils.registerDefaultConverterInstanceToType(LocalDate.class, converter);
com.avides.xpath

AVIDES Media AG

E-Commerce Solutions

Versions

Version
2.1.0
2.0.0
1.1.4.RELEASE
1.1.3.RELEASE
1.1.2.RELEASE