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:DOMUtils.java

/**
 * Returns the value of an attribute of an element. Returns null
 * if the attribute is not found (whereas Element.getAttribute
 * returns "" if an attrib is not found).
 *
 * @param el       Element whose attrib is looked for
 * @param attrName name of attribute to look for 
 * @return the attribute value/*w w  w. j  a  v a 2  s. co  m*/
 */
static public String getAttribute(Element el, String attrName) {
    String sRet = null;
    Attr attr = el.getAttributeNode(attrName);

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

From source file:Main.java

/**
 * Gather all the namespaces defined on a node
 *
 * @return/*from  w w w .jav  a2s . c o  m*/
 */
public static Iterable<Entry<String, String>> getNamespaces(Element element) {
    TreeMap<String, String> map = new TreeMap<String, String>();
    do {
        NamedNodeMap attributes = element.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Attr attr = (Attr) attributes.item(i);
            final String name = attr.getLocalName();

            if (attr.getPrefix() != null) {
                if ("xmlns".equals(attr.getPrefix()))
                    if (!map.containsKey(name))
                        map.put(name, attr.getValue());
            } else if ("xmlns".equals(name)) {
                if (!map.containsKey(""))
                    map.put("", attr.getValue());
            }
        }
        if (element.getParentNode() == null || element.getParentNode().getNodeType() != Node.ELEMENT_NODE)
            break;
        element = (Element) element.getParentNode();
    } while (true);
    return map.entrySet();
}

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  w  w  w.j  av a2  s .  c  o m
                    } else {
                        repair(pid, relsExt, fa, resource, value);
                    }
                }
            }
        }
    }
}

From source file:Main.java

public static String getNamespace(String prefix, Node e, Node stopNode) {
    while (e != null && (e.getNodeType() == Node.ELEMENT_NODE)) {
        Attr attr = null;
        if (prefix == null) {
            attr = ((Element) e).getAttributeNode("xmlns");
        } else {/*w  ww.  j  a  v a2 s .co m*/
            attr = ((Element) e).getAttributeNodeNS(NS_URI_XMLNS, prefix);
        }
        if (attr != null)
            return attr.getValue();
        if (e == stopNode)
            return null;
        e = e.getParentNode();
    }
    return null;
}

From source file:Main.java

public static String nodeToString(Object node) {
    String result = "";
    if (node instanceof String) {
        String str = ((String) node).replaceAll("\\s+", " ").trim();
        result += str;//from   ww  w.j a v a2s. c om
    } else if (node instanceof Text) {
        String str = ((Text) node).getTextContent().replaceAll("\\s+", " ").trim();
        result += str;
    } else if (node instanceof Element) {
        Element en = (Element) node;
        result += "<" + en.getTagName();
        for (int j = 0; j < en.getAttributes().getLength(); j++) {
            Attr attr = (Attr) en.getAttributes().item(j);
            //if (!(attr.getNamespaceURI() != null && attr.getNamespaceURI().equals("http://www.w3.org/2000/xmlns/") && (attr.getLocalName().equals("xmlns") || attr.getLocalName().equals("xsi")))) {
            result += " " + attr.getName() + "=\"" + attr.getValue() + "\"";
            //}
        }
        if (en.getChildNodes().getLength() == 0) {
            result += "/>";
        } else {
            result += ">";
            ArrayList<Object> children = new ArrayList<Object>();
            for (int i = 0; i < en.getChildNodes().getLength(); i++) {
                children.add(en.getChildNodes().item(i));
            }
            result += nodesToString(children);
            result += "</" + en.getTagName() + ">";
        }
    }
    return result;
}

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//www. j av a2s .co 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:Main.java

private static Element copyNode(Document destDocument, Element dest, Element src) {

    NamedNodeMap namedNodeMap = src.getAttributes();
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Attr attr = (Attr) namedNodeMap.item(i);
        dest.setAttribute(attr.getName(), attr.getValue());
    }/*from www. j a v  a  2  s.c  o  m*/
    NodeList childNodeList = src.getChildNodes();
    for (int i = 0; i < childNodeList.getLength(); i++) {
        Node child = childNodeList.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            Text text = destDocument.createTextNode(child.getTextContent());
            dest.appendChild(text);
        } else if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element element = destDocument.createElement(((Element) child).getTagName());
            element = copyNode(destDocument, element, (Element) child);
            dest.appendChild(element);
        }
    }

    return dest;
}

From source file:Main.java

private static Node convertFromNamespaceForm(final Node node) {
    if (node instanceof Element) {
        final Document document = node.getOwnerDocument();
        final Element newElement = document.createElementNS(null, node.getLocalName());
        final NodeList children = node.getChildNodes();

        for (int i = 0, n = children.getLength(); i < n; i++) {
            final Node oldChildNode = children.item(i);
            final Node newChildNode = convertFromNamespaceForm(oldChildNode);

            newElement.appendChild(newChildNode);
        }// w ww .  j a  v a 2 s  . c  o  m

        final NamedNodeMap attributes = node.getAttributes();

        for (int i = 0, n = attributes.getLength(); i < n; i++) {
            final Attr attr = (Attr) attributes.item(i);
            final String attrQualifiedName = attr.getNodeName();
            final String attrLocalName = attr.getLocalName();

            if (!attrQualifiedName.equals(XMLNS) && !attrQualifiedName.startsWith(XMLNS_COLON)
                    && !attrLocalName.equals(XSI_SCHEMA_LOCATION_ATTR)) {
                newElement.setAttributeNS(null, attrLocalName, attr.getValue());
            }
        }

        return newElement;
    } else {
        return node.cloneNode(true);
    }
}

From source file:Main.java

/**
 * Copies all attributes from one element to another in the official way.
 *///from  w  w  w  .  j  ava 2  s .  co  m
public static void copyAttributes(Element elementFrom, Element elementTo) {
    NamedNodeMap nodeList = elementFrom.getAttributes();
    if (nodeList == null) {
        // No attributes to copy: just return
        return;
    }

    Attr attrFrom = null;
    Attr attrTo = null;

    // Needed as factory to create attrs
    Document documentTo = elementTo.getOwnerDocument();
    int len = nodeList.getLength();

    // Copy each attr by making/setting a new one and
    // adding to the target element.
    for (int i = 0; i < len; i++) {
        attrFrom = (Attr) nodeList.item(i);

        // Create an set value
        attrTo = documentTo.createAttribute(attrFrom.getName());
        attrTo.setValue(attrFrom.getValue());

        // Set in target element
        elementTo.setAttributeNode(attrTo);
    }
}

From source file:com.portfolio.data.utils.DomUtils.java

public static String getNodeAttributesString(Node node) {
    String ret = "";
    NamedNodeMap attributes = node.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attribute = (Attr) attributes.item(i);
        ret += attribute.getName().trim() + "=\"" + StringEscapeUtils.escapeXml11(attribute.getValue().trim())
                + "\" ";
    }//w w w .  ja v  a2  s  .c  o  m
    return ret;
}