Example usage for javax.xml.xpath XPathExpressionException printStackTrace

List of usage examples for javax.xml.xpath XPathExpressionException printStackTrace

Introduction

In this page you can find the example usage for javax.xml.xpath XPathExpressionException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Print stack trace to System.err .

Usage

From source file:Main.java

public static Element querySingle(Element element, String query) {
    try {//from   w ww . j av  a2  s.c  om
        XPathExpression expr = xpath.newXPath().compile(query);
        NodeList list = (NodeList) expr.evaluate(element, XPathConstants.NODESET);

        if (list.getLength() > 0)
            return (Element) list.item(0);

    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

static public List<Element> elements(Node context, String expression) {
    try {//from w w  w .j ava 2s. c  o  m
        NodeList nodeList = (NodeList) xpath.evaluate(expression, context, XPathConstants.NODESET);
        List<Element> result = new LinkedList<Element>();
        for (int i = 0, len = nodeList.getLength(); i < len; i++) {
            result.add((Element) nodeList.item(i));
        }
        return result;
    } catch (XPathExpressionException ex) {
        ex.printStackTrace();
        throw new RuntimeException("invalid xpath expresion used");
    }
}

From source file:Main.java

/**
 * Return a list of elements based on xPath
 *///from   ww w.java2 s.  co m
static NodeList getElementsByAttribute(Document doc, String xPath) {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = null;
    NodeList nl = null;
    try {
        expr = xpath.compile(xPath);
        nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return nl;
}

From source file:Main.java

/**
 * Return one element based on xPath// w w  w  .ja  v  a 2s  .c o  m
 */
static Element getElementByAttribute(Document doc, String xPath) {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = null;
    NodeList nl = null;
    try {
        expr = xpath.compile(xPath);
        nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return (Element) nl.item(0);
}

From source file:Main.java

public static ArrayList<Element> getElementByXPath(Element el, String name) {
    try {/*from   w w w  . ja v  a 2  s  .  c  om*/
        NodeList nl = (NodeList) XPathFactory.newInstance().newXPath().evaluate(name, el,
                XPathConstants.NODESET);
        return nodelistToElement(nl);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

static public String string(Node context, String expression) {
    try {//  w  w  w. j  a  va  2s .co  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");
    }
}

From source file:Main.java

static public String string(Node context, String expression, String defaultValue) {
    try {/*from w w  w. j a  va 2  s.co m*/
        String result = (String) xpath.evaluate(expression, context, XPathConstants.STRING);
        if (result == null || result.length() == 0)
            return defaultValue;
        else
            return result;
    } catch (XPathExpressionException ex) {
        ex.printStackTrace();
        throw new RuntimeException("invalid xpath expresion used");
    }
}

From source file:Main.java

public static Node selectSingleNode(String express, Object source) {
    Node result = null;//from   w ww .  ja v a 2 s.  c  o  m
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    try {
        result = (Node) xpath.evaluate(express, source, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static NodeList selectNodes(String express, Object source) {
    NodeList result = null;/*w  ww  .  j a  v  a  2s  . c  o  m*/
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    try {
        result = (NodeList) xpath.evaluate(express, source, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static String getValueAsString(String filePath, String value)
        throws ParserConfigurationException, SAXException, IOException {
    String id = null;//from  www .  j  ava  2s . c  om
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    try {
        XPathExpression expr = xpath.compile(value);
        id = (String) expr.evaluate(getDoc(filePath), XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return id;
}