Example usage for org.w3c.dom Element getAttributes

List of usage examples for org.w3c.dom Element getAttributes

Introduction

In this page you can find the example usage for org.w3c.dom Element getAttributes.

Prototype

public NamedNodeMap getAttributes();

Source Link

Document

A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.

Usage

From source file:Main.java

/**
 * @return A String[] of length two, [prefix, URI].
 *///ww w.  j a va  2 s .  co  m
public static String[] getNamespaceDeclaration(Element ele, String prefixHint) {
    String[] ns = new String[2]; // prefix, URI
    NamedNodeMap attribs = ele.getAttributes();

    for (int i = 0; i < attribs.getLength(); i++) {
        Attr attr = (Attr) attribs.item(i);
        if (attr.getName().startsWith("xmlns")) {
            if ((prefixHint != null && attr.getName().endsWith(prefixHint)) || attr.getName().equals("xmlns")) {
                ns[0] = attr.getLocalName(); // prefix
                ns[1] = attr.getTextContent(); // URI

                // catch default namespace change
                if (ns[0] == "xmlns") {
                    ns[0] = UUID.randomUUID().toString();
                }
            }
        }
    }

    if (ns[1] == null) {
        return getNamespaceDeclaration((Element) ele.getParentNode(), prefixHint);
    } else {
        return ns;
    }
}

From source file:Main.java

private static void getSetRec(final Node rootNode, final Set<Node> result, final Node exclude,
        final boolean com) {
    if (rootNode == exclude) {
        return;/*from w w  w . ja  v  a2 s.  c  o m*/
    }
    switch (rootNode.getNodeType()) {
    case Node.ELEMENT_NODE:
        result.add(rootNode);
        Element el = (Element) rootNode;
        if (el.hasAttributes()) {
            NamedNodeMap nl = el.getAttributes();
            for (int i = 0; i < nl.getLength(); i++) {
                result.add(nl.item(i));
            }
        }
        //no return keep working
    case Node.DOCUMENT_NODE:
        for (Node r = rootNode.getFirstChild(); r != null; r = r.getNextSibling()) {
            if (r.getNodeType() == Node.TEXT_NODE) {
                result.add(r);
                while ((r != null) && (r.getNodeType() == Node.TEXT_NODE)) {
                    r = r.getNextSibling();
                }
                if (r == null) {
                    return;
                }
            }
            getSetRec(r, result, exclude, com);
        }
        return;
    case Node.COMMENT_NODE:
        if (com) {
            result.add(rootNode);
        }
        return;
    case Node.DOCUMENT_TYPE_NODE:
        return;
    default:
        result.add(rootNode);
    }
}

From source file:Main.java

/**
 * Gather all the namespaces defined on a node
 *
 * @return/*from   w  ww .j  ava2 s  . c o m*/
 */
public static Iterable<Entry<String, String>> getNamespaces(Element element) {
    TreeMap<String, String> map = new TreeMap<String, String>();
    do {
        NamedNodeMap attributes = element.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Attr attr = (Attr) attributes.item(i);
            final String name = attr.getLocalName();

            if (attr.getPrefix() != null) {
                if ("xmlns".equals(attr.getPrefix()))
                    if (!map.containsKey(name))
                        map.put(name, attr.getValue());
            } else if ("xmlns".equals(name)) {
                if (!map.containsKey(""))
                    map.put("", attr.getValue());
            }
        }
        if (element.getParentNode() == null || element.getParentNode().getNodeType() != Node.ELEMENT_NODE)
            break;
        element = (Element) element.getParentNode();
    } while (true);
    return map.entrySet();
}

From source file:Main.java

private static boolean equalElements(final Element a, final Element b) {
    if (!a.getTagName().equals(b.getTagName())) {
        return false;
    }/*from  w ww  . j a v  a 2 s.c  o  m*/
    final NamedNodeMap attributes = a.getAttributes();
    int customAttributeCounter = 0;
    for (int i = 0, n = attributes.getLength(); i < n; i++) {
        final Node node = attributes.item(i);
        if (node != null && !node.getNodeName().startsWith("_")) {
            if (!node.getNodeName().equals("z") && (b.getAttribute(node.getNodeName()).length() == 0
                    || !b.getAttribute(node.getNodeName()).equals(node.getNodeValue()))) {
                return false;
            }
        } else {
            customAttributeCounter++;
        }
    }
    if (a.getAttributes().getLength() - customAttributeCounter != b.getAttributes().getLength()) {
        return false;
    }
    return true;
}

From source file:Main.java

