Example usage for javax.xml.xpath XPathConstants NODE

List of usage examples for javax.xml.xpath XPathConstants NODE

Introduction

In this page you can find the example usage for javax.xml.xpath XPathConstants NODE.

Prototype

QName NODE

To view the source code for javax.xml.xpath XPathConstants NODE.

Click Source Link

Document

The XPath 1.0 NodeSet data type.

Usage

From source file:Main.java

public static Node getNodeByPath(Node node, String path) {
    XPathExpression expr = compile(path);
    try {/* w  w w.  j  ava2  s.com*/
        return (Node) expr.evaluate(node, XPathConstants.NODE);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

private static Node getNodeObject(String xpathString, Document doc) throws XPathExpressionException {
    // Create a xptah object
    //logger.debug("Getting the node by using xpath " + xpathString);
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    XPathExpression expr = xpath.compile(xpathString);
    Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
    //logger.debug("Returning the Node object from xml file");
    return node;//w  ww  . j av a2 s.c  o m
}

From source file:Main.java

/**
 * Evaluate XPath expression./*w w  w.j ava  2 s  . com*/
 *
 * @param expression
 *            XPath expression
 * @param node
 *            DOM node
 * @param returnValue
 *            the desired return type
 * @return result of evaluating an XPath expression as an Object of returnType
 * @throws XPathExpressionException
 *             if expression cannot be evaluated
 */
public static Object evaluateXPathExpression(final String expression, final Node node, final QName returnValue)
        throws XPathExpressionException {
    return getNewXPath().evaluate(expression, node, returnValue == null ? XPathConstants.NODE : returnValue);
}

From source file:Main.java

public static Node getNode(String xPathExpression, Node node) throws XPathExpressionException {
    return (Node) xPath.evaluate(xPathExpression, node, XPathConstants.NODE);
}

From source file:Main.java

public static Element getElement(String xPathExpression, Node node) throws XPathExpressionException {
    return (Element) xPath.evaluate(xPathExpression, node, XPathConstants.NODE);
}

From source file:simple.crawler.ext.vanilla.VanillaForumUtil.java

public static HttpContext login(String loginURL, String username, String password) throws Exception {
    DefaultHttpClient httpclient = HttpClientFactory.getInstance();
    CookieStore cookieStore = new BasicCookieStore();
    HttpContext httpContext = new BasicHttpContext();
    httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    HttpResponse res = httpclient.execute(new HttpGet(loginURL), httpContext);

    String html = HttpClientUtil.getContentBodyAsString(res);
    HtmlParser parser = new HtmlParser();
    Document doc = parser.parseNonWellForm(html);

    ////from   w  w w  .  jav  a 2s. co m
    Node loginTransientKey = (Node) XPathUtil.read(doc, "//*[@id=\"Form_TransientKey\"]", XPathConstants.NODE);
    Node hpt = (Node) XPathUtil.read(doc, "//*[@id=\"Form_hpt\"]", XPathConstants.NODE);
    Node target = (Node) XPathUtil.read(doc, "//*[@id=\"Form_Target\"]", XPathConstants.NODE);
    Node clientHour = (Node) XPathUtil.read(doc, "//*[@id=\"Form_ClientHour\"]", XPathConstants.NODE);

    //
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    list.add(new BasicNameValuePair("Form/TransientKey", ((Element) loginTransientKey).getAttribute("value")));
    list.add(new BasicNameValuePair("Form/hpt", ((Element) hpt).getAttribute("value")));
    list.add(new BasicNameValuePair("Form/Target", ((Element) target).getAttribute("value")));
    list.add(new BasicNameValuePair("Form/ClientHour", ((Element) clientHour).getAttribute("value")));
    list.add(new BasicNameValuePair("Form/Email", "admin"));
    list.add(new BasicNameValuePair("Form/Password", "admin"));
    list.add(new BasicNameValuePair("Form/Sign_In", "Sign In"));
    list.add(new BasicNameValuePair("Form/RememberMe", "1"));
    list.add(new BasicNameValuePair("Checkboxes[]", "RememberMe"));

    HttpPost post = new HttpPost(loginURL);
    post.setEntity(new UrlEncodedFormEntity(list));
    res = httpclient.execute(post, httpContext);
    return httpContext;
}

From source file:com.thinkbiganalytics.feedmgr.nifi.NifiTemplateParser.java

public static String updateTemplateName(String nifiTemplate, String newName) throws Exception {

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    Document doc = stringToDocument(nifiTemplate);

    InputSource source = new InputSource(new StringReader(nifiTemplate));

    Element element = (Element) xpath.compile("//template/name").evaluate(doc, XPathConstants.NODE);

    element.setTextContent(newName);//from  w  ww  .j  a v a  2  s  .  co  m
    return documentToString(doc);
}

From source file:com.cloudseal.spring.client.namespace.Utility.java

public static void removeNode(final Element rootElement, final String xPathLocation)
        throws XPathExpressionException {
    final XPath xPath = XPathFactory.newInstance().newXPath();
    xPath.setNamespaceContext(new CloudSealNamespaceContext());
    final Node node = (Node) xPath.evaluate(xPathLocation, rootElement, XPathConstants.NODE);
    final short nodeType = node.getNodeType();

    switch (nodeType) {
    case Node.ELEMENT_NODE:
        final Node parent = node.getParentNode();
        parent.removeChild(node);/*  w  ww .j  a  v a 2 s  .  c o  m*/
        break;

    case Node.ATTRIBUTE_NODE:
        final Attr attribute = (Attr) node;
        final Element element = attribute.getOwnerElement();
        element.removeAttributeNode(attribute);
        break;

    default:
        throw new IllegalArgumentException("Not supported node type: " + nodeType);
    }
}

From source file:au.edu.rmit.GalagoSearchClient.java

protected Node xpathGetNode(Document doc, String expr)
        throws XPathExpressionException, UnsupportedEncodingException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    return (Node) xpath.compile(expr).evaluate(doc, XPathConstants.NODE);
}

From source file:Main.java

/** Evaluates an XPath returning null if not found. <br>
 *  <br>/* www  .  j  av a 2  s . c  o  m*/
 *  This is intended for use with pre-defined XPaths stored as constants,
 *  where runtime exceptions should not be possible.
 *  @param expression The XPath expression to evaluate.
 *  @param item       The {@link Node} or other item to evaluate the XPath on.
 *  @param type       The type to return, this must be one of the following:
 *                    {@link String}, {@link CharSequence}, {@link Boolean},
 *                    {@link Node}, {@link NodeList}, {@link Double}, or
 *                    {@link Number}.
 *  @throws AssertionError If the nested call to <tt>XPath.newInstance(path)</tt>
 *                         throws an {@link XPathExpressionException}.
 */
public static <T> T evalXPath(String expression, Object item, Class<T> type) {
    Object val;

    if (type == String.class)
        val = evalXPath(expression, item, XPathConstants.STRING);
    else if (type == CharSequence.class)
        val = evalXPath(expression, item, XPathConstants.STRING);
    else if (type == Boolean.class)
        val = evalXPath(expression, item, XPathConstants.BOOLEAN);
    else if (type == Boolean.TYPE)
        val = evalXPath(expression, item, XPathConstants.BOOLEAN);
    else if (type == Node.class)
        val = evalXPath(expression, item, XPathConstants.NODE);
    else if (type == NodeList.class)
        val = evalXPath(expression, item, XPathConstants.NODESET);
    else if (type == Double.class)
        val = evalXPath(expression, item, XPathConstants.NUMBER);
    else if (type == Double.TYPE)
        val = evalXPath(expression, item, XPathConstants.NUMBER);
    else if (type == Number.class)
        val = evalXPath(expression, item, XPathConstants.NUMBER);
    else
        throw new IllegalArgumentException("Invalid type given " + type);

    return type.cast(val);
}