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

/**
 * Method get all attributes in a xml element.
 * @param el xml element on input.//from   ww  w  .  j  a v  a 2  s.  c om
 * @return array list of attributes.
 */
public static List<Attr> getAllAttributes(Element el) {
    List<Attr> listAttr = new ArrayList<>();
    NamedNodeMap attributes = el.getAttributes();//get map containing the attributes of this node
    int numAttrs = attributes.getLength(); //get the number of nodes in this map
    for (int i = 0; i < numAttrs; i++) {
        Attr attr = (Attr) attributes.item(i);
        listAttr.add(attr);
    }
    return listAttr;
}

From source file:Main.java

protected static void writeXMLwalkTree(Node node, int indent, PrintWriter out) {
    if (node == null)
        throw new NullPointerException("Null node passed to writeXMLwalkTree()");
    if (node.hasChildNodes()) {
        if (node instanceof Element) {
            Element elem = (Element) node;
            //elem.normalize();
            out.print("\n");
            for (int j = 0; j < indent; j++) {
                out.print(" ");
            }// w w w.  j  a v a  2 s  .  c  om
            out.print("<" + elem.getTagName());
            NamedNodeMap attrs = elem.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Attr a = (Attr) attrs.item(i);
                out.print(" " + a.getName() + "=\"" + a.getValue() + "\"");
            }
            out.print(">");
            NodeList nl = node.getChildNodes();
            for (int i = 0; i < nl.getLength(); i++) {
                writeXMLwalkTree(nl.item(i), indent + 2, out);
            }
            //              for(int j=0;j<indent;j++) {
            //                  out.print(" ");
            //              }
            out.println("</" + elem.getTagName() + ">");
        }
    } else {
        if (node instanceof Element) {
            Element elem = (Element) node;
            out.print("\n");
            for (int j = 0; j < indent; j++) {
                out.print(" ");
            }
            out.print("<" + elem.getTagName());
            NamedNodeMap attrs = elem.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Attr a = (Attr) attrs.item(i);
                out.print(" " + a.getName() + "=\"" + a.getValue() + "\"");
            }
            out.println("/>");
        } else if (node instanceof CDATASection) {
            CDATASection cdata = (CDATASection) node;
            //              for(int j=0;j<indent;j++) {
            //                  out.print(" ");
            //              }
            out.print("<![CDATA[" + cdata.getData() + "]]>");
        } else if (node instanceof Text) {
            Text text = (Text) node;
            StringBuilder buf = new StringBuilder(text.getData().length());
            for (int i = 0; i < text.getData().length(); i++) {
                if (text.getData().charAt(i) == '\n' || text.getData().charAt(i) == '\r'
                        || text.getData().charAt(i) == ' ' || text.getData().charAt(i) == '\t') {
                    if (buf.length() > 0 && buf.charAt(buf.length() - 1) != ' ') {
                        buf.append(' ');
                    }
                } else {
                    buf.append(text.getData().charAt(i));
                }
            }
            if (buf.length() > 0 && !buf.toString().equals(" ")) {
                StringBuilder buf2 = new StringBuilder(buf.length() + indent);
                //                  for(int j=0;j<indent;j++) {
                //                      buf2.append(' ');
                //                  }
                buf2.append(buf.toString());
                out.print(buf2);
            }
        }
    }
}

From source file:Main.java

/**
 * Used for debuging/*from w  ww .j  a  v a  2 s.  co m*/
 * 
 * @param parent
 *            Element
 * @param out
 *            PrintStream
 * @param deep
 *            boolean
 * @param prefix
 *            String
 */
