Example usage for org.w3c.dom Element getLocalName

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

Introduction

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

Prototype

public String getLocalName();

Source Link

Document

Returns the local part of the qualified name of this node.

Usage

From source file:Main.java

/**
 * @param xml/*  w  w  w.ja  v  a  2  s .c o  m*/
 *            The Element whose QName will be returned.
 * @return The QName of the given Element definition.
 */
public static QName getElementQName(final Element xml) {
    final String uri = xml.getNamespaceURI();
    final String prefix = xml.getPrefix();
    final String name = xml.getLocalName();

    //
    // support for DOM Level 1 - no NS concept
    //
    if (name == null)
        return new QName(xml.getNodeName());

    //
    // prefix is not required, but it CANNOT be null
    //
    if (prefix != null && prefix.length() > 0)
        return new QName(uri, name, prefix);

    return new QName(uri, name);
}

From source file:Main.java

public static List<Element> getElementsByTagNameNS(Element parent, String uri, String name, boolean localOnly) {
    List<Element> ret = new ArrayList<Element>();
    if (!localOnly) {
        NodeList elementList = parent.getElementsByTagNameNS(uri, name);
        for (int i = 0; i < elementList.getLength(); i++) {
            ret.add((Element) elementList.item(i));
        }// w  w  w . j  av  a2  s  .c  o m
    } else {
        NodeList childList = parent.getChildNodes();
        for (int i = 0; i < childList.getLength(); i++) {
            if (childList.item(i).getNodeType() != Node.ELEMENT_NODE)
                continue;
            Element child = (Element) childList.item(i);
            if ((uri.equals("*") || child.getNamespaceURI().equals(uri))
                    && ((child.getLocalName() == null && uri.equals("*") && child.getTagName().equals(name))
                            || child.getLocalName().equals(name)))
                ret.add(child);
        }
    }
    return ret;
}

From source file:Utils.java

private static void findAllElementsByTagNameNS(Element el, String nameSpaceURI, String localName,
        List<Element> elementList) {

    if (localName.equals(el.getLocalName()) && nameSpaceURI.contains(el.getNamespaceURI())) {
        elementList.add(el);/*w  w  w . j av  a2 s .c o  m*/
    }
    Element elem = getFirstElement(el);
    while (elem != null) {
        findAllElementsByTagNameNS(elem, nameSpaceURI, localName, elementList);
        elem = getNextElement(elem);
    }
}

From source file:Main.java

public static List<Element> getChildElementsByName(Element element, String localName) {
    List<Element> elements = new ArrayList<Element>();

    for (Element e : getChildElements(element)) {
        // DOM Level 1 API (document.createElement) creates elements
        // that have an empty localname
        if (e.getLocalName() == null && localName.equals(e.getNodeName())) {
            elements.add(e);//from w w w  .  j av  a 2  s.c  o  m
        } else if (localName.equals(e.getLocalName())) {
            elements.add(e);
        }
    }

    return elements;
}

From source file:Utils.java

/**
 * Return the first element child with the specified qualified name.
 * /*from   w ww  .j av  a  2  s  .  co m*/
 * @param parent
 * @param ns
 * @param lp
 * @return
 */
public static Element getFirstChildWithName(Element parent, String ns, String lp) {
    for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) {
        if (n instanceof Element) {
            Element e = (Element) n;
            String ens = (e.getNamespaceURI() == null) ? "" : e.getNamespaceURI();
            if (ns.equals(ens) && lp.equals(e.getLocalName())) {
                return e;
            }
        }
    }
    return null;
}

From source file:Utils.java

/**
 * Return child elements with specified name.
 * /*from ww w .  j av a 2s.  co  m*/
 * @param parent
 * @param ns
 * @param localName
 * @return
 */
public static List<Element> getChildrenWithName(Element parent, String ns, String localName) {
    List<Element> r = new ArrayList<Element>();
    for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) {
        if (n instanceof Element) {
            Element e = (Element) n;
            String eNs = (e.getNamespaceURI() == null) ? "" : e.getNamespaceURI();
            if (ns.equals(eNs) && localName.equals(e.getLocalName())) {
                r.add(e);
            }
        }
    }
    return r;
}

From source file:Main.java

public static List<Element> elementsQName(final Element element, final Set<QName> allowedTagNames) {
    List<Element> elements = null;
    final NodeList nodeList = element.getChildNodes();
    if (nodeList != null) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            final Node child = nodeList.item(i);
            if (Element.class.isAssignableFrom(child.getClass())) {
                final Element childElement = (Element) child;
                final QName childElementQName = new QName(childElement.getNamespaceURI(),
                        childElement.getLocalName());
                if (allowedTagNames.contains(childElementQName)) {
                    if (elements == null) {
                        elements = new ArrayList<Element>();
                    }/*  w ww  . j  a  v  a  2s . c  om*/
                    elements.add(childElement);
                }
            }
        }
    }
    return elements;
}

From source file:Main.java

public static String getTagLocalName(final Element element) {
    if (element == null) {
        return null;
    }/*from w w  w . j av  a2 s .c  om*/
    final String localName = element.getLocalName();
    if (localName != null) {
        return localName;
    }
    return element.getTagName();
}

From source file:Main.java

public static List<Element> getElements(Node parent, String namespaceURI, String localName) {
    List<Element> ret = new ArrayList<Element>();
    for (Node node : getNonEmptyChildNodes(parent)) {
        if (!(node instanceof Element))
            continue;
        Element elm = (Element) node;
        if ((namespaceURI == null && elm.getNamespaceURI() == null
                || namespaceURI != null && namespaceURI.equals(elm.getNamespaceURI()))
                && elm.getLocalName().equals(localName)) {
            ret.add((Element) node);
        }//from  ww w. j a  va  2 s. c o  m
    }
    return ret;
}

From source file:Main.java

/**
 * Append a child element to the parent at the specified location.
 *
 * Starting with a valid document, append an element according to the schema
 * sequence represented by the <code>order</code>. All existing child
 * elements must be include as well as the new element. The existing child
 * element following the new child is important, as the element will be
 * 'inserted before', not 'inserted after'.
 *
 * @param parent parent to which the child will be appended
 * @param el     element to be added// w  ww. j  a  v  a 2 s  .c o m
 * @param order  order of the elements which must be followed
 * @throws IllegalArgumentException if the order cannot be followed, either
 *                                  a missing existing or new child element
 *                                  is not specified in order
 *
 * @since 8.4
 */
public static void appendChildElement(Element parent, Element el, String[] order)
        throws IllegalArgumentException {
    List<String> l = Arrays.asList(order);
    int index = l.indexOf(el.getLocalName());

    // ensure the new new element is contained in the 'order'
    if (index == -1) {
        throw new IllegalArgumentException(
                "new child element '" + el.getLocalName() + "' not specified in order " + l); // NOI18N
    }

    List<Element> elements = findSubElements(parent);
    Element insertBefore = null;

    for (Element e : elements) {
        int index2 = l.indexOf(e.getLocalName());
        // ensure that all existing elements are in 'order'
        if (index2 == -1) {
            throw new IllegalArgumentException(
                    "Existing child element '" + e.getLocalName() + "' not specified in order " + l); // NOI18N
        }
        if (index2 > index) {
            insertBefore = e;
            break;
        }
    }

    parent.insertBefore(el, insertBefore);
}