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:Main.java

/**
 * Checks if is element exists by tag name.
 * @param element the element/*from   ww w. ja  v a 2  s.c  o  m*/
 * @param tagName the tag name
 * @return true, if is element exists by tag name
 */
public static boolean isElementExistsByTagName(final Element element, final String tagName) {

    if (element == null || tagName == null || tagName.isEmpty()) {
        return false;
    }

    // Extract all children on element
    final NodeList res = element.getChildNodes();

    for (int i = 0; i < res.getLength(); i++) {

        final Node node = res.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {

            final Element elem = (Element) node;

            // Check matching with tagname expected
            if (elem.getTagName().equals(tagName)) {
                return true;
            }
        }
    }

    return false;
}

From source file:Main.java

/**
 * Obtains the list of child elements with the specified tag name
 * inside the specific namespace.//from w w  w. j a  va2  s .co  m
 *
 * @param element the root element.
 * @param namespace the child namespace name.
 * @param tagName the child tag name.
 * @return the child element list.
 */
public static List<Element> getChildElements(final Element element, final String namespace,
        final String tagName) {
    final List<Element> elements = getElementList(element);
    final int numElements = elements.size();
    final List<Element> childElements = new ArrayList<Element>();
    for (int i = 0; i < numElements; i++) {
        final Element childElement = elements.get(i);
        String childTagName = childElement.getTagName();
        final String childPrefix = childElement.getPrefix();
        final String childNamespace = (childPrefix != null ? childElement.lookupNamespaceURI(childPrefix)
                : null);

        if (namespace != null) {
            if (!namespace.equals(childNamespace)) {
                continue;
            } else {
                childTagName = childElement.getLocalName();
            }
        }

        if (!childTagName.equals(tagName)) {
            continue;
        }
        childElements.add(childElement);
    }
    return childElements;
}

From source file:DOMCopy.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  v a  2 s.c  om*/
        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 void printElement(Element e) {
    List lst = getChildrenElement(e);
    if (lst.size() == 0) {
        System.out.print(e.getTagName() + "=");
        String value = getNodeText(e);
        if (value.trim().length() > 0)
            System.out.println(value);
        else//from  www  .j a  v a 2 s  . c o  m
            System.out.println();

        NamedNodeMap nn = e.getAttributes();
        for (int i = 0; i < nn.getLength(); i++) {
            Attr attr = (Attr) e.getAttributes().item(i);
            System.out.println(attr.getName() + "=" + attr.getValue());
        }
    } else {
        System.out.println(e.getTagName());
        for (int i = 0; i < lst.size(); i++) {
            printElement((Element) lst.get(i));
        }
    }
}

From source file:Main.java

/**
 * Copy elements from one document to another attaching at the specified
 * element and translating the namespace.
 *
 * @param from         copy the children of this element (exclusive)
 * @param to           where to attach the copied elements
 * @param newNamespace destination namespace
 *
 * @since 8.4/*from w ww .  j  a v a  2  s .com*/
 */
public static void copyDocument(Element from, Element to, String newNamespace) {
    Document doc = to.getOwnerDocument();
    NodeList nl = from.getChildNodes();
    int length = nl.getLength();
    for (int i = 0; i < length; i++) {
        Node node = nl.item(i);
        Node newNode = null;
        if (Node.ELEMENT_NODE == node.getNodeType()) {
            Element oldElement = (Element) node;
            newNode = doc.createElementNS(newNamespace, oldElement.getTagName());
            NamedNodeMap m = oldElement.getAttributes();
            Element newElement = (Element) newNode;
            for (int index = 0; index < m.getLength(); index++) {
                Node attr = m.item(index);
                newElement.setAttribute(attr.getNodeName(), attr.getNodeValue());
            }
            copyDocument(oldElement, newElement, newNamespace);
        } else {
            newNode = node.cloneNode(true);
            newNode = to.getOwnerDocument().importNode(newNode, true);
        }
        if (newNode != null) {
            to.appendChild(newNode);
        }
    }
}

From source file:Main.java

