Example usage for org.w3c.dom Attr getValue

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

Introduction

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

Prototype

public String getValue();

Source Link

Document

On retrieval, the value of the attribute is returned as a string.

Usage

From source file:Main.java

/**
 * Get attribute value.//from  ww w . j  a v  a  2 s  .  c om
 * 
 * @param element an Element object.
 * @param namespace a namespace.
 * @param attrName a name of an attribute
 * @return the attribute value.
 */
public static String getAttributeValueNS(Element element, String namespace, String attrName) {
    String attrValue = null;
    Attr attr = null;

    // Get the attribute using its name
    if ((attr = element.getAttributeNodeNS(namespace, attrName)) != null) {
        attrValue = attr.getValue().trim();
    }

    // Return attribute value
    return attrValue;
}

From source file:Main.java

/**
 * Get attribute value with the given name defined for the specified element.
 * /*  ww  w .j  a  v a 2 s  . c  o m*/
 * @param element an Element object.
 * @param attrName a name of an attribute
 * @return the attribute value.
 */
public static String getAttributeValue(Element element, String attrName) {
    String attrValue = null;
    Attr attr = null;

    // Get the attribute using its name
    if ((attr = element.getAttributeNode(attrName)) != null) {
        attrValue = attr.getValue().trim();
    }

    // Return attribute value
    return attrValue;
}

From source file:DOMCopy.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  ava  2s . 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: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 ww  w.  j  a va  2 s  .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

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 . ja  va  2 s  .c o  m
            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

public static String getText(Element rootElem, String[] path) {
    NodeList nodes = rootElem.getElementsByTagName(path[0]);
    if (nodes == null || nodes.getLength() < 1) {
        // failsafe if first item is @attribute identifier
        // then read attribute value from rootElement.
        boolean isAttrText = path[0].charAt(0) == '@';
        if (!isAttrText)
            return null;
        Attr attr = rootElem.getAttributeNode(path[0].substring(1));
        return (attr != null ? attr.getValue() : null);
    }/*from w w  w.java 2  s .c o  m*/
    Element element = (Element) nodes.item(0);
    return getText(element, path, 1);
}

From source file:Main.java

/** Takes XML node and prints to String.
 *
 * @param node Element to print to String
 * @return XML node as String/*from www .  ja v a  2  s.c  om*/
 */
private static void printNode(StringBuffer sBuffer, Node node) {
    if (node.getNodeType() == Node.TEXT_NODE) {
        sBuffer.append(encodeXMLText(node.getNodeValue().trim()));
    } else if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
        sBuffer.append("<![CDATA[");
        sBuffer.append(node.getNodeValue());
        sBuffer.append("]]>");
    } else if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element el = (Element) node;

        sBuffer.append("<").append(el.getTagName());

        NamedNodeMap attribs = el.getAttributes();

        for (int i = 0; i < attribs.getLength(); i++) {
            Attr nextAtt = (Attr) attribs.item(i);
            sBuffer.append(" ").append(nextAtt.getName()).append("=\"").append(nextAtt.getValue()).append("\"");
        }

        NodeList nodes = node.getChildNodes();

        if (nodes.getLength() == 0) {
            sBuffer.append("/>");
        } else {
            sBuffer.append(">");

            for (int i = 0; i < nodes.getLength(); i++) {
                printNode(sBuffer, nodes.item(i));
            }

            sBuffer.append("</").append(el.getTagName()).append(">");
        }
    }
}

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  .j  a v a  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

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);/* ww  w  . j  a  va  2 s  .co  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

/**
 * Get textvalue from path or attribute. This is 
 * called by other getText() methods. Endusers usually
 * should not call this directly./*from   ww  w  .  ja v  a 2s  . c  om*/
 * @param element
 * @param path
 * @param pathOffset
 * @return
 */
public static String getText(Element element, String[] path, int pathOffset) {
    int len = path.length;
    boolean isAttrText = path[len - 1].charAt(0) == '@';
    if (isAttrText)
        len--; // last item is @attribute identifier

    // start path from given offset index
    for (int i = pathOffset; i < len; i++) {
        if (element == null)
            return null;
        NodeList nodes = element.getElementsByTagName(path[i]);
        element = (Element) nodes.item(0);
    }

    if (isAttrText) {
        if (element == null)
            return null;
        Attr attr = element.getAttributeNode(path[len].substring(1));
        return (attr != null ? attr.getValue() : null);
    } else {
        return getSimpleText(element);
    }
}