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

/**
 * /*from w  ww.j a  v  a2s  .  com*/
 * @param currentNode
 * @param tagName
 * @param attributeValue
 * @return
 */
public static String getTextContentByElementNameANDAttributeValue(Node currentNode, String tagName,
        String attributeValue) {
    String result = "";

    NodeList childNodeList = currentNode.getChildNodes();
    for (int i = 0; i < childNodeList.getLength(); i++) {
        Node childNode = childNodeList.item(i);

        switch (childNode.getNodeType()) {
        case Node.DOCUMENT_NODE:
            break;
        case Node.ELEMENT_NODE:
            Element childElement = (Element) childNodeList.item(i);
            // logger.debug("childElement name : " + childElement.getTagName());
            if (childElement != null && childElement.getNodeName().equals(tagName)) {
                NamedNodeMap attributes = childElement.getAttributes();
                for (int j = 0; j < attributes.getLength(); j++) {
                    Node current = attributes.item(j);

                    if (current.getNodeName().equals("type") && current.getNodeValue().equals(attributeValue)) {
                        result = childElement.getTextContent();
                        break;
                    }
                }
            }
        case Node.TEXT_NODE:
            // logger.debug("textElement name : " + currentNode.getNodeValue());
            break;
        case Node.COMMENT_NODE:
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            break;
        case Node.ENTITY_REFERENCE_NODE:
            break;
        case Node.DOCUMENT_TYPE_NODE:
            break;
        }
    }

    return result;
}

From source file:Main.java

public static ImmutableMap<String, String> attributesOf(Element element) {
    NamedNodeMap map = element.getAttributes();
    ImmutableMap.Builder<String, String> result = ImmutableMap.builder();
    for (int i = 0, size = map.getLength(); i < size; i++) {
        Attr attr = (Attr) map.item(i);
        result.put(attr.getName(), attr.getValue());
    }//from  ww w  .  ja  v a 2  s .c  om
    return result.build();
}

From source file:Main.java

/**
 * Get all attributes of the specified element
 *//*from  ww w. j  av  a  2  s  .c o  m*/
public static ArrayList<Attr> getAllAttributes(Element element) {
    final NamedNodeMap nodeMap = element.getAttributes();
    final ArrayList<Attr> result = new ArrayList<Attr>();

    for (int i = 0; i < nodeMap.getLength(); i++)
        result.add((Attr) nodeMap.item(i));

    return result;
}

From source file:Main.java

public static void printNode(Node node, String indent) {
    String nodeName = node.getNodeName();
    System.out.print(indent);//from   ww w.  j a  v  a2  s .c  o  m

    if (nodeName.equals("#text")) {
        String nodeText = node.getNodeValue();

        if ((nodeText != null) && (nodeText.trim().length() > 0)) {
            System.out.print(nodeText.trim());
        }
    } else {
        System.out.print(nodeName);
    }

    System.out.print(" ");

    if (!nodeName.equals("#text")) {
        NamedNodeMap attrs = node.getAttributes();

        if (attrs != null) {
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = attrs.item(i);
                System.out.print(attr.getNodeName() + "=\"" + attr.getNodeValue() + "\" ");
            }
        }
    }

    NodeList children = node.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
        System.out.println();
        printNode(children.item(i), indent + "\t");
    }
}

From source file:Main.java

private static Map attrbiuteToMap(NamedNodeMap attributes) {
    if (attributes == null)
        return new LinkedHashMap();
    Map result = new LinkedHashMap();
    for (int i = 0; i < attributes.getLength(); i++) {
        result.put(attributes.item(i).getNodeName(), attributes.item(i).getNodeValue());
    }/*from   ww  w.  j a  v a2 s  .c om*/
    return result;
}

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   www .j av  a  2  s . 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

/**
 * Identify variables in attribute values and CDATA contents and replace
 * with their values as defined in the variableDefs map. Variables without
 * definitions are left alone./*  w  w  w  . j a  v  a  2s  .  com*/
 *
 * @param node the node to do variable replacement in
 * @param variableDefs map from variable names to values.
 */
public static void replaceVariables(final Node node, Map<String, String> variableDefs) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        final Element element = (Element) node;
        final NamedNodeMap atts = element.getAttributes();
        for (int i = 0; i < atts.getLength(); i++) {
            final Attr attr = (Attr) atts.item(i);
            attr.setNodeValue(replaceVariablesInString(attr.getValue(), variableDefs));
        }
        break;

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

    default:
        break;
    }

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

From source file:Main.java

/**
 * Returns a Map of all attributes of the given element with the given namespace.
 * Why can we not create our own NamedNodeMaps? This would save trouble.
 * @param element the elment from which to retrieve the attributes.
 * @param namespaceURI the namespace of the attributes to retrieve.
 * @return a Map containing the attributes names and their values.
 *//*from  w  w w. ja  v a 2  s .c  om*/
public static Map getAttributesWithNS(Element element, String namespaceURI) {
    Map result = new HashMap();

    NamedNodeMap attributes = element.getAttributes();

    if (attributes == null)
        return result;

    for (int i = 0; i != attributes.getLength(); i++) {
        Node attribute = attributes.item(i);

        if (namespaceURI == null && attribute.getNamespaceURI() == null) {
            result.put(attribute.getNodeName(), attribute.getNodeValue());
        } else if (attribute.getNamespaceURI() != null && attribute.getNamespaceURI().equals(namespaceURI)) {
            result.put(attribute.getNodeName(), attribute.getNodeValue());
        }
    }

    return result;
}

From source file:Main.java

/**
 * Method to convert a NODE XML to a string.
 * @param n node XML to input./*from  w  w  w .  j  a va  2s. c om*/
 * @return string of the node n.
 */
public static String convertElementToString(Node n) {
    String name = n.getNodeName();
    short type = n.getNodeType();
    if (Node.CDATA_SECTION_NODE == type) {
        return "<![CDATA[" + n.getNodeValue() + "]]&gt;";
    }
    if (name.startsWith("#")) {
        return "";
    }
    StringBuilder sb = new StringBuilder();
    sb.append('<').append(name);
    NamedNodeMap attrs = n.getAttributes();
    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue()).append("\"");
        }
    }
    String textContent;
    NodeList children = n.getChildNodes();
    if (children.getLength() == 0) {
        if ((textContent = n.getTextContent()) != null && !"".equals(textContent)) {
            sb.append(textContent).append("</").append(name).append('>');
            //;
        } else {
            sb.append("/>").append('\n');
        }
    } else {
        sb.append('>').append('\n');
        boolean hasValidChildren = false;
        for (int i = 0; i < children.getLength(); i++) {
            String childToString = convertElementToString(children.item(i));
            if (!"".equals(childToString)) {
                sb.append(childToString);
                hasValidChildren = true;
            }
        }
        if (!hasValidChildren && ((textContent = n.getTextContent()) != null)) {
            sb.append(textContent);
        }
        sb.append("</").append(name).append('>');
    }
    return sb.toString();
}

From source file:Main.java

public static void removeAttributes(Element elem) {
    if (elem.hasAttributes()) {
        NamedNodeMap attributeMap = elem.getAttributes();
        // since node maps are live, hold attributes in a separate collection
        int n = attributeMap.getLength();
        Attr[] attributes = new Attr[n];
        for (int i = 0; i < n; i++)
            attributes[i] = (Attr) attributeMap.item(i);
        // now remove each attribute from the element
        for (int i = 0; i < n; i++)
            elem.removeAttributeNode(attributes[i]);
    }/*from w w w .  j  av a 2  s . c om*/
}