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 getShiftId(Node node) {
    String name = "";
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node curNode = nodeList.item(i);
        if ("shiftId".equalsIgnoreCase(curNode.getNodeName())) {
            name = curNode.getTextContent().replace("\"", "");
            break;
        }//from w w w  .ja v  a2s  .c o  m
    }

    return name;
}

From source file:Main.java

public static Collection<Node> search_nodes_by_name(Node root, String name) {
    Collection<Node> result = new LinkedList<Node>();
    if (root.getNodeName().equals(name))
        result.add(root);//from   ww w.  j  ava  2s. c o m
    NodeList list = root.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        Node child = list.item(i);
        Collection<Node> ret = search_nodes_by_name(child, name);
        result.addAll(ret);
    }
    return result;
}

From source file:Main.java

/**
 * Gets first child element with specified name.
 *
 * @param parentNode parent node./*from   www.ja  v  a  2  s  .c  om*/
 * @param childName  child name.
 * @return first childr element with specified name.
 */
public static Element getChildElement(final Node parentNode, final String childName) {
    //        parentNode.normalize();
    final NodeList nodeList = parentNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        final Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(childName))
            return (Element) node;
    }
    return null;
}

From source file:Main.java

/***
 *  Print every attribute and value of a node, one line at a time.
 *  If {@link entry} is null, then outputs "&lt;null/&gt;".
 *
 * @param outs where to send output, cannot be null.
 * @param entry XML node to examine, OK if null.
 *//*from ww  w  .j  av a  2 s  .com*/
public static void XmlPrintAttrs(PrintStream outs, Node entry) {
    if (entry == null) {
        outs.print("<null/>");
        return;
    }

    //  see http://www.w3.org/2003/01/dom2-javadoc/org/w3c/dom/NamedNodeMap.html
    NamedNodeMap attrs = entry.getAttributes();

    for (int k = attrs.getLength(); --k >= 0;) {
        Node n = attrs.item(k);
        outs.printf("+++ has attr %s = %s\n", n.getNodeName(), n.getNodeValue());
    }
}

From source file:Main.java

public static ArrayList<Element> getElementsByTagName(Node node, String tagName) {
    ArrayList<Element> elements = new ArrayList<Element>();
    for (int i = 0; i < node.getChildNodes().getLength(); i++) {
        Node n = node.getChildNodes().item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tagName)) {
            elements.add((Element) n);
        }//from  w w  w .  j a  va2s .  co m
    }
    return elements;
}

From source file:Main.java

/**
 * Finds the node of the argument name in the tree under the argument node.
 * @param name The name of the node to search for, case insensitive.
 * @param node The root node of the tree under which to look for the named node.
 * @return the first found occurrence of the named node (breadth first),
 * or null if the node is not found.//from   w  ww . j  a  va  2s .  c o  m
 */
public static Node findChild(String name, Node node) {
    if (node == null)
        return null;

    if (node.hasChildNodes()) {
        NodeList kids = node.getChildNodes();
        int numKids = kids.getLength();
        for (int index = 0; index < numKids; ++index) {
            Node kid = kids.item(index);
            if (kid.getNodeName().equalsIgnoreCase(name))
                return kid;
        } // for

        for (int index = 0; index < numKids; ++index) {
            Node found = findChild(name, kids.item(index));
            if (found != null)
                return found;
        } // for
    } // if

    return null;
}

From source file:Main.java

/**
 * Method to convert a NODE XML to a string.
 * @param n node XML to input.//from   w  w w .  j  a  v  a2 s  .  c  o m
 * @return string of the node n.
 */
public static String convertElementToString(Node n) {
    String name = n.getNodeName();
    short type = n.getNodeType();
    if (Node.CDATA_SECTION_NODE == type) {
        return "<![CDATA[" + n.getNodeValue() + "]]&gt;";
    }
    if (name.startsWith("#")) {
        return "";
    }
    StringBuilder sb = new StringBuilder();
    sb.append('<').append(name);
    NamedNodeMap attrs = n.getAttributes();
    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue()).append("\"");
        }
    }
    String textContent;
    NodeList children = n.getChildNodes();
    if (children.getLength() == 0) {
        if ((textContent = n.getTextContent()) != null && !"".equals(textContent)) {
            sb.append(textContent).append("</").append(name).append('>');
            //;
        } else {
            sb.append("/>").append('\n');
        }
    } else {
        sb.append('>').append('\n');
        boolean hasValidChildren = false;
        for (int i = 0; i < children.getLength(); i++) {
            String childToString = convertElementToString(children.item(i));
            if (!"".equals(childToString)) {
                sb.append(childToString);
                hasValidChildren = true;
            }
        }
        if (!hasValidChildren && ((textContent = n.getTextContent()) != null)) {
            sb.append(textContent);
        }
        sb.append("</").append(name).append('>');
    }
    return sb.toString();
}

From source file:Main.java

public static Node findFirstChild(Node node, String name) {
    NodeList childNodes = node.getChildNodes();
    int count = childNodes.getLength();
    for (int i = 0; i < count; i++) {
        Node child = childNodes.item(i);
        if (child instanceof Element && (name == null || child.getNodeName().equals(name))) {
            return child;
        }/* w w  w  .  j a  v  a  2s  .  com*/
    }
    return null;
}

From source file:Main.java

public static String getSubTagAttribute(Element root, String tagName, String subTagName, String attribute) {
    String returnString = "";
    NodeList list = root.getElementsByTagName(tagName);
    for (int loop = 0; loop < list.getLength(); loop++) {
        Node node = list.item(loop);
        if (node != null) {
            NodeList children = node.getChildNodes();
            for (int innerLoop = 0; innerLoop < children.getLength(); innerLoop++) {
                Node child = children.item(innerLoop);
                if ((child != null) && (child.getNodeName() != null)
                        && child.getNodeName().equals(subTagName)) {
                    if (child instanceof Element) {
                        return ((Element) child).getAttribute(attribute);
                    }//from w w w . ja v a  2  s. com
                }
            } // end inner loop
        }
    }
    return returnString;
}

From source file:Main.java

public static Node getElementByTagName(NodeList children, String tagName) {
    if (children == null || tagName == null) {
        return null;
    }//ww w . ja  v a 2 s  .c om
    Node requredNode = null;
    int length = children.getLength();
    for (int i = 0; i < children.getLength(); i++) {
        Node tmpNode = children.item(i);
        if (tagName.equals(tmpNode.getNodeName())) {
            requredNode = tmpNode;
            break;
        }
    }
    return requredNode;
}