Example usage for org.w3c.dom Node getLocalName

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

Introduction

In this page you can find the example usage for org.w3c.dom Node 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

public static List<Element> elements(final Element father, final String ns, final String localName) {
    final List<Element> matchingElements = new ArrayList<Element>();
    final NodeList nl = father.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        final Node n = nl.item(i);
        if (n instanceof Element && n.getLocalName() != null && n.getLocalName().equals(localName)
                && n.getNamespaceURI() != null && n.getNamespaceURI().equals(ns)) {
            matchingElements.add((Element) n);
        }//from w  w  w  . ja va  2  s.  c  o m
    }
    return matchingElements;
}

From source file:Main.java

/**
 * @return a list of all child nodes with this name
 *//*w  ww. j  a v  a  2 s .c  om*/
public static ArrayList<Node> getChildNodes(Node node, String name) {
    ArrayList<Node> results = new ArrayList<Node>();

    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node temp = nodeList.item(i);
        if (temp.getNodeType() != Node.ELEMENT_NODE)
            continue;
        if (name.equals(temp.getLocalName()) || name.equals(temp.getNodeName())) {
            results.add(temp);
        }
    }

    return (results);
}

From source file:Main.java

/**
 * Get the first found child element with the provided local name that is a
 * direct child the provided element.//  w  ww  .  ja va  2 s. c  om
 * 
 * @param parent
 *            The parent element
 * @param name
 *            The name of the child element to find
 * @return The first found child element, null if no matching children
 */
public static Element getFirstChildElementByLocalName(Element parent, String localName) {
    assertNotNull(parent);
    NodeList children = parent.getChildNodes();
    Node node;
    for (int i = 0; i < children.getLength(); i++) {
        node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getLocalName().equals(localName)) {
            return (Element) node;
        }
    }

    return null;
}

From source file:Main.java

/**
 * Creates a map of tag name to dirty header XML from the SOAP header node.
 *
 * @param headerNode the header node to extract all XML from
 * @return a map of tag name to dirty header XML
 *///  w ww  .j  a  v  a2 s  .c o  m
private static Map<String, String> createTagToDirtyXmlMap(Node headerNode) {
    Map<String, String> dirtyXmlMap = new HashMap<String, String>();
    for (int i = 0; i < headerNode.getChildNodes().getLength(); i++) {
        Node headerChildNode = headerNode.getChildNodes().item(i);
        if (Arrays.asList(SENSITIVE_HEADER_TAGS).contains(headerChildNode.getLocalName())) {
            dirtyXmlMap.put(headerChildNode.getLocalName(), headerChildNode.toString());
        }
    }
    return dirtyXmlMap;
}

From source file:Main.java

/**
 * @return a list of all child node text contents with this name
 *//*from w  w w. j a v  a  2  s.c  om*/
public static ArrayList<String> getChildNodesTextContents(Node node, String name) {
    ArrayList<String> results = new ArrayList<String>();

    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node temp = nodeList.item(i);
        if (temp.getNodeType() != Node.ELEMENT_NODE)
            continue;
        if (name.equals(temp.getLocalName()) || name.equals(temp.getNodeName())) {
            results.add(temp.getTextContent());
        }
    }

    return (results);
}

From source file:Main.java

/**
 * only returns the first instance of this child node
 * @param node//from   w w  w . j  ava 2s .  co  m
 * @param localName
 * @return
 */
public static Node getChildNode(Node node, String name) {
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node temp = nodeList.item(i);
        if (temp.getNodeType() != Node.ELEMENT_NODE)
            continue;
        if (name.equals(temp.getLocalName()) || name.equals(temp.getNodeName()))
            return (temp);
    }

    return (null);
}

From source file:Main.java

/**
 * Access all immediate child elements inside the given Element
 *
 * @param element the starting element, cannot be null.
 * @param namespaceURI name space of the child element to look for,
 * cannot be null. Use "*" to match all namespaces
 * @param elemName the name of the child element to look for,
 * cannot be null./* w  w w. j a v  a2s  .  c  om*/
 * @return array of all immediate child element inside element, or
 * an array of size zero if no child elements are found.
 */
public static Element[] getChildElements(Element element, String namespaceURI, String elemName) {
    NodeList list = element.getChildNodes();
    int len = list.getLength();
    ArrayList<Node> array = new ArrayList<Node>(len);

    for (int i = 0; i < len; i++) {
        Node n = list.item(i);

        if (n.getNodeType() == Node.ELEMENT_NODE) {
            if (elemName.equals(n.getLocalName())
                    && ("*".equals(namespaceURI) || namespaceURI.equals(n.getNamespaceURI()))) {
                array.add(n);
            }
        }
    }
    Element[] elems = new Element[array.size()];

    return (Element[]) array.toArray(elems);
}

From source file:Main.java

/**
 * Get all child elements with the provided local name that are direct
 * children the provided element.//from ww w  .  j av  a  2s . com
 * 
 * @param parent
 *            The parent element
 * @param name
 *            The local name of the child elements to find
 * @return The list with child elements, empty list if no matching children
 */
public static Collection<Element> getChildElementsByLocalName(Element parent, String name) {
    assertNotNull(parent);
    Collection<Element> childList = new ArrayList<Element>();

    NodeList children = parent.getChildNodes();
    Node node;
    for (int i = 0; i < children.getLength(); i++) {
        node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getLocalName().equals(name)) {
            childList.add((Element) node);
        }
    }

    return childList;
}

From source file:DomUtils.java

/**
 * Namespace-aware equals comparison. Returns <code>true</code> if either
 * {@link Node#getLocalName} or {@link Node#getNodeName} equals <code>desiredName</code>,
 * otherwise returns <code>false</code>.
 *///from  w  w  w  .j a va 2  s .  c o m
public static boolean nodeNameEquals(Node node, String desiredName) {
    return (desiredName.equals(node.getNodeName()) || desiredName.equals(node.getLocalName()));
}

From source file:Main.java

/**
 * check if given name is equal to name of the element (with and without namespace)
 * @param node/* w w w  .  jav a2 s .co m*/
 * @param k
 * @param caseSensitive
 * @return
 */
public static boolean nameEqual(Node node, String name, boolean caseSensitive) {
    if (name == null)
        return false;
    if (caseSensitive) {
        return name.equals(node.getNodeName()) || name.equals(node.getLocalName());
    }
    return name.equalsIgnoreCase(node.getNodeName()) || name.equalsIgnoreCase(node.getLocalName());
}