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

private static String getNodeAttribute(Element element, String nodeWeiZhi, String attributeName) {
    String result = "";
    String[] nodeNames = nodeWeiZhi.split(">");
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        String nodeName = node.getNodeName();
        if (nodeName.equals(nodeNames[0])) {
            if (nodeNames.length == 1) {
                result += "," + node.getAttributes().getNamedItem(attributeName).getNodeValue().trim();
            }/*from   w ww.  ja v a2s . co  m*/
            String nodeWeiZhiTemp = nodeWeiZhi.replaceFirst(nodeNames[0], "").replaceFirst(">", "");
            NodeList childrenTempList = node.getChildNodes();
            if (childrenTempList.getLength() > 0 && !nodeWeiZhiTemp.equals("")) {
                result += getNodeAttribute((Element) node, nodeWeiZhiTemp, attributeName);
            }
        }
    }
    return result;
}

From source file:Main.java

/**
 * Method provides getting Element objects out of a NodeList. This method is
 * useful if the xml file is formated with whitespaces because they are
 * interpreted by the DOM parser as text nodes. So especially calls to<br>
 * getFirstChild(), getLastChild(), etc<br>
 * will not surely return elements. To be sure, that you will get an Element
 * use this method by giving the list of child nodes
 * (parent.getChildNodes()) and the parent node (necessary so that only the
 * direct child will be returned and no deeper ones).<br>
 * If no Element is in the list that is a direct child of parent null will
 * be returned.//  w  w w .  j  a va 2s  .c  o m
 * 
 * @param childs
 *            a NodeList that contains all nodes which should be checked
 *            wether they are elements and direct childs of the given
 *            parent. The first node that matches these criterions will be
 *            returned
 * @param parent
 *            the parent node of the given child nodes
 * 
 * @return the first Element of the given NodeList whose parent node is the
 *         given parent. If no such Element exists null will be returned
 */
public static Element getChildElement(NodeList childs, Node parent) {
    Node childNode;
    String parentName;

    parentName = parent.getNodeName();

    for (int i = 0; i < childs.getLength(); i++) {
        childNode = childs.item(i);

        if ((childNode instanceof Element) && (childNode.getParentNode().getNodeName().equals(parentName))) {
            return (Element) childNode;
        }
    }

    return null;
}

From source file:Main.java

public static void setResourceAttributeValue(Node node, String resourceType, String attribute, String value) {
    String currentNodeName = node.getNodeName();
    if (currentNodeName.startsWith(resourceType)) {
        setAttributeValue(node, attribute, value);
    } else if (currentNodeName.startsWith(RESOURCE_ITEM)) {
        String typeValue = getAttributeValue(node, ATTRIBUTE_TYPE);
        if (resourceType.equals(typeValue)) {
            setAttributeValue(node, attribute, value);
        }/*from   ww w.jav a 2s. c  o  m*/
    }
}

From source file:Main.java

public static ArrayList<Node> getChildNodesWithTagName(String tagName, Element parentElement) {
    NodeList childNodes = parentElement.getChildNodes();

    ArrayList<Node> childNodesWithTagName = new ArrayList<Node>();

    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE && childNode.getNodeName().equals(tagName)) {
            childNodesWithTagName.add(childNode);
        }//from   www  .  ja v  a  2  s  .  c  om
    }

    return childNodesWithTagName;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public final static Vector subElementList(Element superEle, String subName) {
    Vector v = new Vector();
    NodeList list = superEle.getChildNodes();
    if (list == null) {
        return null;
    }//from ww w .ja  v  a  2 s.  c o m

    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equals(subName)) {
                v.add(node);
            }
        }
    }
    return v;
}

From source file:Main.java

/**
 * @param node//from  w w w .ja  va 2  s . c  o  m
 * @param name
 * @return all children nodes with the given name
 */
public static List<Node> findChildren(Node node, String name) {
    List<Node> ret = new LinkedList<Node>();
    NodeList nl = node.getChildNodes();
    int len = nl.getLength();
    Node child;
    for (int i = 0; i < len; i++) {
        child = nl.item(i);
        if (name.equals(child.getNodeName())) {
            ret.add(child);
        }
    }
    return ret;
}

From source file:Main.java

public static Node findChildNode(Node parent, String childName) {
    NodeList childNodes = parent.getChildNodes();
    int childCount = childNodes.getLength();
    Node child;

    for (int i = 0; i < childCount; i++) {
        child = childNodes.item(i);//from w w  w .j a v  a 2  s .  c  o m

        if (child.getNodeName().equals(childName)) {
            return child;
        }
    }

    return null;
}

From source file:Main.java

public static Map<String, Object> convertNodeToMap(Node node) {
    NodeList nodeList = node.getChildNodes();

    Map<String, Object> map = new HashMap<String, Object>(nodeList.getLength());

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node nodec = nodeList.item(i);

        String key = nodec.getNodeName();

        Object value = null;/*  w w w.ja v  a2s  .c om*/
        if (nodec.hasChildNodes()) {

            NodeList nodeListc = nodec.getChildNodes();
            if (nodeListc.getLength() == 1) {
                Node noded = nodeListc.item(0);

                short type = noded.getNodeType();

                if (type == 3 || type == 4) {
                    value = noded.getNodeValue();
                }

                if (noded.getNodeType() == 1) {
                    value = convertNodeToMap(nodec);
                }
            } else {
                value = convertNodeToMap(nodec);
            }
        }

        map.put(key, value);
    }

    return map;
}

From source file:Main.java

/**
 * get the name of an XML element, with the namespace prefix stripped off
 * @param el/*from  w w  w .  j  a va  2 s . co  m*/
 * @return
 */
public static String getLocalName(Node el) {
    String locName = "";
    StringTokenizer st = new StringTokenizer(el.getNodeName(), ":");
    while (st.hasMoreTokens())
        locName = st.nextToken();
    return locName;
}

From source file:Main.java

/**
 * Removes any child elements that match the type
 * @param parent The element whose children will be removed
 * @param type The name of the children elements to remove
 *///  w  w w .  ja va  2 s.c  om
public static void removeChildrenOfType(final Node parent, final String type) {
    final NodeList children = parent.getChildNodes();
    for (int childIndex = 0; childIndex < children.getLength(); ++childIndex) {
        final Node child = children.item(childIndex);
        if (child.getNodeName().equals(type)) {
            parent.removeChild(child);
            break;
        }
    }
}