diff options
| author | Andrew Branson <andrew.branson@cern.ch> | 2014-03-06 16:28:55 +0100 |
|---|---|---|
| committer | Andrew Branson <andrew.branson@cern.ch> | 2014-03-06 16:28:55 +0100 |
| commit | 696ec0bc6f005ea75db10cff9eded999456e47f2 (patch) | |
| tree | a52e4bddd96128456e4d9e4b1688e96c7c6df225 | |
| parent | 7e98470f8acd69fa885286ccd6efd5769bad1754 (diff) | |
New methods in Outcome to support XPath queries to make extraction of
XML data in scripts easier. Fixes #167
| -rw-r--r-- | src/main/java/com/c2kernel/persistency/outcome/Outcome.java | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/src/main/java/com/c2kernel/persistency/outcome/Outcome.java b/src/main/java/com/c2kernel/persistency/outcome/Outcome.java index b2f706b..f25922c 100644 --- a/src/main/java/com/c2kernel/persistency/outcome/Outcome.java +++ b/src/main/java/com/c2kernel/persistency/outcome/Outcome.java @@ -5,6 +5,11 @@ import java.util.StringTokenizer; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
@@ -29,6 +34,7 @@ public class Outcome implements C2KLocalObject { Document dom;
static DocumentBuilder parser;
static DOMImplementationLS impl;
+ static XPath xpath;
static {
// Set up parser
@@ -50,7 +56,9 @@ public class Outcome implements C2KLocalObject { Logger.error(e);
Logger.die("Cannot function without XML serialiser");
}
-
+
+ XPathFactory xPathFactory = XPathFactory.newInstance();
+ xpath = xPathFactory.newXPath();
}
//id is the eventID
@@ -169,6 +177,20 @@ public class Outcome implements C2KLocalObject { else
return null;
}
+
+ public String getFieldByXPath(String xpathExpr) throws XPathExpressionException {
+
+ XPathExpression expr = xpath.compile(xpathExpr);
+ return (String)expr.evaluate(getDOM(), XPathConstants.STRING);
+
+ }
+
+ public NodeList getNodesByXPath(String xpathExpr) throws XPathExpressionException {
+
+ XPathExpression expr = xpath.compile(xpathExpr);
+ return (NodeList)expr.evaluate(getDOM(), XPathConstants.NODESET);
+
+ }
static public String serialize(Document doc, boolean prettyPrint)
{
|
