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

@Nonnull
public static List<Element> getChildrenElements(@Nonnull Element element, @Nonnull String childElementName) {
    NodeList childElts = element.getChildNodes();
    List<Element> result = new ArrayList<>();

    for (int i = 0; i < childElts.getLength(); i++) {
        Node node = childElts.item(i);
        if (node instanceof Element && node.getNodeName().equals(childElementName)) {
            result.add((Element) node);
        }/*from   ww  w.  ja va 2s  . c om*/
    }

    return result;
}

From source file:Main.java

private static String getSubTagValue(Node node, String subTagName) {
    String returnString = "";
    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)) {
                Node grandChild = child.getFirstChild();
                if (grandChild.getNodeValue() != null)
                    return grandChild.getNodeValue();
            }/*from   w w  w .java 2 s. com*/
        } // end inner loop
    }
    return returnString;
}

From source file:Main.java

/**
 * Returns formatted attributes of the node.
 * /* w ww . jav a2 s  .c o m*/
 * @param node The node.
 * @return Formatted attributes.
 */
public static String formatAttributes(Node node) {
    StringBuilder sb = new StringBuilder();
    NamedNodeMap attrs = node.getAttributes();

    for (int i = 0; i < attrs.getLength(); i++) {
        Node attr = attrs.item(i);
        sb.append(' ').append(attr.getNodeName()).append("= '").append(attr.getNodeValue()).append("'");
    }

    return sb.toString();
}

From source file:Main.java

public static Element findChildElementWithAttribute(Element parent, String name, String attribute,
        String value) {/*from w ww  .  j ava  2 s  .c o  m*/
    if (parent == null) {
        return null;
    }
    org.w3c.dom.Node ret = parent.getFirstChild();
    while (ret != null && (!(ret instanceof Element) || !ret.getNodeName().equals(name)
            || ((Element) ret).getAttribute(attribute) == null
            || !((Element) ret).getAttribute(attribute).equals(value))) {
        ret = ret.getNextSibling();
    }
    return (Element) ret;
}

From source file:Main.java

public static String getNodePath(Node node) {
    StringBuffer sb = new StringBuffer(32);
    while (node != null) {
        sb.append(node.getNodeName());
        sb.append("/");
        node = node.getParentNode();/*from  www . j  ava  2s.c  om*/
    }
    return sb.toString();
}

From source file:Main.java

public static Vector<HashMap> xmlToVector222(InputStream is, String xpath) {
    try {/*ww w  .  j  ava 2s .  c o m*/
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        InputSource inputSource = new InputSource(is);

        NodeList nodes = (NodeList) xPath.evaluate(xpath, inputSource, XPathConstants.NODESET);
        Vector<HashMap> vector = new Vector<HashMap>();

        for (int x = 0; x < nodes.getLength(); x++) {
            NodeList nodeList = nodes.item(x).getChildNodes();
            HashMap hashmap = new HashMap();
            for (int y = 0; y < nodeList.getLength(); y++) {
                Node node = nodeList.item(y);
                if (!node.getNodeName().equals("#text")) {
                    hashmap.put(node.getNodeName(), node.getTextContent());
                }
            }
            vector.add(hashmap);
        }
        return vector;
    } catch (Exception ex) {
        ex.printStackTrace();
        return new Vector();
    }
}

From source file:Main.java

public static List<org.w3c.dom.Node> getMatchingChildren(org.w3c.dom.Node node, String name) {
    if (node == null) {
        return null;
    }// w w w  .ja va  2s  .  co  m

    LinkedList<org.w3c.dom.Node> returnList = new LinkedList<org.w3c.dom.Node>();

    org.w3c.dom.NodeList childList = node.getChildNodes();
    int len = childList.getLength();
    for (int i = 0; i < len; i++) {
        org.w3c.dom.Node curNode = childList.item(i);
        if (name.equals(curNode.getNodeName())) {
            returnList.add(curNode);
        }
    }
    return returnList;
}

From source file:Main.java

private static Node actualFindNodeWithAttributeValue(Node node, String name, String attribute, String value) {

    String nodeName = node.getNodeName();
    nodeName = nodeName.substring((nodeName.indexOf(":") != -1 ? nodeName.indexOf(":") + 1 : 0));
    if (nodeName.equals(name)) {
        Element e = (Element) node;
        if (e.getAttribute(attribute) != null && e.getAttribute(attribute).equals(value))
            return node;
    }/* w  w w.  j a  va2  s. co  m*/
    if (node.hasChildNodes()) {
        NodeList list = node.getChildNodes();
        int size = list.getLength();
        for (int i = 0; i < size; i++) {
            Node found = actualFindNodeWithAttributeValue(list.item(i), name, attribute, value);
            if (found != null)
                return found;
        }
    }
    return null;
}

From source file:Main.java

private static String getSubTagValue(Node node, String subTagName) {
    String returnString = "";
    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))) {
                Node grandChild = child.getFirstChild();
                if (grandChild.getNodeValue() != null) {
                    return grandChild.getNodeValue();
                }// w  w w  .  j  a v a 2  s . com
            }
        }
    }
    return returnString;
}

From source file:Main.java

/**
 * Getter for a specific text content of the given node.
 * @param node Node./* ww  w  . j  a v a  2  s.  c  o m*/
 * @param name Name of element tags around the text content.
 * @return Returns the content or null if no content with the given
 * name exists.
 */
static public String getTextContent(Node node, String name) {
    node = node.getFirstChild();
    while (node != null) {
        if (node.getNodeName().equals(name))
            return (node.getTextContent());
        node = node.getNextSibling();
    }
    return (null);
}