Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Node;

public class Main {
    private static final String XPATH_EVAL_ID = "//*[@id='%s']";

    public static synchronized boolean deleteNode(String charCode, Node root) {
        try {
            XPath xpath = XPathFactory.newInstance().newXPath();
            Node node = (Node) xpath.evaluate(String.format(XPATH_EVAL_ID, charCode), root, XPathConstants.NODE);
            if (node != null) { // remove existing node
                Node parent = node.getParentNode();
                parent.removeChild(node);
                return true;
            }
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        return false;
    }
}