public static String nodeToString(Node node) {
    String strNode = "";
    Node nodeIt = node;//from   w  w w  .j a va  2 s  .c o m

    if (nodeIt instanceof Element) {
        Element elem = (Element) nodeIt;
        String strElem = "";
        strElem += "<" + elem.getTagName();
        NamedNodeMap attribs = elem.getAttributes();
        int len = attribs.getLength();
        for (int i = 0; i < len; i++) {
            Attr attr = (Attr) attribs.item(i);
            strElem += " " + attr.getName() + "=\"" + attr.getValue() + "\"";
        }
        strElem += ">";
        strNode = strElem + strNode;
    } else if (nodeIt instanceof CharacterData) {
        CharacterData charNode = (CharacterData) nodeIt;
        strNode = charNode.getData() + strNode;
    } else if (nodeIt instanceof Document)
        strNode = strNode; // no hacemos nada
    else
        strNode = nodeIt.getNodeValue() + strNode;

    return strNode;
}

From source file:Main.java

/**
 *  Copy the attributes from n2 to n1./*w w w  . j  a va2s  .c o m*/
 *
 *  @param n1 The source of the attributes.
 *  @param n2 What to copy into.
 */
public static void mergeAttributes(Element n1, Element n2) {
    if ((n1 == null) || (n2 == null)) {
        return;
    }
    NamedNodeMap nnm = n2.getAttributes();
    if (nnm == null) {
        return;
    }
    for (int i = 0; i < nnm.getLength(); i++) {
        Attr attr = (Attr) nnm.item(i);
        n1.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }

}

From source file:DOMEdit.java

private static void outputElement(Element node, String indent) {
    System.out.print(indent + "<" + node.getTagName());
    NamedNodeMap nm = node.getAttributes();
    for (int i = 0; i < nm.getLength(); i++) {
        Attr attr = (Attr) nm.item(i);
        System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\"");
    }//from   ww w .  ja va 2s .  c  o  m
    System.out.println(">");
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++)
        outputloop(list.item(i), indent + TAB);
    System.out.println(indent + "</" + node.getTagName() + ">");
}

From source file:Main.java

/** Takes XML node and prints to String.
 *
 * @param node Element to print to String
 * @return XML node as String/*from  ww  w.j av  a2s.c  om*/
 */
private static void printNode(StringBuffer sBuffer, Node node) {
    if (node.getNodeType() == Node.TEXT_NODE) {
        sBuffer.append(encodeXMLText(node.getNodeValue().trim()));
    } else if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
        sBuffer.append("<![CDATA[");
        sBuffer.append(node.getNodeValue());
        sBuffer.append("]]>");
    } else if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element el = (Element) node;

        sBuffer.append("<").append(el.getTagName());

        NamedNodeMap attribs = el.getAttributes();

        for (int i = 0; i < attribs.getLength(); i++) {
            Attr nextAtt = (Attr) attribs.item(i);
            sBuffer.append(" ").append(nextAtt.getName()).append("=\"").append(nextAtt.getValue()).append("\"");
        }

        NodeList nodes = node.getChildNodes();

        if (nodes.getLength() == 0) {
            sBuffer.append("/>");
        } else {
            sBuffer.append(">");

            for (int i = 0; i < nodes.getLength(); i++) {
                printNode(sBuffer, nodes.item(i));
            }

            sBuffer.append("</").append(el.getTagName()).append(">");
        }
    }
}

From source file:Main.java

/**
 * Get a list of the attribute names of an element.
 * @param e Element to use/*from w w w. j  a v a  2 s .  c  o  m*/
 * @return a list with all the names of the attributes of the node
 */
public static List<String> getAttributeNames(final Element e) {

    if (e == null) {
        return null;
    }

    final List<String> result = new ArrayList<>();

    final NamedNodeMap map = e.getAttributes();

    for (int i = 0; i < map.getLength(); i++) {

        final Node attribute = map.item(i);

        result.add(attribute.getNodeName());
    }

    return result;
}

From source file:Main.java

/**
 * Are elements equal./*from w ww. java  2  s  . c om*/
 * 
 * @param element1
 *            the element1
 * @param element2
 *            the element2
 * @return true, if successful
 */
public static boolean areElementsEqual(Element element1, Element element2) {
    if (!element1.getTagName().equals(element2.getTagName())) {
        return false;
    }
    NamedNodeMap nodeAttrMap = element1.getAttributes();
    NamedNodeMap pathAttrMap = element2.getAttributes();
    if ((nodeAttrMap == null && pathAttrMap == null)
            || (pathAttrMap.getLength() == 0 && nodeAttrMap.getLength() == 0)) {
        return true;
    } else {
        if (element1.hasAttribute("name") && element2.hasAttribute("name")) {
            if (element1.getAttribute("name").equals(element2.getAttribute("name"))) {
                return true;
            }
        } else if (nodeAttrMap != null && pathAttrMap != null
                && (nodeAttrMap.getLength() == pathAttrMap.getLength())) {
            for (int k = 0; k < nodeAttrMap.getLength(); k++) {
                Node nodeAttr = nodeAttrMap.item(k);
                String nodeAttrName = nodeAttr.getNodeName();
                String nodeAttrValue = nodeAttr.getNodeValue();
                if (element2.hasAttribute(nodeAttrName)
                        && nodeAttrValue.equals(element2.getAttribute(nodeAttrName))) {
                    return true;
                }
            }
        }
    }
    return false;
}