static boolean canBeMerged(Node node1, Node node2, String requiredTagName) {
    if (node1.getNodeType() != Node.ELEMENT_NODE || node2.getNodeType() != Node.ELEMENT_NODE)
        return false;

    Element element1 = (Element) node1;
    Element element2 = (Element) node2;

    if (!equals(requiredTagName, element1.getTagName()) || !equals(requiredTagName, element2.getTagName()))
        return false;

    NamedNodeMap attributes1 = element1.getAttributes();
    NamedNodeMap attributes2 = element2.getAttributes();

    if (attributes1.getLength() != attributes2.getLength())
        return false;

    for (int i = 0; i < attributes1.getLength(); i++) {
        final Attr attr1 = (Attr) attributes1.item(i);
        final Attr attr2;
        if (isNotEmpty(attr1.getNamespaceURI()))
            attr2 = (Attr) attributes2.getNamedItemNS(attr1.getNamespaceURI(), attr1.getLocalName());
        else//from  w ww  .  ja  v  a2 s  .  c  om
            attr2 = (Attr) attributes2.getNamedItem(attr1.getName());

        if (attr2 == null || !equals(attr1.getTextContent(), attr2.getTextContent()))
            return false;
    }

    return true;
}

From source file:Main.java

/**
 * Obtains the first child element with the specified name inside
 * the specified namespace.//  ww  w.j  av a  2  s  .co m
 *
 * @param element the root element.
 * @param namespace the namespace of the child element.
 * @param tagName the child local name.
 * @return the child element.
 */
public static Element getChildElement(final Element element, final String namespace, final String tagName) {
    final NodeList childNodes = element.getChildNodes();
    final int numChildren = childNodes.getLength();

    for (int i = 0; i < numChildren; i++) {
        final Node childNode = childNodes.item(i);
        if (childNode.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }

        final Element childElement = (Element) childNode;
        String childTagName = childElement.getTagName();
        final String childPrefix = childElement.getPrefix();
        final String childNamespace = (childPrefix != null ? childElement.lookupNamespaceURI(childPrefix)
                : null);

        if (namespace != null) {
            if (!namespace.equals(childNamespace)) {
                continue;
            } else {
                childTagName = childElement.getLocalName();
            }
        }

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

From source file:Main.java

private static List<RSSChannel> readChannels(NodeList nodes) {
    List<RSSChannel> result = new ArrayList<RSSChannel>();
    for (int i = 0; i < nodes.getLength(); ++i) {
        Node node = nodes.item(i);
        if (node instanceof Element) {
            Element elem = (Element) node;
            if (elem.getTagName().equalsIgnoreCase("channel")) {
                result.add(readChannel(elem.getChildNodes()));
            }//w ww.  j av a 2s .  c  o  m
        }
    }
    return result;
}

From source file:Main.java

/**
 * Method getFirstChildTag. Return the first child-node which is an element
 * with tagName equal to given tag.//from ww w . j av  a 2s .  co m
 * This method only looks at the direct children of the given node, and
 * doesn't descent deeper into the tree.
 *
 * @param el       Element where to get children from
 * @param tag      Tag to match
 * @return Element The element found, or <code>null</code> if no match
 *                  found.
 */
static public Element getFirstChildTag(Element el, String tag) {
    NodeList nl;
    int len;

    nl = el.getChildNodes();
    len = nl.getLength();
    for (int i = 0; i < len; ++i) {
        Node n = nl.item(i);
        if (n instanceof Element) {
            Element elem = (Element) n;
            if (elem.getTagName().equals(tag)) {
                return elem;
            }
        }
    }
    return null;
}

From source file:Main.java

private static RSSChannel readChannel(NodeList nodes) {
    RSSChannel channel = new RSSChannel();
    for (int i = 0; i < nodes.getLength(); ++i) {
        Node node = nodes.item(i);
        if (node instanceof Element) {
            Element elem = (Element) node;
            if (elem.getTagName().equalsIgnoreCase("title")) {
                channel.setTitle(getText(elem));
            } else if (elem.getTagName().equalsIgnoreCase("link")) {
                channel.setLink(getText(elem));
            } else if (elem.getTagName().equalsIgnoreCase("description")) {
                channel.setDescription(getText(elem));
            } else if (elem.getTagName().equalsIgnoreCase("item")) {
                channel.addItem(readItem(elem.getChildNodes()));
            }/*  w ww  .  j  av a  2s  . co m*/
        }
    }
    return channel;
}