Example usage for org.w3c.dom NamedNodeMap getLength

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

Introduction

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

Prototype

public int getLength();

Source Link

Document

The number of nodes in this map.

Usage

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 = "-->";
    }/*w ww  . java  2 s.  c  o  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;
}

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;/*w  w  w  . j ava2  s.co 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 Map<String, String> getNamespaceMap(Document document) {
    Map<String, String> nsMap = new HashMap<String, String>();

    NamedNodeMap map = document.getDocumentElement().getAttributes();
    for (int j = 0; j < map.getLength(); j++) {
        Node n = map.item(j);//from  www .  ja v  a 2s .  co m
        String attrName = ((Attr) n).getName();
        String attrValue = ((Attr) n).getValue();
        if (attrName != null && attrValue != null) {
            if (attrName.trim().startsWith("xmlns:")) {
                nsMap.put(attrValue, attrName.substring(6));
            }
        }
    }

    return nsMap;
}

From source file:Main.java

/**
 * Rename an element in a DOM document. It happens to involves a node
 * replication.//w ww .  j  a  v  a  2s.c om
 * 
 * @param document
 *            The document containing the element (some way to verify
 *            that?).
 */
public static Element renameElement(Document document, Element element, String newName, String namespace) {
    if (namespace == null) {
        throw new IllegalArgumentException("No namespace provided for element " + element);
    }
    Element newElement = document.createElementNS(namespace, newName);
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0, iEnd = attributes.getLength(); i < iEnd; i++) {
        Attr attr2 = (Attr) document.importNode(attributes.item(i), true);
        newElement.getAttributes().setNamedItem(attr2);
    }
    while (element.hasChildNodes()) {
        newElement.appendChild(element.getFirstChild());
    }
    element.getParentNode().replaceChild(newElement, element);
    return newElement;
}

From source file:Main.java

/**
 * Extract default values of variables defined in the DOM subtree below node.
 * Default values are used to populate the variableDefs map.  Variables
 * already defined in this map will NOT be modified.
 *
 * @param node root node of DOM subtree to extract default values from.
 * @param variableDefs map which default values will be added to.
 *//* w  w  w  . j a v a 2  s .c  om*/
public static void extractVariableDefaults(final Node node, Map<String, String> variableDefs) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        final Element element = (Element) node;
        final NamedNodeMap attrs = element.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Attr attr = (Attr) attrs.item(i);
            extractVariableDefaultsFromString(attr.getValue(), variableDefs);
        }
        break;

    case Node.CDATA_SECTION_NODE:
        String content = node.getTextContent();
        extractVariableDefaultsFromString(content, variableDefs);
        break;

    default:
        break;
    }

    final NodeList children = node.getChildNodes();
    for (int childIndex = 0; childIndex < children.getLength(); childIndex++)
        extractVariableDefaults(children.item(childIndex), variableDefs);
}

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());
        }//  ww  w. jav a  2 s  .c  o  m
    }
    return result;
}

From source file:MainClass.java

static void listNodes(Node node, String indent) {
    String nodeName = node.getNodeName();
    System.out.println(indent + " Node: " + nodeName);
    short type = node.getNodeType();
    System.out.println(indent + " Node Type: " + nodeType(type));
    if (type == TEXT_NODE) {
        System.out.println(indent + " Content is: " + ((Text) node).getWholeText());
    } else if (node.hasAttributes()) {
        System.out.println(indent + " Element Attributes are:");
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Attr attribute = (Attr) attrs.item(i);
            System.out.println(indent + " " + attribute.getName() + " = " + attribute.getValue());
        }//from  w  w  w . j  a  v  a  2s.c om
    }

    NodeList list = node.getChildNodes();
    if (list.getLength() > 0) {
        System.out.println(indent + " Child Nodes of " + nodeName + " are:");
        for (int i = 0; i < list.getLength(); i++) {
            listNodes(list.item(i), indent + "  ");
        }
    }
}

From source file:Main.java

private static Node convertFromNamespaceForm(final Node node) {
    if (node instanceof Element) {
        final Document document = node.getOwnerDocument();
        final Element newElement = document.createElementNS(null, node.getLocalName());
        final NodeList children = node.getChildNodes();

        for (int i = 0, n = children.getLength(); i < n; i++) {
            final Node oldChildNode = children.item(i);
            final Node newChildNode = convertFromNamespaceForm(oldChildNode);

            newElement.appendChild(newChildNode);
        }/*from   www  .j  a  v  a2s  .  co  m*/

        final NamedNodeMap attributes = node.getAttributes();

        for (int i = 0, n = attributes.getLength(); i < n; i++) {
            final Attr attr = (Attr) attributes.item(i);
            final String attrQualifiedName = attr.getNodeName();
            final String attrLocalName = attr.getLocalName();

            if (!attrQualifiedName.equals(XMLNS) && !attrQualifiedName.startsWith(XMLNS_COLON)
                    && !attrLocalName.equals(XSI_SCHEMA_LOCATION_ATTR)) {
                newElement.setAttributeNS(null, attrLocalName, attr.getValue());
            }
        }

        return newElement;
    } else {
        return node.cloneNode(true);
    }
}

From source file:Main.java

public static void walkNodes(Node nodeIn, StringBuffer sb, String sPad) {
    if (nodeIn == null)
        return;//  w w  w  .  j a  v  a 2s  .com
    NamedNodeMap map = nodeIn.getAttributes();
    if (map != null)
        for (int i = 0; i < map.getLength(); i++) {
            Node n = map.item(i);
            if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
                sb.append(sPad + "   Attribute:" + n.getNodeName() + " = " + n.getNodeValue() + '\n');
            }
        }
    NodeList nodes = nodeIn.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            sb.append(sPad + "Element: " + n.getNodeName() + '\n');
        }
        if (n.getNodeType() == Node.TEXT_NODE) {
            sb.append(sPad + "   Value: = " + n.getNodeValue() + '\n');
        }
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            walkNodes(n, sb, sPad + "   ");
        }
    }
}

From source file:Main.java

/**
 * Puts the node attributes (if it has any) into HashMap<String,String>
 * //from   w  ww. java2s  .com
 * @param node : XML node to look for its attributes
 * @return Hashmap<String,String> of the node attributes
 */
public static HashMap<String, String> getAttributes(Node node) {
    String attrName = null;
    String attrValue = null;
    HashMap<String, String> attributesMap = new HashMap<String, String>();
    NamedNodeMap attributes = node.getAttributes();
    for (int attrIndex = 0; attributes != null && attrIndex < attributes.getLength(); attrIndex++) {
        attrName = attributes.item(attrIndex).getNodeName();
        attrValue = attributes.item(attrIndex).getNodeValue();
        attributesMap.put(attrName, attrValue);
    }
    return attributesMap.size() == 0 ? null : attributesMap;
}