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 List<Node> getChildElementsByTagName(Node element, String tagName) {
    List<Node> list = new ArrayList<Node>();
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeName().equals(tagName)) {
            list.add(child);/*from  w  w  w .ja v a 2  s.c o m*/
        }
    }
    return list;
}

From source file:Main.java

private static boolean nodeNameMatch(Node node, String desiredName) {
    return (desiredName.equals(node.getNodeName()) || desiredName.equals(node.getLocalName()));
}

From source file:Main.java

/**
 * This grabs the attributes from a dom node and overwrites those values with those
 * specified by the overwrite map./* w  ww . j a va  2  s.  com*/
 *
 * @param node node for building
 * @param overwrite map of attributes to overwrite
 * @return map of attributes
 */
public static Map<String, String> mapifyAttrs(Node node, Map<String, String> overwrite) {
    Map<String, String> map = new HashMap<String, String>();
    NamedNodeMap nnMap = node.getAttributes();
    for (int i = 0; i < nnMap.getLength(); i++) {
        Node attr = nnMap.item(i);
        map.put(attr.getNodeName(), attr.getNodeValue());
    }
    if (overwrite != null) {
        for (Map.Entry<String, String> e : overwrite.entrySet()) {
            map.put(e.getKey(), e.getValue());
        }
    }
    return map;
}

From source file:Main.java

/**
 * Finds all children of a node with the given name
 * @param parent the parent node// w ww.ja  v a 2s  .com
 * @param name the name
 * @return the child nodes
 */
public static List<Node> findAllChildren(Node parent, String name) {
    List<Node> found = new ArrayList<Node>();
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node node = children.item(i);
        if (name.equals(node.getNodeName())) {
            found.add(node);
        }
    }
    return found;
}

From source file:Main.java

private static String getNodeText(Element element, String nodeWeiZhi) {
    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.getTextContent().trim();
            }//from w  ww.ja  v  a2s  .  c  o  m
            String nodeWeiZhiTemp = nodeWeiZhi.replaceFirst(nodeNames[0], "").replaceFirst(">", "");
            NodeList childrenTempList = node.getChildNodes();
            if (childrenTempList.getLength() > 0 && !nodeWeiZhiTemp.equals("")) {
                result += getNodeText((Element) node, nodeWeiZhiTemp);
            }
        }
    }
    return result;
}

From source file:Main.java

private static void serializeAttributeOf(XmlSerializer serializer, Element element) throws IOException {
    NamedNodeMap map = element.getAttributes();
    for (int i = 0; i < map.getLength(); i++) {
        Node attr = map.item(i);
        serializer.attribute(null, attr.getNodeName(), attr.getNodeValue());
    }// w w  w. j a v a2  s .c o m
}

From source file:Main.java

public static List<Element> getMultipleElementsByName(Node node, String name) {
    List<Element> elements = new ArrayList<Element>();
    NodeList nodeList = node.getChildNodes();
    int count = nodeList.getLength();
    for (int i = 0; i < count; i++) {
        Node child = nodeList.item(i);
        if (child instanceof Element && child.getNodeName().equals(name)) {
            elements.add((Element) child);
        }/*from  www .j a v  a  2 s . c  o  m*/
    }
    return elements;
}

From source file:Main.java

/**
 * Returns the first XML child tag with the specified name.
 * /*www  . j  av a 2s.c  o m*/
 * @param node The node to search children of
 * @param name The name of the node to search for, case-sensitive.
 * @return The child with the specified name, or null if none exists.
 */
public static Node getChildNode(Node node, String name) {
    Node child = node.getFirstChild();
    while (child != null && !child.getNodeName().equals(name)) {
        child = child.getNextSibling();
    }
    return child;
}

From source file:MainClass.java

static void listNodes(Node node) {
    String nodeName = node.getNodeName();

    if (node instanceof Element) {
        if (node.hasAttributes()) {
            NamedNodeMap attrs = node.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Attr attribute = (Attr) attrs.item(i); // Get an attribute
                System.out.println(" " + attribute.getName() + "=" + attribute.getValue());
            }//from  w  w  w  .  jav  a2 s. c  om
        }
        System.out.println(indent + "<" + nodeName + ">");
    } else if (node instanceof Text) {
        System.out.println(((Text) node).getData());
    } else if (node instanceof DocumentType) {
        System.out.println(getDoctypeString((DocumentType) node));
    }

    indent.append(' ');
    NodeList list = node.getChildNodes();
    if (list.getLength() > 0) {
        for (int i = 0; i < list.getLength(); i++) {
            listNodes(list.item(i));
        }
    }
    System.out.println("</" + nodeName + ">");

}

From source file:Main.java

/**
 * A method to get attribute value from provided xml node and attribute name
 * @param Node parent, a node where the attribute residing
 * @param String attr, attribute name to get
 * @return String , a value from request attribute 
 *//*from w w w .ja  va2s .  c o  m*/
static public String getAttribute(Node parent, String Attr) {
    String ret = "";

    if (!parent.hasAttributes())
        return ("");
    NamedNodeMap nmap = parent.getAttributes();

    for (int i = 0; i < nmap.getLength(); ++i) {
        Node n = nmap.item(i);
        if (n.getNodeName().trim().equals(Attr)) {
            ret = n.getNodeValue().trim();
        }
        //System.out.println("Attribute: "+n.getNodeType()+" - "+n.getNodeName()+" "+n.getNodeValue());

    }
    return (ret);
}