Example usage for org.w3c.dom Element getAttributeNode

List of usage examples for org.w3c.dom Element getAttributeNode

Introduction

In this page you can find the example usage for org.w3c.dom Element getAttributeNode.

Prototype

public Attr getAttributeNode(String name);

Source Link

Document

Retrieves an attribute node by name.

Usage

From source file:Main.java

public static String attribute(Element element, String attrName) {
    Attr attr = element.getAttributeNode(attrName);
    return attr != null ? attr.getValue() : null;
}

From source file:Main.java

/**
 * Gets an attribute value of the given element.
 * @param ownerElem the element that owns the attribute
 * @param attrName the name of the attribute to retrieve
 * @return the attribute value as a string, or <code>null</code> if that attribute does not have
 * a specified value//  w w w  . j  a  va2s  .  co m
 */
public static String getAttribute(Element ownerElem, String attrName) {
    Attr attribute = ownerElem.getAttributeNode(attrName);
    return attribute != null ? attribute.getValue() : null;
}

From source file:Main.java

public static void dupAttributes(Document doc) {
    Element root = doc.getDocumentElement();
    Element personOne = (Element) root.getFirstChild();

    Attr deptAttr = personOne.getAttributeNode("dept");
    personOne.removeAttributeNode(deptAttr);
    String deptString = deptAttr.getValue();
    personOne.setAttribute("dept", deptString + "updated");

    String mailString = personOne.getAttribute("mail");
    personOne.setAttribute("mail", mailString + "updated");

    String titleString = personOne.getAttribute("title");
    //personOne.removeAttribute("title");
    personOne.setAttribute("title", titleString + "updated");
}

From source file:Main.java

/**
 * Get an attribute from the specified Element
 *///w  ww  . j a v  a 2  s  . c o  m
public static Attr getAttribute(Element element, String attribute) {
    if (element != null)
        return element.getAttributeNode(attribute);

    return null;
}

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./*from   w  w w.  jav  a 2  s . c o  m*/
 *
 * @param aElement
 *        the source element to get the attribute from. May not 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 getAttributeValue(@Nonnull final Element aElement, @Nonnull final String sAttrName,
        @Nullable final String sDefault) {
    final Attr aAttr = aElement.getAttributeNode(sAttrName);
    return aAttr == null ? sDefault : aAttr.getValue();
}

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  . com*/
 * @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:DOMEdit.java

public static void dupAttributes(Document doc) {
        Element root = doc.getDocumentElement();
        Element personOne = (Element) root.getFirstChild();

        Attr deptAttr = personOne.getAttributeNode("dept");
        personOne.removeAttributeNode(deptAttr);
        String deptString = deptAttr.getValue();
        personOne.setAttribute("dept", deptString + " updated");

        String mailString = personOne.getAttribute("mail");
        personOne.setAttribute("mail", mailString + " updated");

        String titleString = personOne.getAttribute("title");
        //personOne.removeAttribute("title");
        personOne.setAttribute("title", titleString + " updated");
    }/*ww  w  .j  a  va 2  s. com*/

From source file:Main.java

/**
 * Locates the attribute defined by the XPath expression in the XML file and replaces it with the passed value.
 * @param fileName The XML file to update.
 * @param xPathExpression An XPath expression that locates the attribute to update.
 * @param attributeName The name of the attribute to update.
 * @param value The value to update the attribute to.
 *///  w  ww. ja v a  2  s. com
public static void updateAttributeInXMLFile(String fileName, String xPathExpression, String attributeName,
        String value) {
    try {
        Document document = parseXML(new File(fileName));
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression xpathExpression = xpath.compile(xPathExpression);
        Element element = (Element) xpathExpression.evaluate(document, NODE);

        element.getAttributeNode(attributeName).setValue(value);
        writeElement(document.getDocumentElement(), fileName);

    } catch (Exception e) {
        throw new RuntimeException("Failed to extract element from:" + fileName, e);
    }
}

From source file:Main.java

/**
 * Returns the value of the named attribute of the current element.
 *
 * @param parent//  w ww.j  ava2s . c o m
 * @param localName attribute local name or 'nodeName' if no namespace is
 * specified.
 * @param  namespaceURI or <code>null</code>
 * @return attribute value, or <code>null</code> if not found
 */
public static String getAttribute(Element parent, String localName, String namespaceURI) {
    if (parent == null) {
        return null;
    }
    Attr attribute;
    if (namespaceURI == null) {
        attribute = parent.getAttributeNode(localName);
    } else {
        attribute = parent.getAttributeNodeNS(namespaceURI, localName);
    }
    if (attribute != null) {
        return attribute.getValue();
    } else {
        return null;
    }
}

From source file:Main.java

/**
Assume ELEMENT had attribute ATTRNAME, get its value and
return it as a string.  If there is no attribute return the default.
 *///from w  w  w  .  ja  v  a 2s  .c  om
public static String stringAttribute(Element element, String attrName, String theDefault) {
    String attr = element.getAttribute(attrName);

    if (attr.equals(EmptyString)) {
        /* empty string maybe a valid option */
        Attr attrNode = element.getAttributeNode(attrName);
        if (attrNode != null) {
            return EmptyString;
        }

        return theDefault;
    } else {
        return attr;
    }
}