public static void printChildElements(Element parent, PrintStream out, boolean deep, String prefix) {
    out.print(prefix + "<" + parent.getNodeName());
    NamedNodeMap attrs = parent.getAttributes();
    Node node;
    for (int i = 0; i < attrs.getLength(); i++) {
        node = attrs.item(i);
        out.print(" " + node.getNodeName() + "=\"" + node.getNodeValue() + "\"");
    }
    out.println(">");

    // String data = getElementTextValueDeprecated(parent);
    String data = parent.getNodeValue();
    if (data != null && data.trim().length() > 0) {
        out.println(prefix + "\t" + data);
    }

    data = getElementCDataValue(parent);
    if (data != null && data.trim().length() > 0) {
        out.println(prefix + "\t<![CDATA[" + data + "]]>");
    }

    NodeList nodes = parent.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (deep) {
                printChildElements((Element) node, out, deep, prefix + "\t");
            } else {
                out.println(prefix + node.getNodeName());
            }
        }
    }

    out.println(prefix + "</" + parent.getNodeName() + ">");
}

From source file:Main.java

/**
 *  Copy the attributes from n2 to n1./*from  w ww .  ja v a2  s  .c om*/
 *
 *  @param n1 The source of the attributes.
 *  @param n2 What to copy into.
 */
public static void mergeAttributes(Element n1, Element n2) {
    if ((n1 == null) || (n2 == null)) {
        return;
    }
    NamedNodeMap nnm = n2.getAttributes();
    if (nnm == null) {
        return;
    }
    for (int i = 0; i < nnm.getLength(); i++) {
        Attr attr = (Attr) nnm.item(i);
        n1.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }

}

From source file:Main.java

@SuppressWarnings("fallthrough")
static final void getSetRec(final Node rootNode, final Set<Node> result, final Node exclude,
        final boolean com) {
    //Set result = new HashSet();
    if (rootNode == exclude) {
        return;/* w w  w  .j  a v  a 2s.  c o m*/
    }
    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 - ignore fallthrough warning
    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:Main.java

private static void serializeElement(StringBuilder sb, Element element, int tabIndex) {
    sb.append('\n');
    for (int i = 0; i < tabIndex; i++) {
        sb.append('\t');
    }/*  w  w  w.  ja va2s.  c o  m*/
    sb.append("<");
    sb.append(element.getTagName());
    if (element.hasAttributes()) {
        NamedNodeMap attributes = element.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attribute = attributes.item(i);

            sb.append(" ");
            sb.append(attribute.getNodeName());
            sb.append("=");
            sb.append("\"");
            String value = attribute.getNodeValue();
            sb.append(value.replace("\"", "\\\""));
            sb.append("\"");
        }
    }
    sb.append(">");
    NodeList nodeList = element.getChildNodes();
    ArrayList<Element> childElements = new ArrayList<Element>();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(i);
        if (childNode instanceof Element) {
            childElements.add((Element) childNode);
        }
    }
    if (childElements.size() == 0) {
        sb.append(escapeInvalidCharacters(getTextContent(element)));
    } else {
        for (Element childElement : childElements) {
            serializeElement(sb, childElement, tabIndex + 1);
        }
        sb.append('\n');
        for (int i = 0; i < tabIndex; i++) {
            sb.append('\t');
        }
    }
    sb.append("</");
    sb.append(element.getTagName());
    sb.append(">");
}

From source file:Main.java

/**
 * Prints elements of the specified node.
 * /*from  www  .  j  a va2  s  .  c o  m*/
 * @param node the specified node.
 * @param level a number of indent.
 */
private static void printElement(Node node, int level) {
    for (int i = 0; i < level; i++) {
        System.out.print("  ");
    }
    System.out.print("{" + node.getNamespaceURI() + "}");
    System.out.println(node.getNodeName());
    NamedNodeMap attrs = node.getAttributes();
    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            printNode(attrs.item(i), level + 1);
        }
    }

    NodeList children = node.getChildNodes();
    int n = children.getLength();
    for (int i = 0; i < n; i++) {
        Node child = children.item(i);
        printNode(child, level + 1);
    }
}

From source file:Main.java

