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

/**
 * Get all immediate element children with the given name. This differs from {@link
 * org.w3c.dom.Element#getElementsByTagName getElementsByTagName} in that this only returns
 * direct children, not all descendents.
 *
 * @param parent    the parent node//from  w w w . j av  a  2  s  .c  om
 * @param childName the name of the children elements to get; may be null or "*" to indicate all
 *                  element children
 * @return the list of children; may be empty
 */
public static NodeList getChildren(Element parent, String childName) {
    final NodeList children = parent.getChildNodes();
    final String filter = (childName != null && !childName.equals("*")) ? childName : null;

    return new NodeList() {
        private List elems;

        {
            elems = new ArrayList();
            for (int idx = 0; idx < children.getLength(); idx++) {
                Node n = children.item(idx);
                if (n.getNodeType() == Node.ELEMENT_NODE && (filter == null || n.getNodeName().equals(filter)))
                    elems.add(n);
            }
        }

        public Node item(int index) {
            return (Node) elems.get(index);
        }

        public int getLength() {
            return elems.size();
        }
    };
}

From source file:Main.java

/**
 * Returns a list of elements having a given tag
 *//*from  ww  w  . ja  v a  2s.c om*/
public static List<Element> getElements(Element element, String tagName) {
    Node node = element.getFirstChild();
    List<Element> elements = new ArrayList<Element>();

    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().compareTo(tagName) == 0)
            elements.add((Element) node);

        node = node.getNextSibling();
    }

    return elements;
}

From source file:Main.java

/**
 * Access all immediate child elements inside the given Element
 *
 * @param element the starting element, cannot be null.
 * @param elemName the name of the child element to look for, cannot be
 * null.//from  ww w. j  a v  a2  s . co  m
 * @return array of all immediate child element inside element, or
 * an array of size zero if no child elements are found.
 */
public static Element[] getChildElements(Element element, String elemName) {
    NodeList list = element.getChildNodes();
    int len = list.getLength();
    ArrayList<Node> array = new ArrayList<Node>(len);

    for (int i = 0; i < len; i++) {
        Node n = list.item(i);

        if (n.getNodeType() == Node.ELEMENT_NODE) {
            if (elemName.equals(n.getNodeName())) {
                array.add(n);
            }
        }
    }
    Element[] elems = new Element[array.size()];

    return (Element[]) array.toArray(elems);
}

From source file:Main.java

public static String getAttrsAsString(Node node) {

    if (node == null)
        return "";

    NamedNodeMap attrs = node.getAttributes();
    StringBuilder val = new StringBuilder();

    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            Node nval = attrs.item(i);

            if (nval != null) {
                if (i > 0)
                    val.append(", ");

                val.append(nval.getNodeName()).append("=").append(nval.getNodeValue());
            }/* w ww  .j  a  v  a 2  s  . c om*/
        }
    }

    return val.toString().trim();
}

From source file:com.mingo.parser.xml.dom.DomUtil.java

/**
 * Transform node attributes to map.//  w w  w . j  av a2  s  .c o m
 *
 * @param node {@link Node}
 * @return map : key - attribute name; value - attribute value
 */
public static Map<String, String> getAttributes(Node node) {
    Map<String, String> attributes = ImmutableMap.of();
    if (node.hasAttributes()) {
        ImmutableMap.Builder builder = ImmutableMap.builder();
        // get attributes names and values
        NamedNodeMap nodeMap = node.getAttributes();
        for (int i = 0; i < nodeMap.getLength(); i++) {
            Node currentNode = nodeMap.item(i);
            builder.put(currentNode.getNodeName(), currentNode.getNodeValue());
        }
        attributes = builder.build();
    }
    return attributes;
}

From source file:Main.java

/**
 * Get all child elements with the provided name that are direct children
 * the provided element.// w w  w .  j  av a 2s. c om
 * 
 * @param parent
 *            The parent element
 * @param name
 *            The name of the child elements to find
 * @return The list with child elements, empty list if no matching children
 */
public static Collection<Element> getChildElementsByName(Element parent, String name) {
    assertNotNull(parent);
    Collection<Element> childList = new ArrayList<Element>();

    NodeList children = parent.getChildNodes();
    Node node;
    for (int i = 0; i < children.getLength(); i++) {
        node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) {
            childList.add((Element) node);
        }
    }

    return childList;
}

From source file:Main.java

/** Find the first direct child with a given attribute.
 * @param parent/*from  w  ww. jav  a  2  s. co m*/
 * @param elemName name of the element, or null for any 
 * @param attName attribute we're looking for
 * @param attVal attribute value or null if we just want any
 */
public static Node findChildWithAtt(Node parent, String elemName, String attName, String attVal) {

    Node child = DomUtil.getChild(parent, Node.ELEMENT_NODE);
    if (attVal == null) {
        while (child != null && (elemName == null || elemName.equals(child.getNodeName()))
                && DomUtil.getAttribute(child, attName) != null) {
            child = getNext(child, elemName, Node.ELEMENT_NODE);
        }
    } else {
        while (child != null && (elemName == null || elemName.equals(child.getNodeName()))
                && !attVal.equals(DomUtil.getAttribute(child, attName))) {
            child = getNext(child, elemName, Node.ELEMENT_NODE);
        }
    }
    return child;
}

From source file:Main.java

/**
 * For the given {@code sourceNode}, read each "top level" element into the resulting {@code Map}. Each element name
 * is a key to the map, each element value is the value paired to the key. Example - anchor is the node, label and
 * href are keys:/*from w  w w. jav a 2 s  .  c  o m*/
 *
 * <pre>
 * {@code
 * <anchor>
 *  <label>Slashdot</label>
 *    <href>http://slashdot.org/</href>
 *  </anchor>
 * }
 * </pre>
 */
public static Map<String, String> readNodeElementsToMap(final Node sourceNode) {
    Map<String, String> result = new HashMap<String, String>();

    if (sourceNode == null) {
        return result;
    }

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

        if (element.getNodeType() == Node.ELEMENT_NODE) {
            String elementName = element.getNodeName();
            String elementValue = "";
            Node firstChild = element.getFirstChild();

            if (firstChild != null) {
                elementValue = firstChild.getNodeValue();
            }

            if (elementValue != null) {
                result.put(elementName, elementValue);
            }
        }
    }

    return result;
}

From source file:Main.java

public static void walkNodes(Node nodeIn, StringBuffer sb, String sPad) {
    if (nodeIn == null)
        return;//  w  w w  .  j  a  va  2 s.  com
    NamedNodeMap map = nodeIn.getAttributes();
    if (map != null)
        for (int i = 0; i < map.getLength(); i++) {
            Node n = map.item(i);
            if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
                sb.append(sPad + "   Attribute:" + n.getNodeName() + " = " + n.getNodeValue() + '\n');
            }
        }
    NodeList nodes = nodeIn.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            sb.append(sPad + "Element: " + n.getNodeName() + '\n');
        }
        if (n.getNodeType() == Node.TEXT_NODE) {
            sb.append(sPad + "   Value: = " + n.getNodeValue() + '\n');
        }
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            walkNodes(n, sb, sPad + "   ");
        }
    }
}

From source file:Main.java

/**
 * Get the non qualified tag name//from  w  ww .ja v  a2s  .  c  o  m
 *
 * @param node node
 *
 * @return node name
 */
public static String getNodeName(Node node) {
    String localName = node.getLocalName();
    if (localName != null) {
        return localName;
    }
    String name = node.getNodeName();
    int idx = name.indexOf(":");
    if (idx >= 0) {
        name = name.substring(idx + 1);
    }
    return name;
}