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:XmlUtil.java

/**
 * Returns a map of all node's attributes. All non-attribute nodes are ignored.
 *///from  ww w  .j a  v  a  2s . c  om
public static Map<String, String> getAllAttributes(Node node) {
    HashMap<String, String> attrs = new HashMap<String, String>();
    NamedNodeMap nmm = node.getAttributes();
    for (int j = 0; j < nmm.getLength(); j++) {
        Node attribute = nmm.item(j);
        if (attribute.getNodeType() != Node.ATTRIBUTE_NODE) {
            continue;
        }
        attrs.put(attribute.getNodeName(), attribute.getNodeValue());
    }
    return attrs;
}

From source file:Main.java

static final void getSetRec(final Node rootNode, final Set result, final Node exclude, final boolean com) {
    //Set result = new HashSet();
    if (rootNode == exclude) {
        return;//  w w  w.j a v  a2 s  . c  om
    }
    switch (rootNode.getNodeType()) {
    case Node.ELEMENT_NODE:
        result.add(rootNode);
        Element el = (Element) rootNode;
        if (el.hasAttributes()) {
            NamedNodeMap nl = ((Element) rootNode).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);
    }
    return;
}

From source file:XmlUtil.java

/**
* Returns attribute value of a node or <code>null</code> if attribute name not found.
* Specified attribute is searched on every call.
* Consider {@link #getAllAttributes(org.w3c.dom.Node)} for better performances.
*/// w  w  w  . java2 s. c  om
public static String getAttributeValue(Node node, String attrName) {
    NamedNodeMap nmm = node.getAttributes();
    for (int j = 0; j < nmm.getLength(); j++) {
        Node attribute = nmm.item(j);
        if (attribute.getNodeType() != Node.ATTRIBUTE_NODE) {
            continue;
        }
        String nodeName = attribute.getNodeName();
        if (nodeName.equals(attrName)) {
            return attribute.getNodeValue();
        }
    }
    return null;
}

From source file:com.mingo.parser.xml.dom.DomUtil.java

/**
 * Transform node attributes to map./*from   w ww.j  a v  a2s. c om*/
 *
 * @param node {@link Node}
 * @return map : key - attribute name; value - attribute value
 */
public static Map<String, String> getAttributes(Node node) {
    Map<String, String> attributes = ImmutableMap.of();
    if (node.hasAttributes()) {
        ImmutableMap.Builder builder = ImmutableMap.builder();
        // get attributes names and values
        NamedNodeMap nodeMap = node.getAttributes();
        for (int i = 0; i < nodeMap.getLength(); i++) {
            Node currentNode = nodeMap.item(i);
            builder.put(currentNode.getNodeName(), currentNode.getNodeValue());
        }
        attributes = builder.build();
    }
    return attributes;
}

From source file:DocWriter.java

private static String nodeWithAttrs(Node node, String indent) {
    StringBuffer sb = new StringBuffer();
    // indent bug - leave out
    //sb.append( indent );
    sb.append("<");
    sb.append(node.getNodeName());//from w ww .j a  v  a  2 s . co m

    NamedNodeMap attrs = node.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        sb.append(" ");
        Node attrNode = attrs.item(i);
        sb.append(attrNode.getNodeName());
        sb.append("=\"");
        sb.append(attrNode.getNodeValue());
        sb.append("\"");
    }

    if (!node.hasChildNodes()) {
        sb.append("/>");
    } else {
        sb.append(">");
    }

    return sb.toString();
}

From source file:Main.java

/**
 * This method will compare the attributes of a given src element with the attributes of a given dest element.
 * Both must contain the same attributes, but you can specify a String array of attribute names whose values
 * are ignored. Both elements must have the ignore attributes, their contents can be different and they will
 * still be considered equal./*from   ww  w. j a  v a  2 s  .c  om*/
 * @param src - the src element whose attributes are to be compared
 * @param dest - the dest element whose attributes are to be compared
 * @param ignore - the string array of attributes whose values are to be ignored during the compare.
 * @return true if the attributes of both nodes meet the criteria of being equal, false otherwise.
 */
private static boolean compareAttributes(Element src, Element dest, String[] ignore) {
    NamedNodeMap srcAttrs = src.getAttributes();

    if (srcAttrs.getLength() != dest.getAttributes().getLength())
        return false;

    for (int ctr = 0; ctr < srcAttrs.getLength(); ctr++) {
        Node srcAttr = srcAttrs.item(ctr);

        String name = srcAttr.getNodeName();
        if (Arrays.binarySearch(ignore, name) < 0) {
            Node destAttr = dest.getAttributeNode(name);
            if (destAttr == null || !srcAttr.isEqualNode(destAttr)) {
                return false;
            }
        }
    }

    return true;
}

From source file:com.bstek.dorado.config.xml.XmlParseException.java

