Example usage for org.w3c.dom Attr getNodeName

List of usage examples for org.w3c.dom Attr getNodeName

Introduction

In this page you can find the example usage for org.w3c.dom Attr getNodeName.

Prototype

public String getNodeName();

Source Link

Document

The name of this node, depending on its type; see the table above.

Usage

From source file:Main.java

public static void copyInto(Node src, Node dest) throws DOMException {

    Document factory = dest.getOwnerDocument();

    //Node start = src;
    Node parent = null;// w w  w  .ja  v a  2  s.co m
    Node place = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr) attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException(
                    "can't copy node type, " + type + " (" + place.getNodeName() + ')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place = place.getFirstChild();
            dest = node;
        } else if (parent == null) {
            place = null;
        } else {
            // advance
            place = place.getNextSibling();
            while (place == null && parent != null) {
                place = parent.getNextSibling();
                parent = parent.getParentNode();
                dest = dest.getParentNode();
            }
        }

    }

}

From source file:Main.java

/**
 * Convenience method to copy the contents of the old {@link Node} into the
 * new one./*from w  ww  . jav a 2 s .  com*/
 * 
 * @param newDoc
 * @param newNode
 * @param oldParent
 */
public static void copyContents(Document newDoc, Node newNode, Node oldNode) {
    // FIXME we should be able to achieve this with much less code (e.g.
    // the code commented out) but for some reason there are
    // incompatibility issues being spat out by the tests.
    //      final NodeList childNodes = oldNode.getChildNodes();
    //        for (int i = 0; i < childNodes.getLength(); i++) {
    //           final Node child = newDoc.importNode(childNodes.item(i), true);
    //           newNode.appendChild(child);
    //        }

    final NodeList childs = oldNode.getChildNodes();

    for (int i = 0; i < childs.getLength(); i++) {
        final Node child = childs.item(i);
        Element newElem = null;
        switch (child.getNodeType()) {
        case Node.ELEMENT_NODE:
            //Get all the attributes of an element in a map
            final NamedNodeMap attrs = child.getAttributes();

            // Process each attribute
            newElem = newDoc.createElement(child.getNodeName());
            for (int j = 0; j < attrs.getLength(); j++) {
                final Attr attr = (Attr) attrs.item(j);
                // add attribute name and value to the new element
                newElem.setAttribute(attr.getNodeName(), attr.getNodeValue());
            }
            newNode.appendChild(newElem);
            break;

        case Node.TEXT_NODE:
            newNode.appendChild(newDoc.createTextNode(getString(child)));
        }
        copyContents(newDoc, newElem, child);
    }
}

From source file:Main.java

/**
 * Print SAML Attribute Element and replace its prefix with the input
 * prefix.//from  w ww  .  j a  v a  2 s  .com
 * 
 * @param node
 *            A DOM tree Node
 * @param prefix
 *            A String representing the new prefix
 * @return An xml String representation of the DOM tree.
 */
public static String printAttributeValue(Element node, String prefix) {
    if (node == null) {
        return null;
    }

    StringBuffer xml = new StringBuffer(100);
    xml.append('<');
    xml.append(prefix).append(node.getLocalName());
    NamedNodeMap attrs = node.getAttributes();
    int length = attrs.getLength();
    for (int i = 0; i < length; i++) {
        Attr attr = (Attr) attrs.item(i);
        xml.append(' ');
        xml.append(attr.getNodeName());
        xml.append("=\"");
        // xml.append(normalize(attr.getNodeValue()));
        xml.append(attr.getNodeValue());
        xml.append('"');
    }
    xml.append('>');
    NodeList children = node.getChildNodes();
    if (children != null) {
        int len = children.getLength();
        for (int i = 0; i < len; i++) {
            xml.append(print(children.item(i)));
        }
    }
    xml.append("</");
    xml.append(prefix).append(node.getLocalName());
    xml.append('>');

    return xml.toString();

}

From source file:Main.java

/**
 * _more_/*from   w  w  w .  j  a  v a  2s .  co  m*/
 *
 * @param html _more_
 * @param node _more_
 */
