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 the elements with the given name.
 * @param element//ww  w  .j a va  2 s  . c om
 * @param name
 * @return array, never <code>null</code>
 */
public static Element[] getChildElementsNamed(Element element, String name) {
    if (element == null)
        return new Element[0];
    NodeList nodeList = element.getChildNodes();
    List<Element> elements = new ArrayList<Element>();
    Node node = nodeList.item(0);
    while (node != null) {
        if (node instanceof Element && name.equals(node.getNodeName())) {
            elements.add((Element) node);
        }
        node = node.getNextSibling();
    }
    return (Element[]) elements.toArray(new Element[elements.size()]);
}

From source file:Main.java

/**
 * Gets the nodenames of childs./*from   ww w.  j ava  2 s. c  om*/
 *
 * @param dataNode
 *            the data node
 * @return the nodenames of childs
 */
public static List<String> getNodenamesOfChilds(Node dataNode) {
    List<String> returnList = new ArrayList<String>();

    NodeList list = dataNode.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() != Node.TEXT_NODE) {
            returnList.add(node.getNodeName());
        }
    }

    return returnList;
}

From source file:Main.java

private static void printNote(NodeList nodeList, int depth) {
    for (int count = 0; count < nodeList.getLength(); count++) {
        Node tempNode = nodeList.item(count);
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            System.out.println(depth + "Node Name =" + tempNode.getNodeName());
            System.out.println(depth + "Node Value =" + tempNode.getTextContent());
            if (tempNode.hasAttributes()) {
                NamedNodeMap nodeMap = tempNode.getAttributes();
                for (int i = 0; i < nodeMap.getLength(); i++) {
                    Node node = nodeMap.item(i);
                    System.out.println("attr name : " + node.getNodeName());
                    System.out.println("attr value : " + node.getNodeValue());
                }// w  w w.j a va2s  . c  om
            }
            if (tempNode.hasChildNodes()) {
                printNote(tempNode.getChildNodes(), depth + 1);
            }
            System.out.println(depth + "Node Name =" + tempNode.getNodeName());
        }
    }
}

From source file:Main.java

/**
 * Gets all facets of sourceElement. If facet has a few children the method
 * will return first one./*from w w w .j  av  a  2 s. c  om*/
 * 
 * @param sourceElement the source element
 * @param facetName      *
 * param returnTextNode return child text node if facet doesn't have
 * any child elements;
 * @param returnTextNode the return text node
 * 
 * @return the facets
 */
public static ArrayList<Node> getFacets(Element sourceElement, boolean returnTextNode) {
    ArrayList<Node> facets = new ArrayList<Node>();
    NodeList children = sourceElement.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node instanceof Element && "f:facet".equals(node.getNodeName())) { //$NON-NLS-1$
            Element element = (Element) node;
            NodeList childNodes = element.getChildNodes();
            Text textNode = null;
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node child = childNodes.item(j);
                if (child instanceof Element) {
                    facets.add(child);
                    break;
                } else if (child instanceof Text) {
                    textNode = (Text) child;
                }
            }
            if (returnTextNode && facets.isEmpty()) {
                facets.add(textNode);
            }
        }
    }
    return facets;
}

From source file:Main.java

public static List<Element> getElements(final Element element, final String tagName) {
    final ArrayList<Element> elements = new ArrayList<>();
    final NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        final Node node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            final String nodeName = node.getNodeName();
            if ((nodeName != null) && nodeName.equals(tagName)) {
                elements.add((Element) node);
            }/*from  w w w.j  ava  2  s  .  c om*/
        }
    }
    return elements;
}

From source file:Main.java

public static List<Element> getNamedChildElements(Element parent, 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);
            }//from  www  .  j a v  a 2  s.  com
            child = child.getNextSibling();
        }
    }
    return elements;
}

From source file:Main.java

/**
 * Searches parent node for matching child nodes and collects and returns
 * their matching attribute String values.
 *
 * @param node     The parent node//ww w. j av a2 s.  co m
 * @param elemName The matching child node element name
 * @param attr     The matching child node attribute name
 * @return List of attribute values
 */
public static Enumeration getChildrenAttributeValues(Node node, String elemName, String attr) {
    Vector vect = new Vector();
    NodeList nl = node.getChildNodes();
    int n = nl.getLength();
    for (int i = 0; i < n; i++) {
        Node nd = nl.item(i);
        if (nd.getNodeName().equals(elemName)) {
            vect.add(getAttribute(nd, attr));
        }
    }
    return vect.elements();
}

From source file:Main.java

public static Map<String, String> XmlAsMap(Node node) {
    Map<String, String> map = new HashMap<String, String>();
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node currentNode = nodeList.item(i);
        if (currentNode.hasAttributes()) {
            for (int j = 0; j < currentNode.getAttributes().getLength(); j++) {
                Node item = currentNode.getAttributes().item(i);
                if (item != null)
                    map.put(item.getNodeName(), prepare(item.getTextContent()));
            }//from  ww  w  . j a  v  a2 s .  c o  m
        }
        if (currentNode.getFirstChild() != null) {
            if (currentNode.getFirstChild().getNodeType() == Node.ELEMENT_NODE) {
                map.putAll(XmlAsMap(currentNode));
            } else if (currentNode.getFirstChild().getNodeType() == Node.TEXT_NODE) {
                map.put(currentNode.getLocalName(), prepare(currentNode.getTextContent()));
            }
        }
    }
    return map;
}

From source file:Main.java

public static String getLocalName(Node node) {
    String localName = node.getLocalName();
    if (localName == null) {
        localName = node.getNodeName();

        // If the document is not namespace aware, we must split the tag name
        // with ':' character.
        int i = localName.indexOf(':');
        if (i != -1) {
            localName = localName.substring(i + 1);
        }/*from w w w. j a  va  2 s  .  c  om*/
    }

    return localName;
}

From source file:Main.java

private static void reorganizePrimitiveToArray(Node parentNode, JsonObject upperJson, JsonObject childJson,
        Node childNode, JsonElement existing) {
    upperJson.remove(parentNode.getNodeName());
    JsonArray arrayJson = new JsonArray();
    arrayJson.add(existing);//from   ww w .  ja  v a2 s.c  o m
    arrayJson.add(new JsonPrimitive(childNode.getNodeValue()));
    upperJson.add(parentNode.getNodeName(), arrayJson);
    REORGANIZED.put(parentNode.hashCode() + EMPTY, childJson);
}