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 an array of specific attributes for specific elements
 * in a Node tree. If the node is a Document, use the document
 * element as the starting point.// www .j  av a 2s. co m
 * @param node the top of the tree to search.
 * @param nodeName the element names to include in the search.
 * @param attrName the name of the attributes to include.
 * @return the array of attribute values natching the criteria.
 */
public static String[] getAttributeValues(Node node, String nodeName, String attrName) {
    if (node instanceof Document)
        node = ((Document) node).getDocumentElement();
    if (!(node instanceof Element))
        return new String[0];
    NodeList nodeList = ((Element) node).getElementsByTagName(nodeName);
    ArrayList list = new ArrayList();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Attr attr = ((Element) nodeList.item(i)).getAttributeNode(attrName);
        if (attr != null)
            list.add(attr.getValue());
    }
    String[] values = new String[list.size()];
    return (String[]) list.toArray(values);
}

From source file:Main.java

/**
 * Extract default values of variables defined in the DOM subtree below node.
 * Default values are used to populate the variableDefs map.  Variables
 * already defined in this map will NOT be modified.
 *
 * @param node root node of DOM subtree to extract default values from.
 * @param variableDefs map which default values will be added to.
 *///from  w w  w  .ja v  a  2  s .  co m
public static void extractVariableDefaults(final Node node, Map<String, String> variableDefs) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        final Element element = (Element) node;
        final NamedNodeMap attrs = element.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Attr attr = (Attr) attrs.item(i);
            extractVariableDefaultsFromString(attr.getValue(), variableDefs);
        }
        break;

    case Node.CDATA_SECTION_NODE:
        String content = node.getTextContent();
        extractVariableDefaultsFromString(content, variableDefs);
        break;

    default:
        break;
    }

    final NodeList children = node.getChildNodes();
    for (int childIndex = 0; childIndex < children.getLength(); childIndex++)
        extractVariableDefaults(children.item(childIndex), variableDefs);
}

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  v a2  s.c o  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

/** Helper method - gets a named attribute's value as a <I>String</I>.
   @param pNodeMap The <I>NamedNodeMap</I>.
   @param strName Name of the attribute.
   @return Value of the attribute.// w w  w .j a v a  2  s .  co m
 */
public static String getAttributeString(NamedNodeMap pNodeMap, String strName) throws ClassCastException {
    Attr pAttr = getAttribute(pNodeMap, strName);

    if (null == pAttr)
        return null;

    return pAttr.getValue();
}

From source file:Main.java

/**
 * The latest version of XercesJ 2.9 returns an empty string for non existing
 * attributes. To differentiate between empty attributes and non-existing
 * attributes, this method returns a default value for non existing
 * attributes.//  www.ja  va 2  s. co  m
 *
 * @param aElement
 *        the source element to get the attribute from. May not be
 *        <code>null</code>.
 * @param sNamespaceURI
 *        The namespace URI of the attribute to retrieve. May be
 *        <code>null</code>.
 * @param sAttrName
 *        the name of the attribute to query. May not be <code>null</code>.
 * @param sDefault
 *        the value to be returned if the attribute is not present.
 * @return the default value if the attribute does not exists, the string
 *         value otherwise
 */
@Nullable
public static String getAttributeValueNS(@Nonnull final Element aElement, @Nullable final String sNamespaceURI,
        @Nonnull final String sAttrName, @Nullable final String sDefault) {
    final Attr aAttr = aElement.getAttributeNodeNS(sNamespaceURI, sAttrName);
    return aAttr == null ? sDefault : aAttr.getValue();
}

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  .  ja va  2  s  .  c  o m*/
            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: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  ava2  s.  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: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() + "\"");
    }/* w ww  .  ja v a 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:Main.java

/**
 * Identify variables in attribute values and CDATA contents and replace
 * with their values as defined in the variableDefs map. Variables without
 * definitions are left alone.//from  w  w  w.  j  a v  a2 s  .c  om
 *
 * @param node the node to do variable replacement in
 * @param variableDefs map from variable names to values.
 */
public static void replaceVariables(final Node node, Map<String, String> variableDefs) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        final Element element = (Element) node;
        final NamedNodeMap atts = element.getAttributes();
        for (int i = 0; i < atts.getLength(); i++) {
            final Attr attr = (Attr) atts.item(i);
            attr.setNodeValue(replaceVariablesInString(attr.getValue(), variableDefs));
        }
        break;

    case Node.CDATA_SECTION_NODE:
        String content = node.getTextContent();
        node.setNodeValue(replaceVariablesInString(content, variableDefs));
        break;

    default:
        break;
    }

    // process children
    final NodeList children = node.getChildNodes();
    for (int childIndex = 0; childIndex < children.getLength(); childIndex++)
        replaceVariables(children.item(childIndex), variableDefs);
}

From source file:Main.java

public static String getAttributeNSOrNull(Element e, QName name) {
    Attr a = e.getAttributeNodeNS(name.getNamespaceURI(), name.getLocalPart());
    if (a == null)
        return null;
    return a.getValue();
}