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: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//from w  w  w. ja va  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

/**
 * This method will compare the attributes of a given src element with the attributes of a given dest element.
 * Both must contain the same attributes, but you can specify a String array of attribute names whose values
 * are ignored. Both elements must have the ignore attributes, their contents can be different and they will
 * still be considered equal./*from ww  w.  j a  va 2  s.  co m*/
 * @param src - the src element whose attributes are to be compared
 * @param dest - the dest element whose attributes are to be compared
 * @param ignore - the string array of attributes whose values are to be ignored during the compare.
 * @return true if the attributes of both nodes meet the criteria of being equal, false otherwise.
 */
private static boolean compareAttributes(Element src, Element dest, String[] ignore) {
    NamedNodeMap srcAttrs = src.getAttributes();

    if (srcAttrs.getLength() != dest.getAttributes().getLength())
        return false;

    for (int ctr = 0; ctr < srcAttrs.getLength(); ctr++) {
        Node srcAttr = srcAttrs.item(ctr);

        String name = srcAttr.getNodeName();
        if (Arrays.binarySearch(ignore, name) < 0) {
            Node destAttr = dest.getAttributeNode(name);
            if (destAttr == null || !srcAttr.isEqualNode(destAttr)) {
                return false;
            }
        }
    }

    return true;
}

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);
    }/*w  ww.ja v a2s .c  om*/
    Element element = (Element) nodes.item(0);
    return getText(element, path, 1);
}

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  w ww  . ja v  a2  s .  c  o m*/
 * @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);
    }
}

From source file:Main.java

/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.//from  w ww . jav a 2s. c o  m
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start = src;
    Node parent = src;
    Node place = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr) attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl) element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException(
                    "can't copy node type, " + type + " (" + node.getNodeName() + ')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place = place.getFirstChild();
            dest = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place = parent.getNextSibling();
                parent = parent.getParentNode();
                dest = dest.getParentNode();
            }
        }

    }

}

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.PanelSettings.java

private static void readTiposBaseDatosConfiguration(Document xmlConfiguration, XPath xpath)
        throws XPathExpressionException {

    tiposBaseDatos = new LinkedList<String>();

    Element doc = xmlConfiguration.getDocumentElement();
    NodeList databasetype_aux = doc.getElementsByTagName("databaseType");

    // iterate over the repositories
    for (int i = 0; i < databasetype_aux.getLength(); i++) {

        Node databaseTypeNode = databasetype_aux.item(i);

        if (databaseTypeNode instanceof Element) {
            Element databaseTypeElem = (Element) databaseTypeNode;

            // get alias and url from the repository node
            NodeList databaseTypeChildren = databaseTypeElem.getChildNodes();
            for (int a = 0; a < databaseTypeChildren.getLength(); a++) {

                Node childrenAux = databaseTypeChildren.item(a);
                if (childrenAux instanceof Element) {
                    Element childrenAuxElement = (Element) childrenAux;
                    if (childrenAuxElement.getNodeName().equals("name")) {
                        tiposBaseDatos.add(childrenAuxElement.getAttributeNode("value").getValue());
                    }//  w w w  . j  ava2s  .c  o m
                }
            }
        }

    }

}

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.PanelSettings.java

private static void readGeoserverConfiguration(Document xmlConfiguration, XPath xpath)
        throws XPathExpressionException {

    geoservers = new LinkedList<GeoserverVO>();

    Element doc = xmlConfiguration.getDocumentElement();
    NodeList geoserver_aux = doc.getElementsByTagName("geoserver");

    // iterate over the repositories
    for (int i = 0; i < geoserver_aux.getLength(); i++) {

        GeoserverVO geoserverAux = new GeoserverVO();

        Node geoserverNode = geoserver_aux.item(i);

        if (geoserverNode instanceof Element) {
            Element geoserverElem = (Element) geoserverNode;

            // get alias and url from the repository node
            NodeList geoserverChildren = geoserverElem.getChildNodes();
            for (int a = 0; a < geoserverChildren.getLength(); a++) {

                Node childrenAux = geoserverChildren.item(a);
                if (childrenAux instanceof Element) {
                    Element childrenAuxElement = (Element) childrenAux;
                    if (childrenAuxElement.getNodeName().equals("url")) {
                        geoserverAux.setGeoserverUrl(
                                Utils.removeLastSlash(childrenAuxElement.getAttributeNode("value").getValue()));
                    }//from ww  w.j a va  2  s . c  o m
                    if (childrenAuxElement.getNodeName().equals("user")) {
                        geoserverAux.setGeoserverUser(childrenAuxElement.getAttributeNode("value").getValue());
                    }
                    if (childrenAuxElement.getNodeName().equals("password")) {
                        geoserverAux
                                .setGeoserverPassword(childrenAuxElement.getAttributeNode("value").getValue());
                    }
                    if (childrenAuxElement.getNodeName().equals("version")) {
                        NodeList childrenListVersionAux = childrenAuxElement.getChildNodes();
                        for (int b = 0; b < childrenListVersionAux.getLength(); b++) {
                            Node childrenVersionAux = childrenListVersionAux.item(b);
                            if (childrenVersionAux instanceof Element) {
                                Element childrenElementVersionAux = (Element) childrenVersionAux;
                                if (childrenElementVersionAux.getNodeName().equals("wms")) {
                                    geoserverAux.setGeoserverWMSVersion(
                                            childrenElementVersionAux.getAttributeNode("value").getValue());
                                }
                                if (childrenElementVersionAux.getNodeName().equals("wfs")) {
                                    geoserverAux.setGeoserverWFSVersion(
                                            childrenElementVersionAux.getAttributeNode("value").getValue());
                                }
                            }
                        }

                    }

                }

            }
            geoservers.add(geoserverAux);
        }

    }

}

