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

/**
 * Constructs a XPath query to the supplied node.
 * /*from   w w  w  .  j a  v a  2 s.  c om*/
 * @param n
 * @return
 */
public static String getXPath(Node n) {
    if (null == n) {
        throw new IllegalArgumentException("Invalid node");
    }

    ArrayList<Node> hierarchy = new ArrayList<Node>();
    StringBuffer buffer = new StringBuffer();
    Node parent = null;

    // Push parent element's on stack
    hierarchy.add(n);
    parent = n.getParentNode();
    while (parent != null && parent.getNodeType() != Node.DOCUMENT_NODE) {
        hierarchy.add(0, parent);
        parent = parent.getParentNode();
    }

    Iterator<Node> i = hierarchy.iterator();
    while (i.hasNext()) {
        Node node = i.next();
        buffer.append("/");
        buffer.append(node.getNodeName());
        if (node.hasAttributes()) {
            Node uuid = node.getAttributes().getNamedItem("uuid");
            if (uuid != null) {
                buffer.append("[@uuid='");
                buffer.append(uuid.getNodeValue());
                buffer.append("']");
            }
        }
    }

    // return buffer
    return buffer.toString();
}

From source file:Main.java

public static Map<String, String> getChildElementNodesMap(Node node) {
    if (node == null) {
        return null;
    }/* w w  w .jav a 2  s .c o  m*/

    Map<String, String> map = new ConcurrentHashMap<>();
    NodeList nodeList = node.getChildNodes();
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node item = nodeList.item(i);
            if (item != null && item.getNodeType() == Node.ELEMENT_NODE) {
                map.put(item.getNodeName(), item.getTextContent().trim());
            }
        }
    }
    return map;
}

From source file:Main.java

public static List<Element> getNamedChildElements(final Element parent, final String name) {
    List<Element> elements = new ArrayList<Element>();
    if (parent != null) {
        Node child = parent.getFirstChild();
        while (child != null) {
            if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getNodeName().equals(name))) {
                elements.add((Element) child);
            }/* www  . j a  v a2s.c  om*/
            child = child.getNextSibling();
        }
    }
    return elements;
}

From source file:Main.java

/**
 * List all child nodes with name sName/*from www .  j av a 2  s.  c o m*/
 * NOTE: we assume no same name nodes are nested.
 * @param node
 * @param sName
 * @return Element
 */
public static ArrayList<Element> listChildElementsByName(Node node, String sName) {
    ArrayList<Element> aNodes = new ArrayList<Element>();
    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            if (sName.equals(n.getNodeName())) {
                aNodes.add((Element) n);
            } else {
                ArrayList<Element> nextNodes = listChildElementsByName(n, sName);
                if (nextNodes != null)
                    aNodes.addAll(nextNodes);
            }
        }
        // Don't search anything but elements
    }
    return aNodes;
}

From source file:Main.java

public static void testx() {
    try {/*from   w  w  w.  j av a 2s . c  om*/
        File fXmlFile = new File("C:\\Users\\is96092\\Desktop\\music\\Megaman2.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        //optional, but recommended
        //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName("staff");

        System.out.println("----------------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);

            System.out.println("\nCurrent Element :" + nNode.getNodeName());

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                System.out.println("Staff id : " + eElement.getAttribute("id"));
                System.out.println(
                        "First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
                System.out.println(
                        "Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
                System.out.println(
                        "Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
                System.out.println(
                        "Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());

            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Element findPreviousElement(final Node current, final boolean sameName) {
    String name = null;//from ww w. j  a v  a  2  s .c o  m
    if (sameName) {
        name = current.getNodeName();
    }
    int type = Node.ELEMENT_NODE;
    return (Element) getPrevious(current, name, type);
}

From source file:Main.java

/**
 * Helper Method. Searches through the child nodes of an element and returns an array of the matching nodes.
 * @param element Element// w ww .  java 2  s  . co m
 * @param name String
 * @param caseSensitive boolean
 * @return ArrayList
 */
public static List<Node> getChildNodesByName(Node element, String name, boolean caseSensitive) {
    ArrayList<Node> nodes = new ArrayList<Node>();
    NodeList list = element.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (caseSensitive) {
            if (node.getNodeName().equals(name))
                nodes.add(node);
        } else {
            if (node.getNodeName().equalsIgnoreCase(name))
                nodes.add(node);
        }
    }
    return nodes;
}

From source file:Main.java

/** Look for Element node if given name.
 *  <p>/*from  w w  w  .  j  av  a  2s  . co m*/
 *  Checks the node and its siblings.
 *  Does not descent down the 'child' links.
 *  @param node Node where to start.
 *  @param name Name of the nodes to look for.
 *  @return Returns node or the next matching sibling or null.
 */
final public static Element findFirstElementNode(Node node, final String name) {
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name))
            return (Element) node;
        node = node.getNextSibling();
    }
    return null;
}

From source file:Main.java

public static Object transformXmlNodesIntoMap(Node node) {
    Map<String, Object> nodeMap = new HashMap<String, Object>();

    NodeList subNodes = node.getChildNodes();

    NamedNodeMap nodeAttrs = node.getAttributes();
    for (int nodeAttrIdx = 0; nodeAttrIdx < nodeAttrs.getLength(); nodeAttrIdx++) {
        Node attrNode = nodeAttrs.item(nodeAttrIdx);
        nodeMap.put("@" + attrNode.getNodeName(), attrNode.getTextContent());
    }/*from www  .j  av  a2s .  c o m*/

    if (nodeAttrs.getLength() == 0)
        if (subNodes.getLength() == 0)
            return "";
        else if (subNodes.getLength() == 1 && subNodes.item(0).getNodeType() == Node.TEXT_NODE)
            return subNodes.item(0).getTextContent();

    for (int subNodeIdx = 0; subNodeIdx < subNodes.getLength(); subNodeIdx++) {
        Node subNode = subNodes.item(subNodeIdx);

        if (subNode.getNodeType() == Node.TEXT_NODE) {
            nodeMap.put(subNode.getNodeName(), subNode.getTextContent());
        } else {
            if (nodeMap.containsKey(subNode.getNodeName())) {
                Object subObject = nodeMap.get(subNode.getNodeName());
                if (subObject instanceof List<?>) {
                    ((List<Object>) subObject).add(transformXmlNodesIntoMap(subNode));
                } else {
                    List<Object> subObjectList = new ArrayList<Object>();
                    subObjectList.add(subObject);
                    subObjectList.add(transformXmlNodesIntoMap(subNode));
                    nodeMap.put(subNode.getNodeName(), subObjectList);
                }
            } else {
                nodeMap.put(subNode.getNodeName(), transformXmlNodesIntoMap(subNode));
            }
        }

    }
    return nodeMap;
}

From source file:Main.java

/**
 * Gets the index of the specified element amongst elements with the same
 * name/* w w  w  .  j a v  a2 s.c om*/
 * 
 * @param element
 *           the element to get for
 * @return the index of the element, will be >= 1
 */

public static int getElementIndex(Node element) {
    int result = 1;

    Node elm = element.getPreviousSibling();
    while (elm != null) {
        if (elm.getNodeType() == Node.ELEMENT_NODE && elm.getNodeName().equals(element.getNodeName()))
            result++;
        elm = elm.getPreviousSibling();
    }

    return result;
}