private static String prettyPrintDom(Node node, String indent, boolean isRoot, boolean escapeStrings) {
    String ret = "";
    switch (node.getNodeType()) {

    case Node.DOCUMENT_NODE:
        // recurse on each child
        NodeList nodes = node.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                ret += prettyPrintDom(nodes.item(i), indent, isRoot, escapeStrings);
            }//w  w w . j  a v  a2  s. c o m
        }
        break;

    case Node.ELEMENT_NODE:
        String name = node.getNodeName();
        ret += indent + "<" + name;
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node current = attributes.item(i);
            ret += " " + current.getNodeName() + "=\""
                    + ((escapeStrings) ? escapeStringForXML(current.getNodeValue()) : current.getNodeValue())
                    + "\"";
        }
        ret += ">";

        // recurse on each child
        NodeList children = node.getChildNodes();
        if (children != null) {
            for (int i = 0; i < children.getLength(); i++) {
                String tmp = prettyPrintDom(children.item(i), indent + ((isRoot) ? "" : BI), false,
                        escapeStrings);
                if (!tmp.replaceAll("[\\s]+", "").equals(""))
                    if (tmp.endsWith("\n"))
                        if (ret.endsWith("\n"))
                            ret += tmp;
                        else
                            ret += "\n" + tmp;
                    else
                        ret += tmp;
            }
        }
        if (ret.endsWith("\n"))
            ret += indent + "</" + name + ">\n";
        else
            ret += "</" + name + ">\n";
        break;

    case Node.TEXT_NODE:
        ret += (escapeStrings) ? escapeStringForXML(node.getNodeValue()) : node.getNodeValue();
        break;

    case Node.COMMENT_NODE:
        ret += "<!-- " + node.getNodeValue() + " -->";
        break;
    }
    return ret;
}

From source file:Main.java

/**
 * Whenever you'd need to print a configuration node and/or its children.
 *
 * @param root the root node to print.//from  ww w .  ja  v a2 s .  co m
 * @param out the print stream that should be used to outpu
 * @param recurse boolean
 * @param prefix String
 */
public static void printChildElements(Element root, PrintStream out, boolean recurse, String prefix) {
    out.print(prefix + "<" + root.getNodeName());
    NamedNodeMap attrs = root.getAttributes();
    Node node;
    for (int i = 0; i < attrs.getLength(); i++) {
        node = attrs.item(i);
        out.print(" " + node.getNodeName() + "=\"" + node.getNodeValue() + "\"");
    }
    out.println(">");

    String data = getText(root);
    if (data != null && data.trim().length() > 0)
        out.println(prefix + "\t" + data);

    data = getCData(root);
    if (data != null && data.trim().length() > 0)
        out.println(prefix + "\t<![CDATA[" + data + "]]>");

    NodeList nodes = root.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (recurse)
                printChildElements((Element) node, out, recurse, prefix + "\t");
            else
                out.println(prefix + node.getNodeName());
        }
    }

    out.println(prefix + "</" + root.getNodeName() + ">");
}

From source file:Main.java

/**
 * Searches throgh the passed NamedNodeMap for an attribute. If it is found, it will try to convert it to a boolean.
 * @param nnm NamedNodeMap// ww  w .j a va2  s  .com
 * @param name String
 * @throws RuntimeException on any failure to parse a boolean
 * @return boolean
 */
public static boolean getAttributeBooleanByName(NamedNodeMap nnm, String name) throws RuntimeException {
    for (int i = 0; i < nnm.getLength(); i++) {
        Attr attr = (Attr) nnm.item(i);
        if (attr.getName().equalsIgnoreCase(name)) {
            String tmp = attr.getValue().toLowerCase();
            if (tmp.equalsIgnoreCase("true"))
                return true;
            if (tmp.equalsIgnoreCase("false"))
                return false;
            throw new RuntimeException("Attribute " + name + " value not boolean:" + tmp);
        }
    }
    throw new RuntimeException("Attribute " + name + " not found.");
}