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

/**
 * Gets the child elements of a parent element. Unlike DOM's getElementsByTagName, this does no recursion,
 * uses local name (namespace free) instead of tag name, result is a proper Java data structure and result
 * needs no casting. In other words, this method does not suck unlike DOM.
 * /*from w  w  w  . ja  v  a  2  s .  c o  m*/
 * @param parent the XML parent element
 * @param name name of the child elements, if null then all are returned
 */
public static List<Element> getChildElements(Element parent, String name) {
    List<Element> childElements = new ArrayList<Element>();
    NodeList childNodes = parent.getChildNodes();

    for (int i = 0; i < childNodes.getLength(); i++) {
        // get elements
        if (childNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {

            // match element name
            Element childElement = (Element) childNodes.item(i);
            if (name == null || childElement.getLocalName().equals(name)) {
                childElements.add(childElement);
            }
        }
    }

    return childElements;
}

From source file:Main.java

private static int getChildrenCount(Element parentNode, String namespaceURI, String localName) {
    int counter = 0;

    List<Element> children = getChildElements(parentNode);
    for (Element c : children) {
        if (namespaceURI.equals(c.getNamespaceURI()) && localName.equals(c.getLocalName())) {
            counter++;/*from   www . j a  v  a 2 s  .c  om*/
        }
    }

    return counter;
}

From source file:Main.java

/**
 * Given an element, returns all its direct child elements that have the
 * specified namespace and name.  Use null to indicate the empty namespace.
 *//* www .j  av a2s.c o  m*/
public static List<Element> getElementsNS(Element element, String namespaceURI, String localName) {
    List<Element> elements = new ArrayList<>();
    for (Element candidate : getElements(element)) {
        if (namespaceURI.equals(candidate.getNamespaceURI()) && localName.equals(candidate.getLocalName())) {
            elements.add(candidate);
        }
    }
    return elements;
}

From source file:org.xacml4j.spring.pdp.PolicyDecisionPointDefinitionParser.java

private static BeanDefinitionBuilder parseComponent(Element element) {
    BeanDefinitionBuilder component = element.getLocalName().equals("PolicyIdReference")
            ? BeanDefinitionBuilder.rootBeanDefinition(PolicyIDReferenceFactoryBean.class)
            : BeanDefinitionBuilder.rootBeanDefinition(PolicySetIDReferenceFactoryBean.class);
    String id = element.getTextContent();
    if (StringUtils.hasText(id)) {
        component.addPropertyValue("id", id);
    }//from   w ww.j a  v a  2  s .  c o m
    String version = element.getAttribute("version");
    if (StringUtils.hasText(version)) {
        component.addPropertyValue("version", version);
    }
    String latest = element.getAttribute("latest");
    if (StringUtils.hasText(latest)) {
        component.addPropertyValue("latest", latest);
    }
    String earliest = element.getAttribute("earliest");
    if (StringUtils.hasText(earliest)) {
        component.addPropertyValue("earliest", earliest);
    }
    return component;
}

From source file:Utils.java

/**
 * Build a QName from the element name//from   w w  w .  java2s . c o  m
 * @param el
 * @return
 */
public static QName getQName(Element el) {
    if (el == null) {
        return null;
    } else if (el.getPrefix() != null) {
        return new QName(el.getNamespaceURI(), el.getLocalName(), el.getPrefix());
    } else {
        return new QName(el.getNamespaceURI(), el.getLocalName());
    }
}

From source file:Main.java

public static List<Element> elementsQName(Element element, Set<QName> allowedTagNames) {
    if (element == null || !element.hasChildNodes()) {
        return Collections.emptyList();
    }/* w w  w . java 2  s .  c  o  m*/

    List<Element> elements = new ArrayList<Element>();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element childElement = (Element) child;
            QName childQName = new QName(childElement.getNamespaceURI(), childElement.getLocalName());
            if (allowedTagNames.contains(childQName)) {
                elements.add(childElement);
            }
        }
    }
    return elements;
}

From source file:Main.java

public static Element[] filterChildElements(Element parent, String ns, String lname) {

    List<Element> elems = new ArrayList<Element>();
    NodeList nlst = parent.getChildNodes();
    for (int i = 0, nlen = nlst.getLength(); i < nlen; i++) {
        Node n = nlst.item(i);/*from ww w .ja  v a2  s . c om*/
        if (n instanceof Element) {
            Element e = (Element) n;
            if ((ns == null || ns.equals(e.getNamespaceURI()))
                    && (lname == null || lname.equals(e.getLocalName()))) {
                elems.add(e);
            }
        }
    }
    return elems.toArray(new Element[elems.size()]);
}

From source file:Main.java

public static List<Element> elements(Element element, String tagName) {
    if (element == null || !element.hasChildNodes()) {
        return Collections.emptyList();
    }/*from  www .  j  a  v  a  2s  .com*/

    List<Element> elements = new ArrayList<Element>();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element childElement = (Element) child;
            String childTagName = childElement.getLocalName();

            if (tagName.equals(childTagName))
                elements.add(childElement);
        }
    }
    return elements;
}

From source file:Main.java

/**
 * Convert an XML fragment from one namespace to another.
 *
 * @param from      element to translate
 * @param namespace namespace to be translated to
 * @return the element in the new namespace
 *
 * @since 8.4/*  www . ja  va 2s  .com*/
 */
public static Element translateXML(Element from, String namespace) {
    Element to = from.getOwnerDocument().createElementNS(namespace, from.getLocalName());
    NodeList nl = from.getChildNodes();
    int length = nl.getLength();
    for (int i = 0; i < length; i++) {
        Node node = nl.item(i);
        Node newNode;
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            newNode = translateXML((Element) node, namespace);
        } else {
            newNode = node.cloneNode(true);
        }
        to.appendChild(newNode);
    }
    NamedNodeMap m = from.getAttributes();
    for (int i = 0; i < m.getLength(); i++) {
        Node attr = m.item(i);
        to.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }
    return to;
}

From source file:Main.java

private static void autoQualify(Document document, Element element, Map<String, String> namespaces,
        String parentPrefix) {//from w w w  .j av a 2  s .c o  m
    String localName = element.getLocalName();
    element = renameElement(document, element, localName, namespaces.get(parentPrefix));
    String prefix = parentPrefix;
    if (namespaces.containsKey(localName.toLowerCase())) {
        prefix = localName;
    } else if (genericBranchCoverageTypes.contains(localName.toLowerCase())) {
        prefix = GENERIC_BRANCH_COVERAGE;
    }
    NodeList nodes = element.getChildNodes();
    for (int i = 0, iEnd = nodes.getLength(); i < iEnd; ++i) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            autoQualify(document, (Element) node, namespaces, prefix);
        }
    }
}