public static void toHtml(StringBuffer html, Node node) {
    switch (node.getNodeType()) {

    case Node.ELEMENT_NODE: {
        NodeList children = node.getChildNodes();
        int numChildren = children.getLength();
        html.append("<b>" + node.getNodeName().replace("_", " ") + "</b>");
        html.append(": ");

        for (int i = 0; i < numChildren; i++) {
            Node child = children.item(i);
            if (((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))) {
                String v = child.getNodeValue();
                if (v == null) {
                    continue;
                }
                if (v.trim().length() == 0) {
                    continue;
                }
                html.append(v);
                html.append(" ");
            }
        }
        boolean didone = false;
        NamedNodeMap nnm = node.getAttributes();
        if (nnm != null) {
            for (int i = 0; i < nnm.getLength(); i++) {
                Attr attr = (Attr) nnm.item(i);
                String attrName = attr.getNodeName();
                if (attrName.startsWith("xmlns") || attrName.startsWith("xsi:")) {
                    continue;
                }
                if (!didone) {
                    html.append("<ul>");
                    didone = true;
                }
                html.append(attrName.replace("_", " ") + "=" + attr.getNodeValue());
                html.append("<br>\n");
            }
        }
        int cnt = 0;
        for (int i = 0; i < numChildren; i++) {
            Node child = children.item(i);
            if (((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))) {
                continue;
            }
            if (!didone) {
                html.append("<ul>");
                didone = true;
            }
            if (cnt > 0) {
                html.append("<br>");
            }
            toHtml(html, child);
            cnt++;
        }
        if (didone) {
            html.append("</ul>");
        }
        break;
    }
    }
}

From source file:Main.java

private static void prettyPrintLoop(Node node, StringBuilder string, String indentation) {
    if (node == null) {
        return;/*w  ww  .  j a v  a  2  s . co  m*/
    }

    int type = node.getNodeType();
    switch (type) {
    case Node.DOCUMENT_NODE:
        string.append("\n");
        prettyPrintLoop(node.getChildNodes(), string, indentation + "\t");
        break;

    case Node.ELEMENT_NODE:
        string.append(indentation);
        string.append("<");
        string.append(node.getNodeName());

        Attr[] attributes;
        if (node.getAttributes() != null) {
            int length = node.getAttributes().getLength();
            attributes = new Attr[length];
            for (int loopIndex = 0; loopIndex < length; loopIndex++) {
                attributes[loopIndex] = (Attr) node.getAttributes().item(loopIndex);
            }
        } else {
            attributes = new Attr[0];
        }

        for (Attr attribute : attributes) {
            string.append(" ");
            string.append(attribute.getNodeName());
            string.append("=\"");
            string.append(attribute.getNodeValue());
            string.append("\"");
        }

        string.append(">\n");

        prettyPrintLoop(node.getChildNodes(), string, indentation + "\t");

        string.append(indentation);
        string.append("</");
        string.append(node.getNodeName());
        string.append(">\n");

        break;

    case Node.TEXT_NODE:
        string.append(indentation);
        string.append(node.getNodeValue().trim());
        string.append("\n");
        break;

    case Node.PROCESSING_INSTRUCTION_NODE:
        string.append(indentation);
        string.append("<?");
        string.append(node.getNodeName());
        String text = node.getNodeValue();
        if (text != null && text.length() > 0) {
            string.append(text);
        }
        string.append("?>\n");
        break;

    case Node.CDATA_SECTION_NODE:
        string.append(indentation);
        string.append("<![CDATA[");
        string.append(node.getNodeValue());
        string.append("]]>");
        break;
    }
}

From source file:Main.java

/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.//from   w  w w .java2s .  c  o  m
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start = src;
    Node parent = src;
    Node place = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr) attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl) element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException(
                    "can't copy node type, " + type + " (" + node.getNodeName() + ')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place = place.getFirstChild();
            dest = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place = parent.getNextSibling();
                parent = parent.getParentNode();
                dest = dest.getParentNode();
            }
        }

    }

}

From source file:Main.java

/**
 * Print a Node tree recursively.//w  ww. java2  s .  c  om
 * @param node A DOM tree Node
 * @return An xml String representation of the DOM tree.
 */
