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

/**
 * Determines if a node has a particular qualified name.
 * @param node the node/*from  ww w  .ja va2 s.  c  o m*/
 * @param qname the qualified name
 * @return true if the node has the given qualified name, false if not
 */
public static boolean hasQName(Node node, QName qname) {
    return qname.getNamespaceURI().equals(node.getNamespaceURI())
            && qname.getLocalPart().equals(node.getLocalName());
}

From source file:Main.java

public static String getLocalName(Node node) {
    if (node.getPrefix() == null)
        return node.getNodeName();
    else//from   www. j  a  v  a2 s .c  o m
        return node.getLocalName();
}

From source file:Main.java

public static Vector<String> getPropertiesFromXML(Node propNode) {
    Vector<String> properties;
    properties = new Vector<String>();
    NodeList childList = propNode.getChildNodes();

    for (int i = 0; i < childList.getLength(); i++) {
        Node currentNode = childList.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            String nodeName = currentNode.getLocalName();
            String namespace = currentNode.getNamespaceURI();
            // href is a live property which is handled differently
            properties.addElement(namespace + ":" + nodeName);
        }//w  w  w.j  av  a 2  s .  co  m
    }
    return properties;
}

From source file:Main.java

/**
 * Search for an XML element in the direct children of parent only.
 *
 * This compares localName (nodeName if localName is null) to name, and
 * checks the tags namespace with the provided namespace. A
 * <code>null</code> namespace will match any namespace.
 * <p>/*from  w w w.j  a  va 2  s . c o  m*/
 * This is differs from the DOM version by:
 * <ul>
 * <li>not searching recursively</li>
 * <li>returns a single result</li>
 * </ul>
 *
 * @param parent    a parent element
 * @param name      the intended local name
 * @param namespace the intended namespace (or null)
 * @return the one child element with that name, or null if none
 * @throws IllegalArgumentException if there is multiple elements of the
 *                                  same name
 *
 * @since 8.4
 */
public static Element findElement(Element parent, String name, String namespace)
        throws IllegalArgumentException {
    Element result = null;
    NodeList l = parent.getChildNodes();
    int nodeCount = l.getLength();
    for (int i = 0; i < nodeCount; i++) {
        if (l.item(i).getNodeType() == Node.ELEMENT_NODE) {
            Node node = l.item(i);
            String localName = node.getLocalName();
            localName = localName == null ? node.getNodeName() : localName;

            if (name.equals(localName) && (namespace == null || namespace.equals(node.getNamespaceURI()))) {
                if (result == null) {
                    result = (Element) node;
                } else {
                    throw new IllegalArgumentException("more than one element with same name found");
                }
            }
        }
    }
    return result;
}

From source file:Main.java

/**
 * Returns the first child element that matches the given local name and
 * namespace. If no child element is present or no child element matches,
 * <code>null</code> is returned.
 *
 * @param parent/*from   ww  w. ja  v  a2s.  co  m*/
 * @param childLocalName
 * @param childNamespaceURI
 * @return first child element matching the specified names or <code>null</code>.
 */
public static Element getChildElement(Node parent, String childLocalName, String childNamespaceURI) {
    if (parent != null) {
        NodeList children = parent.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE && childLocalName.equals(child.getLocalName())
                    && childNamespaceURI.equals(child.getNamespaceURI())) {
                return (Element) child;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * @param sibling/*from w  ww .j a v  a2s . c  om*/
 * @param uri
 * @param nodeName
 * @param number
 * @return nodes with the constrain
 */
public static Element selectNode(Node sibling, String uri, String nodeName, int number) {
    while (sibling != null) {
        if (nodeName.equals(sibling.getLocalName()) && uri.equals(sibling.getNamespaceURI())) {
            if (number == 0) {
                return (Element) sibling;
            }
            number--;
        }
        sibling = sibling.getNextSibling();
    }
    return null;
}

From source file:Utils.java

/**
 * Starting from a node, find the namespace declaration for a prefix. for a matching namespace
 * declaration./*ww  w  . j av  a2s .c  o  m*/
 * 
 * @param node search up from here to search for namespace definitions
 * @param searchPrefix the prefix we are searching for
 * @return the namespace if found.
 */
public static String getNamespace(Node node, String searchPrefix) {

    Element el;
    while (!(node instanceof Element)) {
        node = node.getParentNode();
    }
    el = (Element) node;

    NamedNodeMap atts = el.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node currentAttribute = atts.item(i);
        String currentLocalName = currentAttribute.getLocalName();
        String currentPrefix = currentAttribute.getPrefix();
        if (searchPrefix.equals(currentLocalName) && XMLNAMESPACE.equals(currentPrefix)) {
            return currentAttribute.getNodeValue();
        } else if (isEmpty(searchPrefix) && XMLNAMESPACE.equals(currentLocalName) && isEmpty(currentPrefix)) {
            return currentAttribute.getNodeValue();
        }
    }

    Node parent = el.getParentNode();
    if (parent instanceof Element) {
        return getNamespace((Element) parent, searchPrefix);
    }

    return null;
}

From source file:Main.java

/**
 * @param root/*from w ww  .  java  2  s.  co  m*/
 * @param uri
 * @param nodeName
 * @return nodes with the constrain
 */
public static Element selectElement(Node node, String uri, String nodeName) {
    while (node != null) {
        if (nodeName.equals(node.getLocalName()) && uri.equals(node.getNamespaceURI()))
            return (Element) node;

        Element located = findElementinChilds(node, uri, nodeName);
        if (located != null) {
            return located;
        }
    }
    return null;
}

From source file:Main.java

public static List<String> getPropertiesFromXML(Node propNode) {
    ArrayList<String> properties;
    properties = new ArrayList<String>();
    NodeList childList = propNode.getChildNodes();

    for (int i = 0; i < childList.getLength(); i++) {
        Node currentNode = childList.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            String nodeName = currentNode.getLocalName();
            String namespace = currentNode.getNamespaceURI();
            // href is a live property which is handled differently
            properties.add(namespace + ":" + nodeName);
        }//w  w  w. j a v a 2  s  .  c om
    }
    return properties;
}

From source file:Main.java

public static List<Node> getChildElementsByTagName(Element ele, String childEleName) {
    NodeList nl = ele.getChildNodes();
    List<Node> childEles = new ArrayList<Node>();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element && childEleName.equals(node.getNodeName())
                || childEleName.equals(node.getLocalName())) {
            childEles.add(node);//  w  ww .ja  va 2  s  . c  o  m
        }
    }
    return childEles;
}