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

private static void updateNode(String nodeName, String value) throws Exception {
    File f = new File(System.getProperty("user.dir") + "\\DBConf.xml");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(f);
    Node tempElem = doc.getElementsByTagName(nodeName).item(0);
    tempElem.getFirstChild().setNodeValue(value);
    doc.getElementsByTagName("conf").item(0).appendChild(tempElem);
    DOMSource source = new DOMSource(doc);
    StreamResult res = new StreamResult(new FileOutputStream(f));
    TransformerFactory.newInstance().newTransformer().transform(source, res);

}

From source file:Main.java

public static Object getBean() {
    try {//  w  w  w  . j  a va2  s  .  com
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc = builder.parse(new File("src/config.xml"));

        NodeList nl = doc.getElementsByTagName("className");
        Node node = nl.item(0).getFirstChild();
        String cName = node.getNodeValue();

        Class clazz = Class.forName(cName);
        Object obj = clazz.newInstance();
        return obj;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static List<String> parseXmlFile(String nodeName, Document file) {
    List<String> nodeTexts = new ArrayList<String>();
    NodeList nodes = file.getElementsByTagName(nodeName);
    for (int index = 0; index < nodes.getLength(); ++index) {
        Node node = nodes.item(index).getFirstChild();
        String nodeText = node.getNodeValue();
        nodeTexts.add(nodeText);//from   w  w  w.  j av  a2  s .c  om
    }
    return nodeTexts;
}

From source file:Main.java

/**
 * Returns the inner value of the named node in the given document.  The node must only exist once
 *
 * @param doc the XML document//from w w w.  j  a va  2 s .c  o  m
 * @param nodeName the name of the node
 * @return the value of the named node
 */
public static String getNodeInnerValue(final Document doc, final String nodeName) throws SAXException {
    final NodeList nodes = doc.getElementsByTagName(nodeName);
    if (nodes.getLength() != 1) {
        throw new SAXException("Could not get single node for " + nodeName);
    }
    final NodeList childNodes = nodes.item(0).getChildNodes();
    if (childNodes.getLength() != 1) {
        throw new SAXException("Could not get single child node for " + nodeName);
    }
    return childNodes.item(0).getNodeValue();
}

From source file:Main.java

public static String getFirstChildNodeAttributeValue(Document parent, String nodeName, String attributeName) {
    return parent.getElementsByTagName(nodeName).item(0).getAttributes().getNamedItem(attributeName)
            .getNodeValue();//from www.ja  va 2s. com
}

From source file:Main.java

public static void moveElement(Document fromDoc, Document toDoc, Element root, String elementName) {
    NodeList list = fromDoc.getElementsByTagName(elementName);
    if ((list != null) && (list.getLength() > 0)) {
        Element element = (Element) list.item(0);
        Node node = toDoc.importNode(element, true);
        root.appendChild(node);//from  w  ww .  j  a  v a 2  s.  c  o  m
        element.getParentNode().removeChild(element);
    }
}

From source file:Main.java

public static String getElementValueByTagName(Document doc, String parentName, String eleName) {

    NodeList nl = doc.getElementsByTagName(parentName);
    if (null == nl) {
        return null;
    }/*from ww w.  j  a  v a 2 s .c om*/
    Node item = nl.item(0);
    return getChildElementValueByTagName((Element) item, eleName);
}

From source file:Main.java

public static String getRequestID(String xml) throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    Node reqName = document.getElementsByTagName("request-id").item(0);
    if (reqName != null)
        return reqName.getTextContent();
    else/*w ww .  ja v a2  s .c  o m*/
        return null;
}

From source file:Main.java

public static String getRequestName(String xml) throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    Node reqName = document.getElementsByTagName("request-name").item(0);
    if (reqName != null)
        return reqName.getTextContent();
    else// w  w w.  j  a  va  2  s  .c  o  m
        return null;
}

From source file:Main.java

public static String getUnicastID(String xml) throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    Node reqName = document.getElementsByTagName("unicast-id").item(0);
    if (reqName != null)
        return reqName.getTextContent();
    else// w  ww .j  a v a  2 s.c  o  m
        return null;
}