private static String populateErrorMessage(String message, Node node, Resource resource) {
    StringBuffer sb = new StringBuffer();
    if (message != null)
        sb.append(message);/*w w  w.j ava 2s.  c  o m*/

    if (resource != null) {
        sb.append(" - ").append(resource);
    }

    if (node != null) {
        Element element;
        if (node instanceof Element) {
            element = (Element) node;
        } else {
            element = (Element) node.getParentNode();
        }

        if (element != null) {
            sb.append(" - ").append("<" + element.getTagName() + " ");
            NamedNodeMap names = element.getAttributes();
            for (int i = 0; i < 3 && i < names.getLength(); i++) {
                sb.append(populateXmlAttribute(element, names.item(i).getNodeName())).append(" ");
            }
            sb.append("... ");
        }
    }
    return sb.toString();
}

From source file:Main.java

public static void collectNamespaces(Node node, Map namespaces, Map prefixes) {
    if (node == null) {
        throw new IllegalArgumentException("nullArgument: node");
        //                    i18n.getMessage("nullArgument", "node"));
    }/*from w  ww.ja va 2 s.c  o  m*/
    if (namespaces == null) {
        throw new IllegalArgumentException("nullArgument: namespace");
        //                    i18n.getMessage("nullArgument", "namespaces"));
    }
    if (prefixes == null) {
        prefixes = new HashMap();
    }
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            Attr attr = (Attr) attributes.item(i);
            String name = attr.getName();
            String value = attr.getValue();
            if (name.startsWith("xmlns:")) {
                if (namespaces.get(value) == null) {
                    // ns not defined
                    if (prefixes.get(name) != null) {
                        // find unique prefix
                        int j = 1;
                        do {
                            name = "xmlns:ns" + j++;
                        } while (prefixes.get(name) != null);
                    }
                    prefixes.put(name, value);
                    namespaces.put(value, name);
                }
            }
        }
    }
    NodeList children = node.getChildNodes();
    if (children != null) {
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                collectNamespaces(child, namespaces, prefixes);
            }
        }
    }
}

From source file:Main.java

/** Helper method - Converts an XML <I>Element</I> to a <I>String</I> object.
   @param pElement An <I>org.w3c.dom.Element</I> object to be serialized to a <I>String</I>.
   @return <I>String</I> representation of the <I>Element</I>.
 *//*ww w . j av a  2  s.  c  om*/
public static String convertElementToString(Element pElement) {
    // Local variables.
    int nCount = 0;

    // Start the element.
    String strName = pElement.getNodeName();
    String strReturn = "<" + strName;

    // Get the list of attributes.
    NamedNodeMap pNodeMap = pElement.getAttributes();
    nCount = pNodeMap.getLength();

    // Build the attributes.
    for (int i = 0; i < nCount; i++) {
        Attr pAttr = (Attr) pNodeMap.item(i);
        strReturn += " " + pAttr.getNodeName() + "=\"" + pAttr.getNodeValue() + "\"";
    }

    // Get the list of children.
    NodeList pChildren = pElement.getChildNodes();
    nCount = pChildren.getLength();

    // If no children exist, return a single node.
    if (0 == nCount)
        return strReturn + " />";

    // Close node.
    strReturn += ">";

    // Build out the children nodes.
    for (int i = 0; i < nCount; i++) {
        Node pChild = pChildren.item(i);

        if (pChild instanceof CharacterData)
            strReturn += ((CharacterData) pChild).getData();
        else if (pChild instanceof Element)
            strReturn += convertElementToString((Element) pChild);
    }

    return strReturn + "</" + strName + ">";
}

From source file:Main.java

public static String logElement(final Element elem, final int level) {

    String estr = "";
    String indentText = "  ";
    String addIndT = "";

    // add indent
    int ind = 0;//from   w  w w  .j ava  2s  .co m
    while (ind < level) {
        addIndT = addIndT + indentText;
        ind++;

    }
    String name = elem.getNodeName();
    estr = "\n" + addIndT + "<" + name + " ";
    // Attribs
    NamedNodeMap namedNodeMap = elem.getAttributes();
    StringBuilder sb = new StringBuilder(estr);
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Attr att = (Attr) namedNodeMap.item(i);
        sb.append(" " + estr + att.getName() + "=\"" + att.getNodeValue() + "\" ");
    }
    sb.append(">");
    estr = sb.toString();
    NodeList pl = elem.getChildNodes();
    int index = pl.getLength();
    // text nodes
    if (index > 0) {
        int i = 0;
        while (i < index) {
            Node domNode = pl.item(i);
            if ((domNode.getNodeType()) == org.w3c.dom.Node.TEXT_NODE) {
                String Etext = domNode.getNodeValue();
                estr = estr + "\n " + addIndT + addIndT + Etext;
            }
            i++;
        }
    }
    // Child Elements
    if (index > 0) {
        int i = 0;
        while (i < index) {
            Node domNode = pl.item(i);
            if ((domNode.getNodeType()) == org.w3c.dom.Node.ELEMENT_NODE) {
                Element el = (Element) domNode;
                estr = estr + logElement(el, level + 1);
            }
            i++;
        }
    }
    estr = estr + "\n" + addIndT + "</" + name + ">";

    return estr;
}