Example usage for org.w3c.dom Element getAttributeNodeNS

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

Introduction

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

Prototype

public Attr getAttributeNodeNS(String namespaceURI, String localName) throws DOMException;

Source Link

Document

Retrieves an Attr node by local name and namespace URI.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//from   w  w w .  j a v  a2  s.  c om
    factory.setValidating(true);

    DocumentBuilder loader = factory.newDocumentBuilder();
    Document document = loader.parse("sample.xml");

    String docNS = "http://www.my-company.com";

    Element order = document.getDocumentElement();
    Attr date = order.getAttributeNodeNS(docNS, "date");
    NodeList children = order.getElementsByTagNameNS(docNS, "item");

}

From source file:Main.java

/**
 * Returns the attribute value for the attribute with the specified name.
 * Returns null if there is no such attribute, or
 * the empty string if the attribute value is empty.
 *
 * <p>This works around a limitation of the DOM
 * <code>Element.getAttributeNode</code> method, which does not distinguish
 * between an unspecified attribute and an attribute with a value of
 * "" (it returns "" for both cases).// w w w  . j  a  v  a 2s  .  c  o  m
 *
 * @param elem the element containing the attribute
 * @param name the name of the attribute
 * @return the attribute value (may be null if unspecified)
 */
public static String getAttributeValue(Element elem, String name) {
    Attr attr = elem.getAttributeNodeNS(null, name);
    return (attr == null) ? null : attr.getValue();
}

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();
}

From source file:Main.java

/**
 * Returns the String value of the unqualified attribute with the given
 * local name belonging to the given element, or null if the attribute is
 * not present./*w  ww .j a v  a2  s.co m*/
 * 
 * @param elem an element
 * @param localName an unqualified attribute name
 * @return the String value of the attribute, or null if the attribute is
 *         not present
 */
public static String getAttrString(Element elem, String localName) {
    Attr attr = elem.getAttributeNodeNS(null, localName);
    String value = (attr != null) ? attr.getValue() : null;
    return value;
}

From source file:Main.java

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

From source file:Main.java

/**
 * Get attribute value./*from  ww  w .  j av 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

/**
 * 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 www .j a  v a  2s  . 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

/**
 * Returns the value of the named attribute of the current element.
 *
 * @param parent//  w w w . j  ava  2  s.  co 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:DOMUtils.java

/**
 * Returns the value of an attribute of an element. Returns null
 * if the attribute is not found (whereas Element.getAttributeNS
 * returns "" if an attrib is not found).
 *
 * @param el       Element whose attrib is looked for
 * @param namespaceURI namespace URI of attribute to look for 
 * @param localPart local part of attribute to look for 
 * @return the attribute value//from w  w  w.ja  v a  2  s.c  o m
 */
static public String getAttributeNS(Element el, String namespaceURI, String localPart) {
    String sRet = null;
    Attr attr = el.getAttributeNodeNS(namespaceURI, localPart);

    if (attr != null) {
        sRet = attr.getValue();
    }

    return sRet;
}

From source file:cz.incad.cdk.RepairVCProcess.java

public static void checkRelsExt(String pid, Document relsExt, FedoraAccess fa) {
    Element descElement = XMLUtils.findElement(relsExt.getDocumentElement(), "Description",
            FedoraNamespaces.RDF_NAMESPACE_URI);
    List<Element> delems = XMLUtils.getElements(descElement);
    for (Element rel : delems) {
        if (rel.getNamespaceURI() != null) {
            if (rel.getNamespaceURI().equals(FedoraNamespaces.RDF_NAMESPACE_URI)
                    && rel.getLocalName().equals("isMemberOfCollection")) {
                Attr resource = rel.getAttributeNodeNS(FedoraNamespaces.RDF_NAMESPACE_URI, "resource");
                if (resource != null) {
                    String value = resource.getValue();
                    if (value.startsWith(PIDParser.INFO_FEDORA_PREFIX)) {
                        try {
                            PIDParser pars = new PIDParser(value);
                            pars.disseminationURI();
                        } catch (LexerException e) {
                            LOGGER.log(Level.SEVERE, e.getMessage(), e);
                            //repair(pid, relsExt, fa, resource, value);
                        }//from  www.j  av  a2s  .  c o m
                    } else {
                        repair(pid, relsExt, fa, resource, value);
                    }
                }
            }
        }
    }
}