Java XML Element Get getElementsByXpath(Document docInput, String xPath)

Here you can find the source of getElementsByXpath(Document docInput, String xPath)

Description

get Elements By Xpath

License

Apache License

Declaration

public static NodeList getElementsByXpath(Document docInput, String xPath) throws XPathExpressionException 

Method Source Code

//package com.java2s;
/**//from  ww w.ja v a2  s .  co  m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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;

public class Main {
    public static NodeList getElementsByXpath(Document docInput, String xPath) throws XPathExpressionException {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile(xPath);
        return ((NodeList) expr.evaluate(docInput, XPathConstants.NODESET));
    }
}

Related

  1. getElementsByTagName(Element element, String tagName)
  2. getElementsByTagName(Element node, String tagName)
  3. getElementsByTagName(Element node, String xmlns, String nodeName)
  4. getElementsByTagNameNS1(Element element, String nsName, String tagName)
  5. getElementsByTagNameNS1(Element element, String nsName, String tagName)
  6. getElementsWithName(Document doc, String name)
  7. getElementText(Document owner, String elementName, String nsURI)
  8. getElementWithKeyFromDocument(final Document document, final String key)