List of usage examples for javax.xml.xpath XPathConstants STRING
QName STRING
To view the source code for javax.xml.xpath XPathConstants STRING.
Click Source Link
The XPath 1.0 string data type.
Maps to Java String .
From source file:Main.java
public static void main(String[] args) throws Exception { XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); String xml1 = "<xml><product><product_id>2</product_id></product><car><product_id>12345678</product_id></car></xml>"; String xml2 = "<xml><product><product_id>2</product_id></product><car><car_type id_1=\"2\" id_2=\"32\">55555</car_type></car></xml>"; Document doc1 = stringToDom(xml1); Document doc2 = stringToDom(xml2); XPathExpression expr1 = xpath.compile("//car/product_id/text()"); String carId = (String) expr1.evaluate(doc1, XPathConstants.STRING); XPathExpression expr2 = xpath.compile("//car/car_type/text()"); String carType = (String) expr2.evaluate(doc2, XPathConstants.STRING); System.out.println("carId: " + carId); System.out.println("carType: " + carType); }
From source file:Main.java
public static void main(String[] args) throws Throwable { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); String entryType = null;//from ww w . jav a 2 s .c o m XPathExpression[] expressions = new XPathExpression[] { xpath.compile("local-name(*[local-name() = 'package'])"), xpath.compile("local-name(*[local-name() = 'libapp'])"), xpath.compile("local-name(*[local-name() = 'module'])") }; DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = fac.newDocumentBuilder(); Document doc = parser.parse(args[0]); for (int i = 0; i < expressions.length; i++) { String found = (String) expressions[i].evaluate(doc.getDocumentElement(), XPathConstants.STRING); entryType = mappings.get(found); if (entryType != null && !entryType.trim().isEmpty()) { break; } } System.out.println(entryType); }
From source file:ApplyXPathJAXP.java
public static void main(String[] args) { QName returnType = null;/*from w ww . j a v a2s . com*/ if (args.length != 3) { System.err.println("Usage: java ApplyXPathAPI xml_file xpath_expression type"); } InputSource xml = new InputSource(args[0]); String expr = args[1]; // set the return type if (args[2].equals("num")) returnType = XPathConstants.NUMBER; else if (args[2].equals("bool")) returnType = XPathConstants.BOOLEAN; else if (args[2].equals("str")) returnType = XPathConstants.STRING; else if (args[2].equals("node")) returnType = XPathConstants.NODE; else if (args[2].equals("nodeset")) returnType = XPathConstants.NODESET; else System.err.println("Invalid return type: " + args[2]); // Create a new XPath XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); Object result = null; try { // compile the XPath expression XPathExpression xpathExpr = xpath.compile(expr); // Evaluate the XPath expression against the input document result = xpathExpr.evaluate(xml, returnType); // Print the result to System.out. printResult(result); } catch (Exception e) { e.printStackTrace(); } }
From source file:ExtensionFunctionResolver.java
public static void main(String[] args) throws Exception { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); // set the NamespaceContext to // org.apache.xalan.extensions.ExtensionNamespaceContext xpath.setNamespaceContext(new ExtensionNamespaceContext()); // set the XPathFunctionResolver to // org.apache.xalan.extensions.XPathFunctionResolverImpl xpath.setXPathFunctionResolver(new XPathFunctionResolverImpl()); Object result = null;/*w ww .j ava2 s . c om*/ // Evaluate the XPath expression "math:max(/doc/num)" against // the input document numlist.xml InputSource context = new InputSource("numlist.xml"); result = xpath.evaluate(EXPR1, context, XPathConstants.NUMBER); System.out.println(EXPR1 + " = " + result); // Evaluate the XPath expression "java:ExtensionTest.test('Bob')" result = xpath.evaluate(EXPR2, context, XPathConstants.STRING); System.out.println(EXPR2 + " = " + result); }
From source file:Main.java
public static String selectString(String xpath, Object node) { return ((String) evaluateXPath(xpath, node, XPathConstants.STRING)).trim(); }
From source file:Main.java
public static String findString(Node node, XPathExpression name) throws XPathExpressionException { return (String) name.evaluate(node, XPathConstants.STRING); }
From source file:Main.java
public static String selectString(String xpath, Object node) { try {/*from www . java 2 s. co m*/ return ((String) getXPath(xpath).evaluate(node, XPathConstants.STRING)).trim(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String evalXPathString(String xpath, InputSource source) throws XPathExpressionException { return (String) newXPath(xpath).evaluate(source, XPathConstants.STRING); }
From source file:Main.java
public static String getXmlNodeValue(Document doc, String nodePath) throws Exception { XPath xpath = XPathFactory.newInstance().newXPath(); String nodeValue = (String) xpath.evaluate(nodePath, doc, XPathConstants.STRING); return nodeValue; }
From source file:Main.java
static public String string(Node context, String expression) { try {/*from ww w . j a v a 2 s . c o m*/ String result = (String) xpath.evaluate(expression, context, XPathConstants.STRING); if (result == null || result.length() == 0) return null; else return result; } catch (XPathExpressionException ex) { ex.printStackTrace(); throw new RuntimeException("invalid xpath expresion used"); } }