set XML Attribute by xpath - Java XML

Java examples for XML:XPath

Description

set XML Attribute by xpath

Demo Code


import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main{
    //from w  ww .  j a  va2  s  .  c om
    public static void setAttribute(Document doc, String nodeXpath,
            String attrName, String attrValue) {
        Node node = findNodeByXpath(doc, nodeXpath);
        if (node != null && isNodeTypeMatches(node, NodeType.ELEMENT_NODE)) {
            Element element = (Element) node;
            element.setAttribute(attrName, attrValue);
        }
    }
    public static Node findNodeByXpath(Document doc, String xpath) {
        NodeList nodelist = findNodeListByXpath(doc, xpath);
        if (nodelist.getLength() > 0) {
            return nodelist.item(0);
        }
        return null;
    }
    public static boolean isNodeTypeMatches(Node node, NodeType nodeType) {
        return ((int) node.getNodeType()) == nodeType.getValue();
    }
    public static NodeList findNodeListByXpath(Document doc, String xpath) {
        XPathFactory pathFactory = XPathFactory.newInstance();
        //??XPathFactory???? XPath ??  
        XPath x_path = pathFactory.newXPath();
        //??XPath????XPath????  
        Object obj = null;
        try {
            XPathExpression pathExpression = x_path.compile(xpath);
            obj = pathExpression.evaluate(doc, XPathConstants.NODESET);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException("Error while xpath search");
        }
        NodeList nodelist = (NodeList) obj;

        return nodelist;
    }
    public static List<Node> findNodeListByXpath(Document doc,
            String xpath, NodeType nodeType) {
        XPathFactory pathFactory = XPathFactory.newInstance();
        //??XPathFactory???? XPath ??  
        XPath x_path = pathFactory.newXPath();
        //??XPath????XPath????  
        Object obj = null;
        try {
            XPathExpression pathExpression = x_path.compile(xpath);
            obj = pathExpression.evaluate(doc, XPathConstants.NODESET);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException("Error while xpath search");
        }
        NodeList nodelist = (NodeList) obj;

        List<Node> nlist = new ArrayList<Node>();
        for (int i = 0; i < nodelist.getLength(); i++) {
            Node n = nodelist.item(i);
            if (isNodeTypeMatches(n, nodeType)) {
                nlist.add(n);
            }
        }

        return nlist;
    }
    public static NodeType getNodeType(Node node) {
        switch (node.getNodeType()) {
        case Node.ATTRIBUTE_NODE:
            return NodeType.ATTRIBUTE_NODE;

        case Node.ELEMENT_NODE:
            return NodeType.ELEMENT_NODE;

        case Node.TEXT_NODE:
            return NodeType.TEXT_NODE;

        case Node.CDATA_SECTION_NODE:
            return NodeType.CDATA_SECTION_NODE;

        case Node.ENTITY_REFERENCE_NODE:
            return NodeType.ENTITY_REFERENCE_NODE;

        case Node.ENTITY_NODE:
            return NodeType.ENTITY_NODE;

        case Node.DOCUMENT_NODE:
            return NodeType.DOCUMENT_NODE;

        case Node.DOCUMENT_TYPE_NODE:
            return NodeType.DOCUMENT_TYPE_NODE;

        case Node.DOCUMENT_FRAGMENT_NODE:
            return NodeType.DOCUMENT_FRAGMENT_NODE;

        case Node.NOTATION_NODE:
            return NodeType.NOTATION_NODE;

        case Node.PROCESSING_INSTRUCTION_NODE:
            return NodeType.PROCESSING_INSTRUCTION_NODE;

        case Node.COMMENT_NODE:
            return NodeType.COMMENT_NODE;

        default:
            break;
        }

        throw new RuntimeException("No NodeType detected");
    }
}

Related Tutorials