commit f974215e759a3ecf9d5b65a3f396696aa03e548c
parent ed631099da00ece0d1ddf058f06d753200748470
Author: Marcello Stanisci <stanisci.m@gmail.com>
Date: Thu, 26 Mar 2020 09:17:13 +0100
String-picking XPath helper.
Diffstat:
2 files changed, 12 insertions(+), 6 deletions(-)
diff --git a/nexus/src/test/kotlin/XPathTest.kt b/nexus/src/test/kotlin/XPathTest.kt
@@ -15,8 +15,11 @@ class XPathTest {
<node>lorem ipsum</node>
</root>""".trimIndent()
val doc: Document = XMLUtil.parseStringIntoDom(xml)
- val node = XMLUtil.evalXpath(doc, "/*[local-name()='root']")
+ val node = XMLUtil.getNodeFromXpath(doc, "/*[local-name()='root']")
assert(node != null)
+ val text = XMLUtil.getStringFromXpath(doc, "//*[local-name()='node']")
+ assert(text != null)
+ println(text)
}
}
diff --git a/util/src/main/kotlin/XMLUtil.kt b/util/src/main/kotlin/XMLUtil.kt
@@ -90,9 +90,7 @@ class XMLUtil private constructor() {
throw Exception("invalid EBICS XML signature URI: '${myRef.uri}'")
val xp: XPath = XPathFactory.newInstance().newXPath()
val nodeSet = xp.compile("//*[@authenticate='true']/descendant-or-self::node()").evaluate(
- myRef.here
- .ownerDocument, XPathConstants
- .NODESET
+ myRef.here.ownerDocument, XPathConstants.NODESET
)
if (nodeSet !is NodeList)
throw Exception("invalid type")
@@ -409,9 +407,14 @@ class XMLUtil private constructor() {
return valResult
}
- fun evalXpath(doc: Document, query: String): Node? {
+ fun getNodeFromXpath(doc: Document, query: String): Node? {
val xpath = XPathFactory.newInstance().newXPath()
- return xpath.evaluate(query, doc, XPathConstants.NODE) as Node
+ return xpath.evaluate(query, doc, XPathConstants.NODE) as Node?
+ }
+
+ fun getStringFromXpath(doc: Document, query: String): String? {
+ val xpath = XPathFactory.newInstance().newXPath()
+ return xpath.evaluate(query, doc, XPathConstants.STRING) as String?
}
}
}