Example usage for org.w3c.dom Node getNodeName

List of usage examples for org.w3c.dom Node getNodeName

Introduction

In this page you can find the example usage for org.w3c.dom Node getNodeName.

Prototype

public String getNodeName();

Source Link

Document

The name of this node, depending on its type; see the table above.

Usage

From source file:Main.java

public static Node findNode(Node node, String nodeName) {
    if (node == null) {
        return null;
    }/*from  ww  w  .j a  v  a  2s.c  om*/

    if (node.getNodeName().equals(nodeName)) {
        return node;
    }
    NodeList nodeList = node.getChildNodes();
    int i = 0;
    for (int cnt = nodeList.getLength(); i < cnt; i++) {
        Node child = findNode(nodeList.item(i), nodeName);
        if (child != null) {
            return child;
        }
    }
    return null;
}

From source file:Main.java

public static String getFullPath(Node node) {
    StringBuilder buffer = new StringBuilder();
    while (node != null) {
        buffer.insert(0, node.getNodeName());
        char separator = '/';
        if (node instanceof Attr) {
            separator = '@';
        }/*from  ww w.j a  v a 2  s  . co  m*/
        buffer.insert(0, separator);
        node = node.getParentNode();
    }
    return buffer.toString();
}

From source file:Main.java

public static Element getChild(Element node, String tagName) {
    Element element = null;//from   w  ww  .  j a  va2  s  . c o  m

    do {
        if (node == null) {
            break;
        }

        NodeList childNodes = node.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);

            if (child.getNodeType() == Node.ELEMENT_NODE) {
                if (child.getNodeName().equals(tagName)) {
                    element = (Element) child;
                    break;
                }
            }
        }
    } while (false);

    return element;
}

From source file:Main.java

public static Element findNextElementWithAttribute(Node ret, String name, String attribute, String value) {
    ret = ret.getNextSibling();//from w  w w  . j  a v a  2s . c o  m
    while (ret != null && (!(ret instanceof Element) || !ret.getNodeName().equals(name)
            || ((Element) ret).getAttribute(attribute) == null
            || !((Element) ret).getAttribute(attribute).equals(value))) {
        ret = ret.getNextSibling();
    }
    return (Element) ret;
}

From source file:Main.java

static public ArrayList<Element> selectElements(Element element, String xpathExpression)
        throws XPathExpressionException {
    ArrayList<Element> resultVector = new ArrayList<Element>();
    if (element == null) {
        return resultVector;
    }/*from   w ww.ja va 2 s .co  m*/
    if (xpathExpression.indexOf("/") == -1) {
        NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(xpathExpression)) {
                resultVector.add((Element) node);
            }
        }
    } else {
        XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList nodes = (NodeList) xpath.evaluate(xpathExpression, element, XPathConstants.NODESET);

        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            resultVector.add((Element) node);
        }
    }
    return resultVector;
}

From source file:Main.java

public static <T> T loadBean(Node node, Class<T> beanClass)
        throws IntrospectionException, InstantiationException, IllegalAccessException, IllegalArgumentException,
        DOMException, InvocationTargetException {
    T store = beanClass.newInstance();// w w  w  .  j  a va 2 s  .c  o  m

    Map<String, PropertyDescriptor> properties = new HashMap<String, PropertyDescriptor>();
    BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
    for (PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
        properties.put(property.getName(), property);
    }

    NamedNodeMap attributes = node.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node attribute = attributes.item(i);
        PropertyDescriptor property = properties.get(attribute.getNodeName());
        if (property != null && property.getPropertyType().equals(String.class)
                && property.getWriteMethod() != null) {
            property.getWriteMethod().invoke(store, attribute.getNodeValue());
        }
    }

    NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        PropertyDescriptor property = properties.get(child.getNodeName());
        if (property != null && property.getPropertyType().equals(String.class)
                && property.getWriteMethod() != null) {
            property.getWriteMethod().invoke(store, child.getTextContent());
        }
    }

    return store;
}

From source file:Main.java

public static Element getFirstChildElement(Element elm, String name) {
    if (elm == null)
        return null;

    NodeList nl = elm.getChildNodes();
    for (int c = 0; c < nl.getLength(); c++) {
        Node node = nl.item(c);
        if (node.getNodeType() == Node.ELEMENT_NODE && (name == null || node.getNodeName().equals(name)))
            return (Element) node;
    }//from www .j av a  2 s .c om

    return null;
}

From source file:Main.java

private static String getXPath(Node node, String xpath) {
    String elementName = "";
    if (node instanceof Element) {
        elementName = node.getNodeName();
        int prev_siblings = 1;
        Node prev_sibling = node.getPreviousSibling();
        while (null != prev_sibling) {
            if (prev_sibling.getNodeType() == node.getNodeType()) {
                if (prev_sibling.getNodeName().equalsIgnoreCase(node.getNodeName())) {
                    prev_siblings++;/*from ww w  . j  a  v  a  2 s. co  m*/
                }
            }
            prev_sibling = prev_sibling.getPreviousSibling();
        }
        elementName = elementName.concat("[" + prev_siblings + "]");
    }
    Node parent = node.getParentNode();
    if (parent == null) {
        return xpath;
    }
    return getXPath(parent, "/" + elementName + xpath);
}

From source file:Main.java

public static Element getElement2(Element parentElement, String nodeName) {
    Node node = parentElement.getFirstChild();
    while (null != node) {
        if ((Element.ELEMENT_NODE == node.getNodeType()) && (nodeName.equals(node.getNodeName()))) {
            return (Element) node;
        }/*w ww .  ja v a  2  s  . c  om*/
        node = node.getNextSibling();
    }
    return null;
}

From source file:Main.java

public static Element overrideXml(Element target, Element parent) {

    if (parent != null) {

        NamedNodeMap namedNodeMap = parent.getAttributes();
        for (int i = 0; i < namedNodeMap.getLength(); i++) {

            Node attributeNode = namedNodeMap.item(i);
            String parentAttributeName = attributeNode.getNodeName();
            String parentAttributeValue = attributeNode.getNodeValue();

            // attribute override
            if (!target.hasAttribute(parentAttributeName)) {
                target.setAttribute(parentAttributeName, parentAttributeValue);
            }/*from   w  ww .  ja v  a  2 s .co  m*/

            // children override
            if (parent.getChildNodes().getLength() > 0) {
                if (target.getChildNodes().getLength() == 0) {
                    for (int j = 0; j < target.getChildNodes().getLength(); j++) {

                        target.appendChild(target.getChildNodes().item(j));
                    }
                }
            }

        }
    }

    return target;
}