From source file:com.doculibre.constellio.feedprotocol.parse.xml.FeedParser.java

private String getAttrValueIfPresent(Element elementName, String attributeName) {
    String value = null;//w  w w . jav  a  2 s.com
    Attr attr = elementName.getAttributeNode(attributeName);
    if (attr != null && attr.getSpecified()) {
        value = attr.getValue();
    }
    return value;
}

From source file:gov.nih.nci.caadapter.ws.AddNewScenario.java

/**
* Update the reference in .map to the .scs and .h3s files.
*
* @param  mapplingFileName  mapping file name
*///from   w w w  .ja  v a 2s  .  co m
public void updateMapping(String mapplingBakFileName) throws Exception {
    Document xmlDOM = readFile(mapplingBakFileName);

    NodeList components = xmlDOM.getElementsByTagName("component");
    for (int i = 0; i < components.getLength(); i++) {
        Element component = (Element) components.item(i);
        //update location of SCS, H3S
        Attr locationAttr = component.getAttributeNode("location");
        Attr kindAttr = component.getAttributeNode("kind");
        if (locationAttr != null) {
            String cmpKind = "";
            if (kindAttr != null)
                cmpKind = kindAttr.getValue();
            if (cmpKind != null && cmpKind.equalsIgnoreCase("v2"))
                continue;
            String localName = extractOriginalFileName(locationAttr.getValue());
            locationAttr.setValue(localName);
        }
        //update VOM reference
        Attr groupAttr = component.getAttributeNode("group");
        if (groupAttr != null && groupAttr.getValue() != null
                && groupAttr.getValue().equalsIgnoreCase("vocabulary")) {
            NodeList chldComps = component.getElementsByTagName("data");
            for (int j = 0; j < chldComps.getLength(); j++) {
                Element chldElement = (Element) chldComps.item(j);
                Attr valueAttr = chldElement.getAttributeNode("value");
                if (valueAttr != null) {
                    String localFileName = extractOriginalFileName(valueAttr.getValue());
                    valueAttr.setValue(localFileName);
                }
            }
        }
    }
    System.out.println("AddNewScenario.updateMapping()..mapbakfile:" + mapplingBakFileName);

    outputXML(xmlDOM, mapplingBakFileName.substring(0, mapplingBakFileName.lastIndexOf(".bak")));
}

From source file:org.gvnix.dynamic.configuration.roo.addon.config.XpathAttributesDynamicConfiguration.java

/**
 * {@inheritDoc}//from www  .  ja  va 2 s .  co  m
 */
@Override
public void write(DynPropertyList dynProps) {

    // Get the XML file path
    MutableFile file = getFile();
    if (file != null) {

        // Obtain document representation of the XML file
        Document doc = getXmlDocument(file);

        // Update the root element property values with dynamic properties
        for (DynProperty dynProp : dynProps) {

            // Obtain the element related to this dynamic property
            String xpath = getXpath() + XPATH_ARRAY_PREFIX + XPATH_ATTRIBUTE_PREFIX + getKey()
                    + XPATH_EQUALS_SYMBOL + XPATH_STRING_DELIMITER + getKeyValue(dynProp.getKey())
                    + XPATH_STRING_DELIMITER + XPATH_ARRAY_SUFIX;
            Element elem = XmlUtils.findFirstElement(xpath, doc.getDocumentElement());
            if (elem == null) {

                logger.log(Level.WARNING, "Element " + xpath + " to set attribute value not exists on file");
            } else {

                // If value attribute exists, set new value
                Attr attr = elem.getAttributeNode(getValue());
                if (attr == null) {

                    logger.log(Level.WARNING,
                            "Element attribute " + xpath + " to set value not exists on file");
                } else {

                    attr.setValue(dynProp.getValue());
                }
            }
        }

        // Update the XML file
        XmlUtils.writeXml(file.getOutputStream(), doc);

    } else if (!dynProps.isEmpty()) {

        logger.log(Level.WARNING,
                "File " + getFilePath() + " not exists and there are dynamic properties to set it");
    }
}