Java Utililty Methods XPath Expression

List of utility methods to do XPath Expression

Description

The list of methods to do XPath Expression are organized into topic(s).

Method

MapcreateReverseCMDIComponentItemMap(String urlToComponent)
Create a mapping out of simple CMDI components for instance: lists of items: ti Will become key (after removal of trailing 2 or 3 letter codes), values: Tigrinya, ti
Map<String, String> result = new HashMap<String, String>();
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
URL url = new URL(urlToComponent);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(url.openStream());
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xpath.evaluate("//item", doc, XPathConstants.NODESET);
...
XPathcreateXPath()
Creates an XPath object with a custom NamespaceContext given the Node to operate on

the Node or document to operate on.

return XPathFactory.newInstance().newXPath();
XPathcreateXPath(NamespaceContext namespaceContext, XPathFunctionResolver functionResolver)
create X Path
XPath xPath = XPathFactory.newInstance().newXPath();
if (namespaceContext != null) {
    xPath.setNamespaceContext(namespaceContext);
if (functionResolver != null) {
    xPath.setXPathFunctionResolver(functionResolver);
return xPath;
...
XPathExpressioncreateXPathExpression(NamespaceContext context, String xPathQuery)
create X Path Expression
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(context);
return xpath.compile(xPathQuery);
XPathExpressioncreateXPathExpression(String expression, NamespaceContext namespaceContext)
create X Path Expression
XPath xpath = getFactory().newXPath();
if (namespaceContext != null) {
    xpath.setNamespaceContext(namespaceContext);
XPathExpression expr = xpath.compile(expression);
return expr;
XPathExpressioncreateXPathExpression(String xpath)
Create an XPathExpression from the supplied xpath string.
if (XPATH == null) {
    XPATH = XPathFactory.newInstance().newXPath();
return XPATH.compile(xpath);
XPathExpressioncreateXPathExpression(String xpathString)
create X Path Expression
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
xpath.setNamespaceContext(new NamespaceContext() {
    @Override
    public Iterator<?> getPrefixes(String namespaceURI) {
        throw new RuntimeException();
    @Override
...
StringdeleteXMLElement(String xml, String xpath)
delete XML Element
try {
    final Node node = findXMLNode(xml, xpath);
    Node parent = node.getParentNode();
    parent.removeChild(node);
    return XMLtoString(parent.getOwnerDocument());
} catch (XPathExpressionException e) {
    return null;
voiddumpXpath(Node node, PrintStream printer)
dump Xpath
NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
    Node item = list.item(i);
    printer.println("Xpath: " + getXPath(item));
    printer.println("Text Content: " + item.getTextContent());
    if (item.hasChildNodes()) {
        dumpXpath(item, printer);
NodeListexecutePath(final Node node, final XPathExpression expression)
execute Path
return (NodeList) expression.evaluate(node, XPathConstants.NODESET);