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

public static String parsePrefValue(Document doc, String attrName) {
    NodeList nodes = doc.getElementsByTagName(TAG_PREFERENCE);
    int len = (null == nodes ? 0 : nodes.getLength());
    for (int i = 0; i < len; i++) {
        Node node = nodes.item(i);
        if (null != node) {
            String parseName = ((Element) node).getAttribute(ATTR_NAME);
            if (parseName.equals(attrName)) {
                return ((Element) node).getAttribute(ATTR_VALUE);
            }//from   ww  w.  j a v a 2  s  .  c o  m
        }
    }
    return null;
}

From source file:Main.java

public static String getBrandName() {
    try {/*from w w  w  . ja v a2 s  .  c o  m*/
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc = builder.parse(new File("configTV.xml"));

        NodeList nl = doc.getElementsByTagName("Name");
        Node classNode = nl.item(0).getFirstChild();
        String Name = classNode.getNodeValue().trim();
        return Name;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String getCharttype() {
    try {//from ww  w .ja v a  2s.co  m
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc = builder.parse(new File(configPath));

        NodeList nl = doc.getElementsByTagName("chartType");
        Node classNode = nl.item(0).getFirstChild();
        String chartType = classNode.getNodeValue().trim();
        return chartType;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Element getElementWithKeyFromDocument(final Document document, final String key) {
    final NodeList nodeList = document.getElementsByTagName(key);
    Element result = null;//from   w ww  .jav  a 2 s . co  m
    if (nodeList.getLength() > 0) {
        result = (Element) nodeList.item(0);
    }
    return result;
}

From source file:Main.java

public static Object getBean(String className) {
    try {//w w  w .  j  a  v  a  2  s  . c  o m
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc = builder.parse(new File(configPath));

        NodeList nl = doc.getElementsByTagName(className);
        Node classNode = nl.item(0).getFirstChild();
        String chartType = classNode.getNodeValue().trim();

        Class c = Class.forName(chartType);
        return c.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Gets the first node with the specified tag name from the given document.
 * If there are multiple nodes with the specified tag name, the first node
 * found will be returned./*from w w w. j av  a 2 s.c  o m*/
 * 
 * @param doc
 *            the document
 * @param tagname
 *            the tag name of the node to find.
 * @return the first node found with the specified tag name or
 *         <code>null</code> if no node with that name could be found.
 */
public static Node getNode(Document doc, String tagname) {
    NodeList nl = doc.getElementsByTagName(tagname);
    if (!isEmpty(nl)) {
        return nl.item(0);
    }
    return null;
}

From source file:Main.java

/**
 * This method is used for updating the value of a tag in a
 * <code>Document</code> object.
 * /*from   w  ww  . ja  va 2 s  . c om*/
 * @param doc
 *            Document object
 * @param tagName
 *            name of the tag
 * @param tagValue
 *            the updated value of the tag
 */
public static void replaceTagValue(Document doc, String tagName, String tagValue) {
    NodeList nodeList = doc.getElementsByTagName(tagName);
    int j = nodeList.getLength();
    Node node;
    for (int i = 0; i < j; i++) {
        Node newNode = doc.createTextNode(tagValue);
        node = nodeList.item(i);
        if (node.getFirstChild() != null) {
            node.replaceChild(newNode, node.getFirstChild());
        } else {
            node.appendChild(newNode);
        }
    }
}

From source file:Main.java

/**
 * Get the value of the attribute with the given name of the first tag found with the given tag name in the given
 * document<br/>./*w w w . ja  va 2 s.c  om*/
 *
 * @param doc
 * @param tagName
 * @param attributeName
 * @return the value of the attribute with the given name of the node or the empty string, if no node with this name
 * exits in this document or the attribute does not exist
 */
public static String getTagAttributeValue(Document doc, String tagName, String attributeName) {
    NodeList tagList = doc.getElementsByTagName(tagName);
    if (tagList.getLength() > 0) {
        NamedNodeMap attributes = tagList.item(0).getAttributes();
        if (attributes != null) {
            Node attribute = attributes.getNamedItem(attributeName);
            if (attribute != null) {
                return attribute.getNodeValue().trim();
            }
        }
    }
    return "";
}

From source file:Main.java

public static String getText(Document doc, String tagName, boolean flag) {

    NodeList nodeList = doc.getElementsByTagName(tagName);
    int leng = nodeList.getLength();
    if (leng <= 0)
        return "";

    Random rnd = new Random();
    int ran = rnd.nextInt(leng);

    return nodeList.item(ran).getTextContent();
}

From source file:Main.java

/**
 * This method is used to insert a new tag below the tag specified by
 * <code>appendTo</code> parameter.
 * //ww  w .j  a va2s . com
 * @param d
 *            the <code>Document</code> object to which a new tag is to be
 *            inserted.
 * @param appendTo
 *            the tag below which a new tag needs to be inserted.
 * @param tagName
 *            the name of new tag
 * @param tagValue
 *            the value of new tag
 */
public static Element insertNewTagBelow(Document d, String appendTo, String tagName, String tagValue) {
    Node element = d.getElementsByTagName(appendTo).item(0);
    if (element == null) {
        element = d.createElement(appendTo);
    }
    Element newElement = d.createElement(tagName);
    element.appendChild(newElement);
    newElement.appendChild(d.createTextNode(tagValue));
    return newElement;
}