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

public static void printNode(Node node, String indent) {
    String nodeName = node.getNodeName();
    System.out.print(indent);/*from   w ww  .j ava2s.co 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

/**
 * Puts the node attributes (if it has any) into HashMap<String,String>
 * /*w ww .ja va2  s.c  om*/
 * @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;
}

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   www  . j a v a 2s.co  m
    }

    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

/**
 * Method to convert a NODE XML to a string.
 * @param n node XML to input./*from  www. j  av a 2 s .c  o m*/
 * @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

/** Returns a sorted list of attributes.
 *
 *  @param attrs A map of attributes//from   w  ww. j av a 2  s  . c o m
 *  @return A sorted array of attributes 
 */
protected static Attr[] sortAttributes(NamedNodeMap attrs) {
    int len = (attrs != null) ? attrs.getLength() : 0;
    Attr array[] = new Attr[len];
    for (int i = 0; i < len; i++) {
        array[i] = (Attr) attrs.item(i);
    }
    /*    
    for (int i = 0; i < len - 1; i++) {
      String name  = array[i].getNodeName();
      int index = i;
      for (int j = i + 1; j < len; j++) {
    String curName = array[j].getNodeName();
    if (curName.compareTo(name) < 0) {
      name  = curName;
      index = j;
    }
      }
      if (index != i) {
    Attr temp    = array[i];
    array[i]     = array[index];
    array[index] = temp;
      }
    }
     */
    return (array);
}

From source file:Main.java

protected static Map<String, String> createAttributeTable(Element e) {
    Map<String, String> attributeTable = new HashMap<>();
    NamedNodeMap attrs = e.getAttributes();
    if (attrs != null) {
        int numAttrs = attrs.getLength();
        for (int i = 0; i < numAttrs; i++) {
            Node na = attrs.item(i);
            if (na.getNodeType() != Node.ATTRIBUTE_NODE) {
                continue;
            }//from  ww w  .ja v a2  s  .c  o  m
            Attr a = (Attr) na;
            attributeTable.put(a.getName(), a.getValue());
        }
    }
    return attributeTable;
}

From source file:Main.java

/**
 * Output a DOM node including children using a log4j logger.
 * If logger is null it will just output to system out.
 * It will indent by the number of tabs passed in.
 * This method recursively calls itself to output
 * children nodes.//from  w  ww.  ja v  a  2s  .  co m
 * 
 * @param logger
 * @param n
 * @param tabs
 */
public static void outputNode(Logger logger, Node n, int tabs) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < tabs; i++) {
        sb.append("\t");
    }
    sb.append("<" + n.getNodeName());

    if (n.hasAttributes()) {
        NamedNodeMap nnMap = n.getAttributes();
        for (int i = 0; i < nnMap.getLength(); i++) {
            Node att = nnMap.item(i);
            sb.append(" " + att.getNodeName() + "=\"" + att.getNodeValue() + "\"");
        }
    }
    sb.append(">");
    sb = printBuffer(logger, sb, true);

    for (int i = 0; i < tabs + 1; i++) {
        sb.append("\t");
    }
    sb.append(n.getNodeValue());
    sb = printBuffer(logger, sb, true);

    if (n.hasChildNodes()) {
        NodeList nodes = n.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            outputNode(nodes.item(i), tabs + 1);
        }
    }
    for (int i = 0; i < tabs; i++) {
        sb.append("\t");
    }
    sb.append("</" + n.getNodeName() + ">");
    sb = printBuffer(logger, sb, true);
}

From source file:Main.java

public static List<Node> getAttributes(Node node) {
    NamedNodeMap attrs = node.getAttributes();
    int attrCount = attrs.getLength();
    List<Node> nodes = new ArrayList<Node>(attrCount);
    for (int i = 0; i < attrCount; i++) {
        nodes.add(attrs.item(i));
    }/* w ww.  j  a va 2  s  .  c o m*/
    return nodes;
}

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 . j ava 2  s .  c  om
    return result.build();
}

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.
 *///w  w w.ja va 2s .  com
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;
}