commit 6f1d7f31a73848a9dfb762b6a2fe408e3024e407
parent f3ad293d57dab27868a83ab09e670ed8e75072ac
Author: Marcello Stanisci <stanisci.m@gmail.com>
Date: Thu, 26 Mar 2020 08:32:03 +0100
Simple XPath test case.
Diffstat:
2 files changed, 25 insertions(+), 2 deletions(-)
diff --git a/nexus/src/test/kotlin/XPathTest.kt b/nexus/src/test/kotlin/XPathTest.kt
@@ -0,0 +1,22 @@
+package tech.libeufin.nexus
+
+import org.junit.Test
+import org.w3c.dom.Document
+import tech.libeufin.util.XMLUtil
+
+
+class XPathTest {
+
+ @Test
+ fun pickDataFromSimpleXml() {
+ val xml = """
+ <root>
+ <node>lorem ipsum</node>
+ </root>""".trimIndent()
+ val doc: Document = tech.libeufin.util.XMLUtil.parseStringIntoDom(xml)
+ val nodeSlashes = XMLUtil.evalXpath(doc, "/root/node/text()")
+ println(nodeSlashes?.nodeValue)
+ val nodeDoubleSlashes = XMLUtil.evalXpath(doc, "//node/text()")
+ println(nodeDoubleSlashes?.nodeValue)
+ }
+}
+\ No newline at end of file
diff --git a/util/src/main/kotlin/XMLUtil.kt b/util/src/main/kotlin/XMLUtil.kt
@@ -409,9 +409,9 @@ class XMLUtil private constructor() {
return valResult
}
- fun getStringViaXpath(doc: Document, query: String): String {
+ fun evalXpath(doc: Document, query: String): Node? {
val xpath = XPathFactory.newInstance().newXPath()
- return xpath.compile(query).evaluate(doc, XPathConstants.STRING).toString()
+ return xpath.evaluate(query, doc, XPathConstants.NODE) as Node
}
}
}