Example usage for org.w3c.dom NamedNodeMap item

List of usage examples for org.w3c.dom NamedNodeMap item

Introduction

In this page you can find the example usage for org.w3c.dom NamedNodeMap item.

Prototype

public Node item(int index);

Source Link

Document

Returns the indexth item in the map.

Usage

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  ww  w. java2s  .  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

public static String nodeToString(Node node) {
    String strNode = "";
    Node nodeIt = node;/*from  www  .  j a  v  a2 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

/**
 * Find all attribute in a node with specific prefix.
 *
 * @param node         the note that contains the attributes
 * @param prefix       the prefix to search for
 * @param removePrefix true: removes the prefix from attribute name (name is the key of resulting map)
 * @return the values or a empty map//from ww w . ja v  a2 s . c o m
 */
public static HashMap<String, String> getAttributes(Node node, String prefix, boolean removePrefix) {
    final HashMap<String, String> result = new HashMap<>();

    final NamedNodeMap attributes = node.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        final Node att = attributes.item(i);
        String name = att.getNodeName();
        if (name.startsWith(prefix)) {
            if (removePrefix) {
                name = name.substring(prefix.length());
            }
            result.put(name, att.getNodeValue());
        }
    }
    return result;
}

From source file:Main.java

/**
 * Compare necessary namespace declarations between original and proposed
 * document, if namespaces in the original are missing compared to the
 * proposed, we add them to the original.
 * //www. j a v a 2  s.  c om
 * @param original document as read from the file system
 * @param proposed document as determined by the JspViewManager
 * @return true if the document was adjusted, otherwise false
 */
private static boolean checkNamespaces(final Document original, final Document proposed) {
    boolean originalDocumentChanged = false;
    final NamedNodeMap nsNodes = proposed.getDocumentElement().getAttributes();
    for (int i = 0; i < nsNodes.getLength(); i++) {
        if (0 == original.getDocumentElement().getAttribute(nsNodes.item(i).getNodeName()).length()) {
            original.getDocumentElement().setAttribute(nsNodes.item(i).getNodeName(),
                    nsNodes.item(i).getNodeValue());
            originalDocumentChanged = true;
        }
    }
    return originalDocumentChanged;
}

From source file:Main.java

public static Map<String, String> getAttributes(Node node) {
    NamedNodeMap map = node.getAttributes();
    Map<String, String> result = Maps.newHashMap();
    if (map != null) {
        for (int i = 0; i < map.getLength(); i++) {
            result.put(map.item(i).getNodeName(), map.item(i).getNodeValue());
        }//from w w w  .  ja va  2  s .  c  o m
    }
    return result;
}

From source file:Main.java

private static boolean equalElements(final Element a, final Element b) {
    if (!a.getTagName().equals(b.getTagName())) {
        return false;
    }/*w w w. jav a  2s.  com*/
    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

/**
 * Changes the tag name of an element./*from   ww w.  j  a  v a 2s .c o m*/
 * 
 * @param elem Element which name should be changed
 * @param newName new tag name of the element
 * @return true if the name was changed successfully or false otherwise
 */
static public boolean changeTagName(Element elem, String newName) {
    if (elem == null)
        return false; // not Found!
    Document doc = elem.getOwnerDocument();
    Element newElem = doc.createElement(newName);

    // Copy the attributes to the new element
    NamedNodeMap attrs = elem.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr2 = (Attr) doc.importNode(attrs.item(i), true);
        newElem.getAttributes().setNamedItem(attr2);
    }

    // Copy all Child Elements
    for (Node node = elem.getFirstChild(); node != null; node = node.getNextSibling())
        newElem.appendChild(node.cloneNode(true));
    // insert
    Node parent = elem.getParentNode();
    parent.replaceChild(newElem, elem);
    return true;
}

From source file:Main.java

/**
 * Get a node from a list with a specific attribute value.
 *
 * @param nodeList the list of nodes//from  w w w.  j a  va2s  .  c  o m
 * @param name the name of the attribute to check against
 * @param value the value to match
 * @return the node with matching attribute value
 */
public static Node getNodeWithGivenValue(final NodeList nodeList, final String name, final String value) {

    Node node = null;

    for (int i = 0; i < nodeList.getLength(); i++) {
        final NamedNodeMap map = nodeList.item(i).getAttributes();
        if (map != null) {
            for (int j = 0; j < map.getLength(); j++) {
                if ((name.equals(map.item(j).getNodeName())) && (value.equals(map.item(j).getTextContent()))) {

                    node = nodeList.item(i);
                }
            }
        }
    }

    return node;
}

From source file:Main.java

private static void appendAttributes(Node node, StringBuffer sb) {
    if (node instanceof Element) {
        NamedNodeMap nodeMap = node.getAttributes();
        for (int i = 0; i < nodeMap.getLength(); i++) {
            sb.append(' ');
            sb.append(nodeMap.item(i).getNodeName());
            sb.append('=');
            sb.append('"');
            sb.append(nodeMap.item(i).getNodeValue());
            sb.append('"');
        }/*  w  ww  .  j  a va  2  s  . c om*/
    }
}

From source file:Main.java

public static int getAllChildren(Node start, int offset) {
    //String spacers  = "                                                         ";
    String tagOpen = "<";
    String tagClose = ">";
    String tagEndOpen = "</";
    String tagEndClose = ">";
    String tagName = start.getNodeName();
    String tagValue = (start.getNodeValue() == null ? "" : start.getNodeValue());

    if (start.getNodeName().trim().equals("#text")) {
        tagOpen = "";
        tagClose = "";
        tagName = "";
        tagEndOpen = "";
        tagEndClose = "";
    } else if (start.getNodeName().trim().equals("#comment")) {
        tagOpen = "<!--";
        tagClose = "";
        tagName = "";
        tagEndOpen = "";
        tagEndClose = "-->";
    }//from  w w w . j a v  a2 s  .co  m
    if (offset == 0)
        HTMLString = "";

    HTMLString += tagOpen + tagName;

    if (start.getNodeType() == Element.ELEMENT_NODE) {
        NamedNodeMap startAttr = start.getAttributes();
        for (int i = 0; i < startAttr.getLength(); i++) {
            Node attr = startAttr.item(i);
            HTMLString += " " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\"";
        }
    }
    HTMLString += tagClose + tagValue;

    for (Node child = start.getFirstChild(); child != null; child = child.getNextSibling()) {
        getAllChildren(child, offset + 1);
    }

    //if (offset > 0 && tagName.length() > 0) {
    if (tagName.length() > 0 || start.getNodeName().trim().equals("#comment")) {
        HTMLString += tagEndOpen + tagName + tagEndClose;
    }

    return ++offset;
}