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

/**
 * Gets the name of the node.//from w ww  . j  a va 2s  .  c  om
 * @param node the node
 * @return the name, first trying the local name (getLocalName()), but if null or zero-length, the node name (getNodeName())
 */
public static String nameOf(Node node) {
    if (node != null) {
        String name = node.getLocalName();
        if (name == null || name.length() == 0) {
            name = node.getNodeName();
        }
        return name;
    }
    return null;
}

From source file:Main.java

public static Element getElementByTagName(Element n, String elementName) {
    int sz = n.getChildNodes().getLength();
    ArrayList<Element> elements = new ArrayList<Element>(sz);
    for (int idx = 0; idx < sz; idx++) {
        Node node = n.getChildNodes().item(idx);
        if (node instanceof Element && node.getLocalName().equals(elementName))
            elements.add((Element) node);
    }/*from ww  w .j a v a2  s. co m*/
    if (elements.size() > 0)
        return elements.get(0);
    return null;
}

From source file:Main.java

/**
 * Extracts a {@code Map} of header tag to value from the header node.
 *
 * @param headerNode the header node/* w w w . j  a  v a 2s .  c  o m*/
 * @return a {@code Map} of header tag to value from the header node.
 */
public static Map<String, String> extractHeaderMap(Node headerNode) {
    Map<String, String> headerMap = new HashMap<String, String>();
    for (int i = 0; i < headerNode.getChildNodes().getLength(); i++) {
        Node headerChildNode = headerNode.getChildNodes().item(i);
        headerMap.put(headerChildNode.getLocalName(), headerChildNode.getFirstChild().getNodeValue());
    }
    return headerMap;
}

From source file:Main.java

/**
 * Delete children elements for Element by element name
 *///  w  w  w .jav a  2s  .  c  o m
public static void removeAllChildren(Element node, String elementName) {
    if (node != null) {
        NodeList nl = node.getChildNodes();
        int len = nl.getLength();
        for (int i = 0; i < len; i++) {
            Node childNode = nl.item(i);
            if (childNode != null && childNode.getLocalName() != null
                    && childNode.getLocalName().equals(elementName))
                node.removeChild(childNode);
        }
    }
}

From source file:Main.java

/**
 * Get the non qualified tag name// w ww  .j a  v a2s. c  o m
 *
 * @param element element
 *
 * @return tag name
 */
public static String getLocalName(Node element) {
    String localName = element.getLocalName();
    if (localName != null) {
        return localName;
    }
    String name = element.getNodeName();
    int idx = name.indexOf(":");
    if (idx >= 0) {
        name = name.substring(idx + 1);
    }
    return name;
}

From source file:Main.java

public final static String getEnclosingTagPair(Node node) {
    return getEnclosingTagPair(node.getPrefix(), node.getLocalName());
}

From source file:Main.java

public static void printAttributes(Element element) {
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node node = attributes.item(i);
        System.out.println("## prefix=" + node.getPrefix() + " localname:" + node.getLocalName() + " value="
                + node.getNodeValue());/* w w  w.  j  ava 2  s .  c  om*/
    }
}

From source file:Main.java

static String stringElement(Element row, String name) {
    final NodeList childNodes = row.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        final Node node = childNodes.item(i);
        if (name.equals(node.getLocalName())) {
            String result = node.getTextContent();
            // If content is not plain text then returns name of
            // first child tag
            if (result == null && node.hasChildNodes()) {
                result = node.getFirstChild().getLocalName();
            }/*from  ww w  .j  a  v  a 2  s  .  com*/
            return result;
        }
    }
    return null;
}

From source file:Main.java

static List<Element> findChildren(Element element, String ns, String tag) {
    final List<Element> list = new ArrayList<Element>();
    for (Node node : listOf(element.getChildNodes())) {
        if (tag.equals(node.getLocalName()) && ((ns == null) || node.getNamespaceURI().equals(ns))) {
            list.add((Element) node);
        }//w w  w.ja  v a  2 s  .co m
    }
    return list;
}

From source file:Main.java

/**
 * Compares two DOM nodes for (deep) equality.
 * @param n1//from  w w  w.  j av a2 s  . c o m
 * @param n2
 * @return whether the nodes are equal
 */
public static final boolean compare(Node n1, Node n2) {
    boolean ret = true;
    ret &= n1.getLocalName().equals(n2.getLocalName());
    ret &= n1.getNamespaceURI().equals(n2.getNamespaceURI());
    String text = n1.getTextContent();
    if (text != null) {
        ret &= text.equals(n2.getTextContent());
    }
    NodeList children = n1.getChildNodes();
    ret &= (children.getLength() == n2.getChildNodes().getLength());
    for (int i = 0; ret && i < children.getLength(); i++) {
        final Node child = children.item(i);
        if (child instanceof Element) {
            ret &= compare(child, n2.getChildNodes().item(i));
        }
    }
    return ret;
}