Example usage for org.w3c.dom Attr getName

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

Introduction

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

Prototype

public String getName();

Source Link

Document

Returns the name of this attribute.

Usage

From source file:Main.java

private static void outputElement(Element node, String indent) {
    System.out.print(indent + "<" + node.getTagName());
    NamedNodeMap nm = node.getAttributes();
    for (int i = 0; i < nm.getLength(); i++) {
        Attr attr = (Attr) nm.item(i);
        System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\"");
    }/* w ww. j  av  a 2s.  c om*/
    System.out.print(">");
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++)
        outputloop(list.item(i), indent + TAB);
    System.out.println(indent + "</" + node.getTagName() + ">");
}

From source file:TryDOM.java

static void listNodes(Node node, String indent) {
        String nodeName = node.getNodeName();
        System.out.println(indent + nodeName + " Node, type is " + node.getClass().getName() + ":");
        System.out.println(node);

        if (node instanceof Element && node.hasAttributes()) {
            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   ww w.ja  va 2  s  .com*/
        }
        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

/**
 * Setup the ID attribute into <code>destElement</code> depending on the <code>isId</code> flag of an attribute of
 * <code>sourceNode</code>./*from  w  w  w .j a va 2  s.c  o  m*/
 *
 * @param sourceNode
 * @param destDocElement
 */
public static void propagateIDAttributeSetup(Node sourceNode, Element destElement) {
    NamedNodeMap nnm = sourceNode.getAttributes();
    for (int i = 0; i < nnm.getLength(); i++) {
        Attr attr = (Attr) nnm.item(i);
        if (attr.isId()) {
            destElement.setIdAttribute(attr.getName(), true);
            break;
        }
    }
}

From source file:MainClass.java

static void listNodes(Node node) {
    String nodeName = node.getNodeName();

    if (node instanceof Element) {
        if (node.hasAttributes()) {
            NamedNodeMap attrs = node.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Attr attribute = (Attr) attrs.item(i); // Get an attribute
                System.out.println(" " + attribute.getName() + "=" + attribute.getValue());
            }/*  w w w .j av a  2s  .  c  o m*/
        }
        System.out.println(indent + "<" + nodeName + ">");
    } else if (node instanceof Text) {
        System.out.println(((Text) node).getData());
    } else if (node instanceof DocumentType) {
        System.out.println(getDoctypeString((DocumentType) node));
    }

    indent.append(' ');
    NodeList list = node.getChildNodes();
    if (list.getLength() > 0) {
        for (int i = 0; i < list.getLength(); i++) {
            listNodes(list.item(i));
        }
    }
    System.out.println("</" + nodeName + ">");

}

From source file:Main.java

public static void printElement(Element e) {
    List lst = getChildrenElement(e);
    if (lst.size() == 0) {
        System.out.print(e.getTagName() + "=");
        String value = getNodeText(e);
        if (value.trim().length() > 0)
            System.out.println(value);
        else//from  w w  w.  j a  v  a2 s. c om
            System.out.println();

        NamedNodeMap nn = e.getAttributes();
        for (int i = 0; i < nn.getLength(); i++) {
            Attr attr = (Attr) e.getAttributes().item(i);
            System.out.println(attr.getName() + "=" + attr.getValue());
        }
    } else {
        System.out.println(e.getTagName());
        for (int i = 0; i < lst.size(); i++) {
            printElement((Element) lst.get(i));
        }
    }
}

From source file:DOMEdit.java

private static void outputElement(Element node, String indent) {
    System.out.print(indent + "<" + node.getTagName());
    NamedNodeMap nm = node.getAttributes();
    for (int i = 0; i < nm.getLength(); i++) {
        Attr attr = (Attr) nm.item(i);
        System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\"");
    }/*from ww w  .j a v a 2  s  .c o m*/
    System.out.println(">");
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++)
        outputloop(list.item(i), indent + TAB);
    System.out.println(indent + "</" + node.getTagName() + ">");
}

From source file:Main.java

public static String getPrefix(String uri, Node e) {
    while (e != null && (e.getNodeType() == Element.ELEMENT_NODE)) {
        NamedNodeMap attrs = e.getAttributes();
        for (int n = 0; n < attrs.getLength(); n++) {
            Attr a = (Attr) attrs.item(n);
            String name;//from w  w w .j  a v  a 2s. com
            if ((name = a.getName()).startsWith("xmlns:") && a.getNodeValue().equals(uri)) {
                return name.substring(6);
            }
        }
        e = e.getParentNode();
    }
    return null;
}

From source file:Main.java

private static void fixupAttrsSingle(Element e) throws DOMException {
    removeXmlBase(e);/*  www .j a  va2s  .c o m*/
    Map<String, String> replace = new HashMap<String, String>();
    NamedNodeMap attrs = e.getAttributes();
    for (int j = 0; j < attrs.getLength(); j++) {
        Attr attr = (Attr) attrs.item(j);
        if (attr.getNamespaceURI() == null && !attr.getName().equals("xmlns")) { // NOI18N
            replace.put(attr.getName(), attr.getValue());
        }
    }
    for (Map.Entry<String, String> entry : replace.entrySet()) {
        e.removeAttribute(entry.getKey());
        e.setAttributeNS(null, entry.getKey(), entry.getValue());
    }
}

From source file:Main.java

private static void attributize(Element root) {
    NamedNodeMap attributeMap = root.getAttributes();
    for (int i = 0; i < attributeMap.getLength(); i++) {
        org.w3c.dom.Attr attr = (Attr) attributeMap.item(i);

        Element attrElement = root.getOwnerDocument().createElement(attr.getName());
        attrElement.setTextContent(attr.getValue());
        root.appendChild(attrElement);/*from   w ww  .ja  va  2s .c o m*/
    }

    NodeList children = root.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i) instanceof Element) {
            attributize((Element) children.item(i));
        }
    }
}

From source file:Main.java

/**
 * Searches throgh the passed NamedNodeMap for an attribute and returns it if it is found.
 * If it is not found, returns a null./*from w w w .j a  va  2  s .  co m*/
 * @param nnm NamedNodeMap
 * @param name String
 * @return String
 */
public static String getAttributeValueByName(final NamedNodeMap nnm, final String name) {
    for (int i = 0; i < nnm.getLength(); i++) {
        Attr attr = (Attr) nnm.item(i);
        if (attr.getName().equalsIgnoreCase(name)) {
            return attr.getValue();
        }
    }
    return null;
}