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 String getElementValue(Node node, String nodeName) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getNodeName().equals(nodeName)))
            return getElementValue(child);
    }/*w w  w  .j  av a2 s  .  c o  m*/
    return null;
}

From source file:Main.java

public static String getPkey(final Node markitBondNode) {
    final NodeList markitBondNodeChildrenList = markitBondNode.getChildNodes();
    final int length = markitBondNodeChildrenList.getLength();
    for (int j = 0; j < length; ++j) {
        final Node childNode = markitBondNodeChildrenList.item(j);
        final String childName = childNode.getNodeName();
        if ("pkey".equals(childName)) {
            final String issuerIdString = childNode.getTextContent();
            return issuerIdString;
        }//from  w  w  w .  j a  v a 2 s .  c  om
    }
    return null;
}

From source file:Main.java

public static String[] getChildrenText(Element parentElement, String childrenName) {
    NodeList nl = parentElement.getChildNodes();
    int max = nl.getLength();
    LinkedList<String> list = new LinkedList<String>();
    for (int i = 0; i < max; i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(childrenName)) {
            list.add(getText((Element) n));
        }/*from  w  ww  . j ava2 s  .  c o  m*/
    }
    return list.toArray(new String[list.size()]);
}

From source file:Main.java

public static List<Element> getChildElementsByTagName(Element parentElement, String childTag) {
    NodeList nodelist = parentElement.getChildNodes();
    List<Element> nodes = new ArrayList<Element>();
    for (int i = 0; i < nodelist.getLength(); i++) {
        Node temp = nodelist.item(i);
        if (temp.getNodeType() == Node.ELEMENT_NODE && temp.getNodeName().equals(childTag)) {
            nodes.add((Element) temp);
        }// w  w w .j a  v a 2s .  c o  m
    }
    return nodes;
}

From source file:Main.java

public static NodeList ChangeNode(NodeList listNode, ArrayList<String> arrStrCompare, String strNewValue) {
    ArrayList<String> arrTempList = arrStrCompare;
    Iterator<String> iterator = arrTempList.iterator();

    while (iterator.hasNext()) {
        String strCompare = (String) iterator.next();
        iterator.remove();//from w  ww . j a v  a2 s.  c o  m

        for (int i = 0; i < listNode.getLength(); i++) {
            Node node = listNode.item(i);

            if (strCompare.equals(node.getNodeName())) {
                if (iterator.hasNext()) {
                    node = ChangeNode(node.getChildNodes(), arrTempList, strNewValue).item(0);
                    break;
                }
                node.setTextContent(strNewValue);

                break;
            }
        }
    }

    return listNode;
}

From source file:TryDOM.java

static void listNodes(Node node, String indent) {
        String nodeName = node.getNodeName();
        System.out.println(indent + nodeName + " Node, type is " + node.getClass().getName() + ":");
        System.out.println(node);

        if (node instanceof Element && node.hasAttributes()) {
            NamedNodeMap attrs = node.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Attr attribute = (Attr) attrs.item(i);
                System.out.println(indent + attribute.getName() + "=" + attribute.getValue());
            }/*  w ww  .j a v a2 s .  co m*/
        }
        NodeList list = node.getChildNodes();
        if (list.getLength() > 0) {
            System.out.println(indent + "Child Nodes of " + nodeName + " are:");
            for (int i = 0; i < list.getLength(); i++)
                listNodes(list.item(i), indent + " ");
        }
    }

From source file:Main.java

private static Map<?, ?> getNodeBean(Node parent) {
    Map<Object, Object> rtn = new HashMap<Object, Object>();

    if (parent != null) {
        Map<Object, Object> attrMap = new HashMap<Object, Object>();
        if (parent.hasAttributes()) {
            NamedNodeMap attrs = parent.getAttributes();
            for (int j = 0; j < attrs.getLength(); j++) {
                Node attr = attrs.item(j);
                attr.getNodeName();
                attr.getNodeValue();//from  w w w .j  av a2  s . c  om
                attrMap.put(attr.getNodeName(), attr.getNodeValue());
            }
        }

        rtn.put("tagName", parent.getNodeName());
        rtn.put("attr", attrMap);

        NodeList nodeList = parent.getChildNodes();
        if (nodeList != null) {
            List<Object> children = new ArrayList<Object>();
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node child = nodeList.item(i);
                if (child.getNodeType() == Node.ELEMENT_NODE) {
                    children.add(getNodeBean(child));
                }
            }

            rtn.put("children", children);
        }
    }

    return rtn;
}

From source file:Main.java

public static List<Element> getChildElements(String tagName, Element element) {
    LinkedList<Element> result = new LinkedList<>();
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tagName)) {
            result.add((Element) n);
        }/* w w  w.j  ava 2  s .  c o m*/
    }
    return result;
}

From source file:Main.java

public static String getResourceAttributeValue(Node node, String resourceType, String attribute) {
    String currentNodeName = node.getNodeName();
    if (currentNodeName.startsWith(resourceType)) {
        return getAttributeValue(node, attribute);
    } else if (currentNodeName.startsWith(RESOURCE_ITEM)) {
        String typeValue = getAttributeValue(node, ATTRIBUTE_TYPE);
        if (resourceType.equals(typeValue)) {
            return getAttributeValue(node, attribute);
        }//from  w ww .ja  v  a 2 s  .  c  om
    }
    return null;
}

From source file:Main.java

/**
 * Rename an element, replacing it in its document.
 * @param element the element to rename.
 * @param name the new element name.// w w  w.j a v  a  2 s.  c  om
 * @return the renamed element.
 */
public static Element renameElement(Element element, String name) {
    if (element.getNodeName().equals(name))
        return element;
    Element el = element.getOwnerDocument().createElement(name);

    //Copy the attributes
    NamedNodeMap attributes = element.getAttributes();
    int nAttrs = attributes.getLength();
    for (int i = 0; i < nAttrs; i++) {
        Node attr = attributes.item(i);
        el.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }

    //Copy the children
    Node node = element.getFirstChild();
    while (node != null) {
        Node clone = node.cloneNode(true);
        el.appendChild(clone);
        node = node.getNextSibling();
    }

    //Replace the element
    element.getParentNode().replaceChild(el, element);
    return el;
}