Example usage for org.w3c.dom Node getPrefix

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

Introduction

In this page you can find the example usage for org.w3c.dom Node getPrefix.

Prototype

public String getPrefix();

Source Link

Document

The namespace prefix of this node, or null if it is unspecified.

Usage

From source file:Main.java

public static String getNameSpacePrefix(Node node) {
    String nameSpacePrefix = node.getPrefix();
    if (nameSpacePrefix != null) {
        nameSpacePrefix += ":";
    } else {/*from  w ww. j a v  a2  s. co m*/
        nameSpacePrefix = "";
    }
    return nameSpacePrefix;
}

From source file:Main.java

public static String getLocalName(Node node) {
    if (node.getPrefix() == null)
        return node.getNodeName();
    else//  w  ww .j av  a 2  s  .c o  m
        return 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());//www . jav  a2 s . com
    }
}

From source file:Main.java

public static String getPrefix(Node node) {
    String prefix = node.getPrefix();
    if (prefix == null) {
        prefix = node.getNodeName();// ww w  . j a v  a 2 s .co m

        // If the document is not namespace aware, we must split the attribute name
        // with ':' character.
        int i = prefix.indexOf(':');
        if (i != -1) {
            prefix = prefix.substring(0, i);
        }
    }

    return prefix;
}

From source file:Main.java

public static String lookupNamespaceURI(Node root, String specifiedPrefix) {
    if (root == null) {
        return null;
    }//w ww  . j a  v  a2s .  c  om
    if (root.hasAttributes()) {
        NamedNodeMap nnm = root.getAttributes();
        for (int i = 0; i < nnm.getLength(); i++) {
            Node n = nnm.item(i);
            if (("xmlns".equals(n.getPrefix()) && specifiedPrefix.equals(n.getNodeName()))
                    || ("xmlns:" + specifiedPrefix).equals(n.getNodeName())) {
                return n.getNodeValue();
            }
        }
    }
    return lookupNamespaceURI(root.getParentNode(), specifiedPrefix);
}

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.err.println("## prefix=" + node.getPrefix() + " localname:" + node.getLocalName() + " value="
                + node.getNodeValue());/*from ww  w. j  a  va2s  .  c o  m*/
    }
}

From source file:Main.java

public static boolean nodeHasNameNamespace(Node node, String name, String namespaceURI) {
    String localName = node.getNodeName();

    String pre = node.getPrefix();
    if (pre != null) {
        localName = localName.substring(pre.length() + 1);
    }/*from  w ww . ja v a  2s  .c o m*/

    if (namespaceURI != null) {
        if (!namespaceURI.equals(node.getNamespaceURI())) {
            return false;
        }
    }

    return name.equals(localName);
}

From source file:Main.java

private static Set<String> getTree(Node doc) {
    final Set<String> set = new TreeSet<String>();
    if (doc == null)
        return set;

    String path = new String();
    if (doc.getPrefix() != null && doc.getLocalName() != null) {
        path = new String(doc.getPrefix() + ":" + doc.getLocalName());
        set.add(path);/*  w w w.  jav a2  s. com*/
    }

    final NamedNodeMap attributes = doc.getAttributes();
    for (int i = 0; attributes != null && i < attributes.getLength(); i++) {
        final Node attribute = attributes.item(i);
        String name = attribute.getLocalName();
        if (attribute.getPrefix() != null)
            name = attribute.getPrefix() + ":" + name;
        set.add(path + "/@" + name);
    }

    final NodeList nodes = doc.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        final Node node = nodes.item(i);
        if (node instanceof Element) {
            final Set<String> children = getTree(node);
            for (final String child : children) {
                set.add(path + "/" + child);
            }
        }
    }
    return set;
}

From source file:Utils.java

/**
 * Starting from a node, find the namespace declaration for a prefix. for a matching namespace
 * declaration./*from  www  .j a v  a  2 s  . co 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;
}