Example usage for org.w3c.dom Element getTagName

List of usage examples for org.w3c.dom Element getTagName

Introduction

In this page you can find the example usage for org.w3c.dom Element getTagName.

Prototype

public String getTagName();

Source Link

Document

The name of the element.

Usage

From source file:DOMEdit.java

private static void outputElement(Element node, String indent) {
    System.out.print(indent + "<" + node.getTagName());
    NamedNodeMap nm = node.getAttributes();
    for (int i = 0; i < nm.getLength(); i++) {
        Attr attr = (Attr) nm.item(i);
        System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\"");
    }//from  w w  w.j a va  2 s .c  o m
    System.out.println(">");
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++)
        outputloop(list.item(i), indent + TAB);
    System.out.println(indent + "</" + node.getTagName() + ">");
}

From source file:Main.java

public static Collection<Element> fetchSubElements(Element e, String subElementName) {
    if (e == null || subElementName == null) {
        return null;
    }/*  w  w  w .ja  v a  2s. c om*/

    subElementName = subElementName.toUpperCase();

    Collection<Element> elements = new ArrayList<Element>();

    NodeList list = e.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        if (list.item(i) instanceof Element) {
            Element element = (Element) list.item(i);
            if (element.getTagName().toUpperCase().equals(subElementName)) {
                elements.add(element);
            }
        }
    }

    return elements;
}

From source file:Utils.java

private static void findAllElementsByTagName(Element el, String tagName, List<Element> elementList) {

    if (tagName.equals(el.getTagName())) {
        elementList.add(el);/*  www.j  a v  a 2  s .c  o m*/
    }
    Element elem = getFirstElement(el);
    while (elem != null) {
        findAllElementsByTagName(elem, tagName, elementList);
        elem = getNextElement(elem);
    }
}

From source file:Main.java

public static String getNodeHierarchy(Node node) {

    StringBuffer sb = new StringBuffer();
    if (node != null) {
        Stack<Node> st = new Stack<Node>();
        st.push(node);//  w w w . j  ava  2  s.  c  o  m

        Node parent = node.getParentNode();

        while ((parent != null) && (parent.getNodeType() != Node.DOCUMENT_NODE)) {

            st.push(parent);

            parent = parent.getParentNode();
        }

        // constructs node hierarchy
        Node n = null;
        while (!st.isEmpty() && null != (n = st.pop())) {

            if (n instanceof Element) {
                Element e = (Element) n;

                if (sb.length() == 0) {
                    sb.append(e.getTagName());
                } else {
                    sb.append("/" + e.getTagName());
                }
            }

        }
    }
    return sb.toString();

}

From source file:Main.java

static public Element selectParentElement(Element element, String parentName) {
    Element parentElement = (Element) element.getParentNode();
    if (parentElement == null) {
        return null;
    }/*w  w w.j av  a  2 s  . c  o  m*/
    if (parentElement.getTagName().equals(parentName)) {
        return parentElement;
    }
    return selectParentElement(parentElement, parentName);
}

From source file:Main.java

public static void parseXMLDoc(Element element, Document doc, Map<String, String> oauthResponse) {
    NodeList child = null;/*from ww w .j a va 2s. c  o m*/
    if (element == null) {
        child = doc.getChildNodes();

    } else {
        child = element.getChildNodes();
    }
    for (int j = 0; j < child.getLength(); j++) {
        if (child.item(j).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
            Element childElement = (Element) child.item(j);
            if (childElement.hasChildNodes()) {
                System.out.println(childElement.getTagName() + " : " + childElement.getTextContent());
                oauthResponse.put(childElement.getTagName(), childElement.getTextContent());
                parseXMLDoc(childElement, null, oauthResponse);
            }

        }
    }
}

From source file:Main.java

/** Takes XML node and prints to String.
 *
 * @param node Element to print to String
 * @return XML node as String/*  ww  w . j  ava 2  s.  c om*/
 */
private static void printNode(StringBuffer sBuffer, Node node) {
    if (node.getNodeType() == Node.TEXT_NODE) {
        sBuffer.append(encodeXMLText(node.getNodeValue().trim()));
    } else if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
        sBuffer.append("<![CDATA[");
        sBuffer.append(node.getNodeValue());
        sBuffer.append("]]>");
    } else if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element el = (Element) node;

        sBuffer.append("<").append(el.getTagName());

        NamedNodeMap attribs = el.getAttributes();

        for (int i = 0; i < attribs.getLength(); i++) {
            Attr nextAtt = (Attr) attribs.item(i);
            sBuffer.append(" ").append(nextAtt.getName()).append("=\"").append(nextAtt.getValue()).append("\"");
        }

        NodeList nodes = node.getChildNodes();

        if (nodes.getLength() == 0) {
            sBuffer.append("/>");
        } else {
            sBuffer.append(">");

            for (int i = 0; i < nodes.getLength(); i++) {
                printNode(sBuffer, nodes.item(i));
            }

            sBuffer.append("</").append(el.getTagName()).append(">");
        }
    }
}

From source file:Main.java

protected static Element getChildElement(Element element, String tagName) {
    NodeList childNodes = element.getChildNodes();
    int numChildren = childNodes.getLength();

    for (int i = 0; i < numChildren; i++) {
        Node childNode = childNodes.item(i);
        if (childNode.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }/*from  ww  w  .ja va  2 s  .c o  m*/
        Element childElement = (Element) childNode;

        if (tagName != null) {
            String childTagName = childElement.getTagName();
            if (!childTagName.equals(tagName)) {
                continue;
            }
        }
        return childElement;
    }
    return null;
}

From source file:Main.java

/**
 * Finds the first (direct) child Element with a given tag name.
 * //from ww w.  ja va2 s . c o m
 * @param parent the parent element below which to search the child
 * @param tagName the (tag) name of the desired child element
 * @return the child element if an element of that name existed, or null otherwise
 */
static public Element findFirstChild(Node parent, String tagName) { // Child Element suchen
    if (parent == null)
        return null;
    Node node = parent.getFirstChild();
    while (node != null) { // Find all Element nodes
        if (node.getNodeType() == Node.ELEMENT_NODE) { // check name
            Element elem = (Element) node;
            if (tagName.equalsIgnoreCase(elem.getTagName()))
                return elem; // found
        }
        node = node.getNextSibling();
    }
    return null; // not found!
}

From source file:Main.java

/**
 * Returns the next sibling Element for an element, optionally matching tag names.
 * /* w  w  w .j  a  va 2  s .c o  m*/
 * @param child the element from which to search for a next sibling
 * @param sameName true to retrive the next sibling element of the same name, of false if any name is allowed
 * @return the next sibling element if one exists, or null otherwise
 */
static public Element getNextSiblingElement(Element child, boolean sameName) { // Child Element suchen
    if (child == null)
        return null;
    String name = child.getTagName();
    Node node = child.getNextSibling();
    while (node != null) { // Find all Element nodes
        if (node.getNodeType() == Node.ELEMENT_NODE) { // check name
            Element elem = (Element) node;
            if (sameName && name.equalsIgnoreCase(elem.getTagName()))
                return elem; // found
        }
        node = node.getNextSibling();
    }
    return null; // not found!
}