public static String print(Node node) {
    if (node == null) {
        return null;
    }

    StringBuffer xml = new StringBuffer(100);
    int type = node.getNodeType();

    switch (type) {
    // print element with attributes
    case Node.ELEMENT_NODE: {
        xml.append('<');
        xml.append(node.getNodeName());

        NamedNodeMap attrs = node.getAttributes();
        int length = attrs.getLength();
        ;

        for (int i = 0; i < length; i++) {
            Attr attr = (Attr) attrs.item(i);
            xml.append(' ');
            xml.append(attr.getNodeName());
            xml.append("=\"");

            //xml.append(normalize(attr.getNodeValue()));
            xml.append(attr.getNodeValue());
            xml.append('"');
        }

        xml.append('>');

        NodeList children = node.getChildNodes();

        if (children != null) {
            int len = children.getLength();

            for (int i = 0; i < len; i++) {
                xml.append(print(children.item(i)));
            }
        }

        break;
    }

    // handle entity reference nodes
    case Node.ENTITY_REFERENCE_NODE: {
        NodeList children = node.getChildNodes();

        if (children != null) {
            int len = children.getLength();

            for (int i = 0; i < len; i++) {
                xml.append(print(children.item(i)));
            }
        }

        break;
    }

    // print cdata sections
    case Node.CDATA_SECTION_NODE: {
        xml.append("<![CDATA[");
        xml.append(node.getNodeValue());
        xml.append("]]>");

        break;
    }

    // print text
    case Node.TEXT_NODE: {
        //xml.append(normalize(node.getNodeValue()));
        xml.append(node.getNodeValue());

        break;
    }

    // print processing instruction
    case Node.PROCESSING_INSTRUCTION_NODE: {
        xml.append("<?");
        xml.append(node.getNodeName());

        String data = node.getNodeValue();

        if ((data != null) && (data.length() > 0)) {
            xml.append(' ');
            xml.append(data);
        }

        xml.append("?>");

        break;
    }
    }

    if (type == Node.ELEMENT_NODE) {
        xml.append("</");
        xml.append(node.getNodeName());
        xml.append('>');
    }

    return xml.toString();
}

From source file:Main.java

/** Prints the specified node, recursively.
 *
 *  @param node Node to be printed/*from ww  w.  j a  va  2  s .  c om*/
 */
public static void print(Node node) {
    // is there anything to do?
    if (node == null) {
        return;
    }
    System.out.println("");

    int type = node.getNodeType();
    switch (type) {
    // print document
    case Node.DOCUMENT_NODE: {
        /*
        if (!canonical) {
          if (Encoding.equalsIgnoreCase("DEFAULT"))
        Encoding = "UTF-8";
          else if(Encoding.equalsIgnoreCase("Unicode"))
        Encoding = "UTF-16";
          else 
        Encoding = MIME2Java.reverse(Encoding);
        out.println("<?xml version=\"1.0\" encoding=\"" +
          Encoding + "\"?>");
        }
         */
        print(((Document) node).getDocumentElement());
        out.flush();
        break;
    }
    // print element with attributes
    case Node.ELEMENT_NODE: {
        out.print('<');
        out.print(node.getNodeName());
        Attr attrs[] = sortAttributes(node.getAttributes());
        for (int i = 0; i < attrs.length; i++) {
            Attr attr = attrs[i];
            out.print(' ');
            out.print(attr.getNodeName());
            out.print("=\"");
            out.print(normalize(attr.getNodeValue()));
            out.print('"');
        }
        out.print('>');
        NodeList children = node.getChildNodes();
        if (children != null) {
            int len = children.getLength();
            for (int i = 0; i < len; i++) {
                print(children.item(i));
            }
        }
        break;
    }
    // handle entity reference nodes
    case Node.ENTITY_REFERENCE_NODE: {
        if (canonical) {
            NodeList children = node.getChildNodes();
            if (children != null) {
                int len = children.getLength();
                for (int i = 0; i < len; i++) {
                    print(children.item(i));
                }
            }
        } else {
            out.print('&');
            out.print(node.getNodeName());
            out.print(';');
        }
        break;
    }
    // print cdata sections
    case Node.CDATA_SECTION_NODE: {
        if (canonical) {
            out.print(normalize(node.getNodeValue()));
        } else {
            out.print("<![CDATA[");
            out.print(node.getNodeValue());
            out.print("]]>");
        }
        break;
    }
    // print DocumentType sections
    case Node.DOCUMENT_TYPE_NODE: {
        out.print("<!DOCTYPE ");
        out.print(((DocumentType) node).getName());
        out.print(" SYSTEM ");
        out.print(((DocumentType) node).getSystemId());
        out.print(">");
        break;
    }
    // print text
    case Node.TEXT_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }
    // print processing instruction
    case Node.PROCESSING_INSTRUCTION_NODE: {
        out.print("<?");
        out.print(node.getNodeName());
        String data = node.getNodeValue();
        if (data != null && data.length() > 0) {
            out.print(' ');
            out.print(data);
        }
        out.print("?>");
        break;
    }
    }
    if (type == Node.ELEMENT_NODE) {
        out.print("</");
        out.print(node.getNodeName());
        out.print('>');
    }
    out.flush();
}

From source file:Main.java

