Example usage for org.w3c.dom Element getAttribute

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

Introduction

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

Prototype

public String getAttribute(String name);

Source Link

Document

Retrieves an attribute value by name.

Usage

From source file:Main.java

/**
 * Are elements equal.//ww w .  ja v a  2 s .  c o m
 * 
 * @param element1
 *            the element1
 * @param element2
 *            the element2
 * @return true, if successful
 */
public static boolean areElementsEqual(Element element1, Element element2) {
    if (!element1.getTagName().equals(element2.getTagName())) {
        return false;
    }
    NamedNodeMap nodeAttrMap = element1.getAttributes();
    NamedNodeMap pathAttrMap = element2.getAttributes();
    if ((nodeAttrMap == null && pathAttrMap == null)
            || (pathAttrMap.getLength() == 0 && nodeAttrMap.getLength() == 0)) {
        return true;
    } else {
        if (element1.hasAttribute("name") && element2.hasAttribute("name")) {
            if (element1.getAttribute("name").equals(element2.getAttribute("name"))) {
                return true;
            }
        } else if (nodeAttrMap != null && pathAttrMap != null
                && (nodeAttrMap.getLength() == pathAttrMap.getLength())) {
            for (int k = 0; k < nodeAttrMap.getLength(); k++) {
                Node nodeAttr = nodeAttrMap.item(k);
                String nodeAttrName = nodeAttr.getNodeName();
                String nodeAttrValue = nodeAttr.getNodeValue();
                if (element2.hasAttribute(nodeAttrName)
                        && nodeAttrValue.equals(element2.getAttribute(nodeAttrName))) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:com.bluexml.side.form.utils.DOMUtil.java

/**
 * Removes the child element which has a the specified attribute and value.
 * //from w w w .j  av a  2s . co  m
 * @param node
 *            the root of the tree to search
 * @param tagName
 * @return whether something was removed
 */
public static boolean removeEltInDescentByAttrValue(Element node, String attr, String value) {
    boolean res;

    if (node != null) {
        List<Element> children = getAllChildren(node);
        for (Element elt : children) {
            String actualValue = elt.getAttribute(attr);
            if (StringUtils.equals(actualValue, value)) {
                node.removeChild(elt);
                return true;
            }
        }
        // not found yet, try in each child
        for (Element elt : children) {
            res = removeEltInDescentByAttrValue(elt, attr, value);
            if (res == true) {
                return true;
            }
        }
    }
    return false;
}

From source file:XMLUtils.java

public static String getAttribute(Element el, String att) {
    String str = el.getAttribute(att);
    if (str == null || str.length() == 0) {
        return null;
    } else {/*from  ww  w  . j a  v  a  2s  . co m*/
        return str;
    }
}

From source file:gov.nist.healthcare.ttt.parsing.Parsing.java

public static String getPatientIDFromWsse(String mtom)
        throws IOException, MessagingException, SAXException, ParserConfigurationException {
    String wsseHeader = Parsing.getWsseHeaderFromMTOM(mtom);
    if (wsseHeader == null)
        return ""; // NOSAML
    String patientId = null;//from   www . j a v  a 2 s .  co  m
    //  Node security = JAXB.unmarshal(new StringReader(wsseHeader), Node.class);
    Node securityDoc = MiscUtil.stringToDom(wsseHeader);

    Node security = securityDoc.getFirstChild();

    NodeList securityChildren = security.getChildNodes();
    for (int i = 0; i < securityChildren.getLength(); i++) {
        Node securityChild = securityChildren.item(i);
        if (securityChild.getLocalName() != null && securityChild.getLocalName().equals("Assertion")
                && securityChild.getNamespaceURI().equals("urn:oasis:names:tc:SAML:2.0:assertion")) {
            Node assertion = securityChild;
            NodeList assertionChildren = assertion.getChildNodes();
            for (int j = 0; j < assertionChildren.getLength(); j++) {
                Node assertionChild = assertionChildren.item(j);

                if (assertionChild.getLocalName().equals("AttributeStatement")
                        && assertionChild.getNamespaceURI().equals("urn:oasis:names:tc:SAML:2.0:assertion")) {
                    Node attributeStatement = assertionChild;
                    NodeList attributeStatementChildren = attributeStatement.getChildNodes();
                    for (int k = 0; k < attributeStatementChildren.getLength(); k++) {
                        Node attributeStatementChild = attributeStatementChildren.item(k);
                        if (attributeStatementChild.getLocalName().equals("Attribute")
                                && attributeStatementChild.getNamespaceURI()
                                        .equals("urn:oasis:names:tc:SAML:2.0:assertion")) {
                            Element attribute = (Element) attributeStatementChild;
                            if (attribute.getAttribute("Name")
                                    .equals("urn:oasis:names:tc:xacml:2.0:resource:resource-id")) {
                                NodeList attributeChildren = attribute.getChildNodes();
                                for (int l = 0; l < attributeChildren.getLength(); l++) {
                                    Node attributeChild = attributeChildren.item(l);
                                    if (attributeChild.getLocalName().equals("AttributeValue")
                                            && attributeChild.getNamespaceURI()
                                                    .equals("urn:oasis:names:tc:SAML:2.0:assertion")) {
                                        Node attributeValue = attributeChild;
                                        return attributeValue.getFirstChild().getNodeValue();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return patientId;
}

From source file:Main.java

/**
 * Get the value of a node specified by a starting node and a
 * path string. If the starting node is a Document, use the
 * document element as the starting point.
 * <ul>//from  w  w w. ja v a  2 s.com
 * <li>A path to an element has the form: elem1/.../elemN
 * <li>A path to an attribute has the form: elem1/.../elemN@attr
 * </ul>
 * The value of an element node is the sum of all the element's
 * first generation child text nodes. Note that this is not what you
 * would get from a mixed element in an XSL program.
 * @param node the top of the tree to search.
 * @param path the path from the top of the tree to the desired node.
 * @return the value of the first node matching the path, or the
 * empty string if no node exists at the path location or if the
 * starting node is not an element.
 */
public static String getValueViaPath(Node node, String path) {
    if (node instanceof Document)
        node = ((Document) node).getDocumentElement();
    if (!(node instanceof Element))
        return "";
    path = path.trim();
    int kAtsign = path.indexOf("@");

    //If the target is an element, get the element's value.
    if (kAtsign == -1) {
        Element target = getElementViaPath(node, path);
        if (target == null)
            return "";
        return target.getTextContent();
    }

    //The target is an attribute; first find the element.
    String subpath = path.substring(0, kAtsign);
    Element target = getElementViaPath(node, subpath);
    if (target == null)
        return null;
    String name = path.substring(kAtsign + 1);
    return target.getAttribute(name);
}

From source file:com.vmware.qe.framework.datadriven.utils.XMLUtil.java

public static Vector<String> getValueOnTag(Document doc, String elementName) {
    Vector<String> elementNames = new Vector<String>();
    NodeList list = doc.getElementsByTagName(elementName);
    log.info("XML Elements: ");
    for (int i = 0; i < list.getLength(); i++) {
        Element element = (Element) list.item(i);
        elementNames.add(element.getAttribute("name"));
        log.info("Element Name : " + element.getLocalName() + " Value :" + element.getAttribute("name"));
    }//from   w  w  w. ja v  a 2 s.  c om
    return elementNames;
}

From source file:com.impetus.kundera.loader.PersistenceXMLLoader.java

/**
 * Find persistence units./* w ww  .  j  a va  2  s.c om*/
 * 
 * @param url
 *            the url
 * @param defaultTransactionType
 *            the default transaction type
 * @return the list
 * @throws Exception
 *             the exception
 */
public static List<PersistenceUnitMetadata> findPersistenceUnits(final URL url, final String[] persistenceUnits,
        PersistenceUnitTransactionType defaultTransactionType) throws InvalidConfigurationException {
    Document doc;
    try {
        doc = getDocument(url);
    } catch (InvalidConfigurationException e) {
        throw e;
    }
    doc.getXmlVersion();
    Element top = doc.getDocumentElement();

    String versionName = top.getAttribute("version");

    NodeList children = top.getChildNodes();
    ArrayList<PersistenceUnitMetadata> units = new ArrayList<PersistenceUnitMetadata>();

    // parse for persistenceUnitRootInfoURL.
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) children.item(i);
            String tag = element.getTagName();
            // look for "persistence-unit" element
            if (tag.equals("persistence-unit")) {
                PersistenceUnitMetadata metadata = parsePersistenceUnit(url, persistenceUnits, element,
                        versionName);
                if (metadata != null) {
                    units.add(metadata);
                }
            }
        }
    }
    return units;
}

From source file:com.bluexml.side.form.utils.DOMUtil.java

/**
 * Retrieves a specific element based on attribute value anywhere below a node.
 * //  w w  w  .  java  2  s  .c  o  m
 * @param node
 *            the root of the tree to search
 * @param tagName
 * @return the first Element that matches the tagName
 */
public static Element getEltInDescentByAttrValue(Element node, String attr, String value) {
    if (node != null) {
        String actualValue = node.getAttribute(attr);
        if (StringUtils.equals(actualValue, value)) {
            return node;
        }
        List<Element> children = getAllChildren(node);
        for (Element elt : children) {
            Element res = getEltInDescentByAttrValue(elt, attr, value);
            if (res != null) {
                return res;
            }
        }
    }
    return null;
}

From source file:com.vmware.qe.framework.datadriven.utils.XMLUtil.java

/**
 * Gets the descendant elements which have an attribute with the specified name and value
 * //from   w  w  w  .j av  a 2  s.c o  m
 * @param parentNode - Node whose children are to be searched
 * @param attrId - attribute name
 * @param attrVal - attribute value
 * @return List of all children nodes
 * @throws Exception
 */
public static List<Element> getElements(Node parentNode, String attrId, String attrVal) throws Exception {
    NodeList nodeList = parentNode.getChildNodes();
    List<Element> elements = new ArrayList<Element>();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node child = nodeList.item(i);
        if (child instanceof Element) {
            Element element = (Element) child;
            if (attrVal.equalsIgnoreCase(element.getAttribute(attrId))) {
                elements.add(element);
            }
            if (element.hasChildNodes()) {
                // recursive call to search for child's children
                List<Element> children = getElements(element, attrId, attrVal);
                if (children != null) {
                    elements.addAll(children);
                }
            }
        }
    }
    return elements.isEmpty() ? null : elements;
}

From source file:XMLUtils.java

/**
 * Retrieve the namespace for a given prefix. 
 * Does a lookup into the parent hierarchy.
 * @param el/*from w  w  w .  j  ava  2  s .c  o m*/
 * @param prefix
 * @return
 */
public static String getNamespace(Element el, String prefix) {
    Element parent = el;
    while (parent != null) {
        String ns = parent.getAttribute("xmlns:" + prefix);
        if (ns != null && ns.length() > 0) {
            return ns;
        }
        // get the parent
        Node n = parent.getParentNode();
        if (n instanceof Element) {
            parent = (Element) n;
        } else {
            parent = null;
        }
    }
    // nothing found
    return null;
}