Example usage for org.w3c.dom Document getElementsByTagName

List of usage examples for org.w3c.dom Document getElementsByTagName

Introduction

In this page you can find the example usage for org.w3c.dom Document getElementsByTagName.

Prototype

public NodeList getElementsByTagName(String tagname);

Source Link

Document

Returns a NodeList of all the Elements in document order with a given tag name and are contained in the document.

Usage

From source file:Main.java

/**
 * Returns the first node that matches the specified string.
 * /*from w  ww. j ava2 s .  com*/
 * @param d
 * @param s
 * @return Node object of the first node that matches name
 */
public static Node getFirstNodeByName(Document d, String s) {
    NodeList ns = d.getElementsByTagName(s);
    Node n = null;
    if (ns.getLength() > 0) {
        n = ns.item(0);
    }
    return n;
}

From source file:Main.java

/**
 * Get Element by according tag//from  w  ww .  j a v  a2  s .  co m
 * Only one such element should exist
 *
 * @param document document to search
 * @param tag tag to look for in document
 * @return corresponding element
 */
public static Element getSingleAppearingElementByTag(Document document, String tag) {
    NodeList elementList = document.getElementsByTagName(tag);
    if (elementList.getLength() != 1)
        return null;

    Node nNode = elementList.item(0);
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) nNode;
    }
    return null;
}

From source file:Main.java

static private Node getNode(Document doc, String nodeName, String textValue) {
    NodeList nodelist = doc.getElementsByTagName(nodeName);
    for (int i = 0; i < nodelist.getLength(); i++) {
        if (nodelist.item(i).getTextContent().equals(textValue)) {
            return nodelist.item(i);
        }//from  w w  w  .jav  a  2s. c  o  m
    }
    return null;
}

From source file:Main.java

public static Node getRootNode(Document doc, String nodeName) {
    NodeList nodes = doc.getElementsByTagName(nodeName);
    if (nodes == null || nodes.getLength() == 0)
        return (null);
    return (nodes.item(0));
}

From source file:Main.java

public static String getNodeValue(Document doc, String tagname) {
    if (doc != null)
        return ((Element) doc.getElementsByTagName(tagname).item(0)).getFirstChild().getNodeValue();
    else/*from  www.  j a  va2  s  . co m*/
        return "";
}

From source file:Main.java

/**
 * Returns the first element matching the given tag name.
 * @param doc the document from which to search the element
 * @param tagName the name of the tag to match
 * @return the element or null if the element cannot be found
 *//*  w w  w.  ja va 2s .  com*/
public static Element getFirstElementByTagName(Document doc, String tagName) {
    NodeList nodes = doc.getElementsByTagName(tagName);
    if (nodes.getLength() > 0) {
        return (Element) nodes.item(0);
    }
    return null;
}

From source file:Main.java

/** Returns the first element with the given name from a document.
 * /*  ww  w.  j a va  2  s.  c om*/
 *  @param doc Document to be searched
 *  @param name The element name to search for
 *  @return Node representing the element
 */
public static Node getNodeByName(Document doc, String name) {
    Node retVal = null;
    NodeList nl = doc.getElementsByTagName(name);
    if (nl != null && nl.getLength() != 0)
        retVal = nl.item(0);
    return retVal;
}

From source file:Main.java

public static Node getElementByTagName(Document document, String tagname) {

    NodeList nodeList = document.getElementsByTagName(tagname);

    if (nodeList != null && nodeList.getLength() > 0) {

        return nodeList.item(0);
    } else {// www .j  av a2  s. c om

        return null;
    }
}

From source file:Main.java

public static void createAttribute(Document doc, String nodeName, Map<String, String> map) {
    Node node = doc.getElementsByTagName(nodeName).item(0);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        for (Map.Entry<String, String> entry : map.entrySet()) {
            element.setAttribute(entry.getKey(), entry.getValue());
        }//from   ww w. j a  v  a 2 s  .c om
        //TransformerFactory factory = TransformerFactory.newInstance();
        //Transformer former = factory.newTransformer();
        //former.transform(new DOMSource(doc), new StreamResult(new File("src/shuiguo.xml")));
    }
}

From source file:Main.java

public static void setAllIdentityAttribute(Document document, String attributeValue) {
    NodeList propertyList = document.getElementsByTagName("Property");
    for (int i = 0; i < propertyList.getLength(); i++) {
        Node node = propertyList.item(i);
        for (int j = 0; j < node.getAttributes().getLength(); j++) {
            Node attribute = node.getAttributes().item(j);
            if (attribute.getNodeName().equals("name") && attribute.getNodeValue().equals(attributeValue)) {
                Element element = (Element) node;
                element.setAttribute("isIdentity", "true");
            }/*from  w  ww.j av a 2 s .  co  m*/
        }
    }
}