protected static void print(PrintStream out, Node node) {
    if (node == null)
        return;/*from   w  ww . jav a 2  s . c om*/
    short type = node.getNodeType();
    switch (type) {
    case Node.DOCUMENT_NODE: {
        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        //out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
        NodeList nodelist = node.getChildNodes();
        int size = nodelist.getLength();
        for (int i = 0; i < size; i++)
            print(out, nodelist.item(i));
        break;
    }

    case Node.DOCUMENT_TYPE_NODE: {
        DocumentType docType = (DocumentType) node;
        out.print("<!DOCTYPE " + getDocumentTypeData(docType) + ">\n");
        break;
    }

    case Node.ELEMENT_NODE: {
        out.print('<');
        out.print(node.getNodeName());
        NamedNodeMap map = node.getAttributes();
        if (map != null) {
            int size = map.getLength();
            for (int i = 0; i < size; i++) {
                Attr attr = (Attr) map.item(i);
                out.print(' ');
                out.print(attr.getNodeName());
                out.print("=\"");
                out.print(normalize(attr.getNodeValue()));
                out.print('"');
            }
        }

        if (!node.hasChildNodes())
            out.print("/>");
        else {
            out.print('>');
            NodeList nodelist = node.getChildNodes();
            int numChildren = nodelist.getLength();
            for (int i = 0; i < numChildren; i++)
                print(out, nodelist.item(i));

            out.print("</");
            out.print(node.getNodeName());
            out.print('>');
        }
        break;
    }

    case Node.ENTITY_REFERENCE_NODE: {
        NodeList nodelist = node.getChildNodes();
        if (nodelist != null) {
            int size = nodelist.getLength();
            for (int i = 0; i < size; i++)
                print(out, nodelist.item(i));

        }
        break;
    }

    case Node.CDATA_SECTION_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    case Node.TEXT_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    case Node.PROCESSING_INSTRUCTION_NODE: {
        out.print("<?");
        out.print(node.getNodeName());
        String s = node.getNodeValue();
        if (s != null && s.length() > 0) {
            out.print(' ');
            out.print(s);
        }
        out.print("?>");
        break;
    }

    case Node.COMMENT_NODE: {
        out.print("<!--");
        out.print(node.getNodeValue());
        out.print("-->");
        break;
    }

    default: {
        out.print(normalize(node.getNodeValue()));
        break;
    }
    }
    out.flush();
}

From source file:Main.java

protected static void print(PrintStream out, Node node) {
    if (node == null)
        return;/*from  w  ww  . j av  a2  s  . co  m*/
    short type = node.getNodeType();
    switch (type) {
    case Node.DOCUMENT_NODE: {
        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        // out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
        NodeList nodelist = node.getChildNodes();
        int size = nodelist.getLength();
        for (int i = 0; i < size; i++)
            print(out, nodelist.item(i));
        break;
    }

    case Node.DOCUMENT_TYPE_NODE: {
        DocumentType docType = (DocumentType) node;
        out.print("<!DOCTYPE " + getDocumentTypeData(docType) + ">\n");
        break;
    }

    case Node.ELEMENT_NODE: {
        out.print('<');
        out.print(node.getNodeName());
        NamedNodeMap map = node.getAttributes();
        if (map != null) {
            int size = map.getLength();
            for (int i = 0; i < size; i++) {
                Attr attr = (Attr) map.item(i);
                out.print(' ');
                out.print(attr.getNodeName());
                out.print("=\"");
                out.print(normalize(attr.getNodeValue()));
                out.print('"');
            }
        }

        if (!node.hasChildNodes())
            out.print("/>");
        else {
            out.print('>');
            NodeList nodelist = node.getChildNodes();
            int numChildren = nodelist.getLength();
            for (int i = 0; i < numChildren; i++)
                print(out, nodelist.item(i));

            out.print("</");
            out.print(node.getNodeName());
            out.print('>');
        }
        break;
    }

    case Node.ENTITY_REFERENCE_NODE: {
        NodeList nodelist = node.getChildNodes();
        if (nodelist != null) {
            int size = nodelist.getLength();
            for (int i = 0; i < size; i++)
                print(out, nodelist.item(i));

        }
        break;
    }

    case Node.CDATA_SECTION_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    case Node.TEXT_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    case Node.PROCESSING_INSTRUCTION_NODE: {
        out.print("<?");
        out.print(node.getNodeName());
        String s = node.getNodeValue();
        if (s != null && s.length() > 0) {
            out.print(' ');
            out.print(s);
        }
        out.print("?>");
        break;
    }

    case Node.COMMENT_NODE: {
        out.print("<!--");
        out.print(node.getNodeValue());
        out.print("-->");
        break;
    }

    default: {
        out.print(normalize(node.getNodeValue()));
        break;
    }
    }
    out.flush();
}