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:it.sasabz.android.sasabus.classes.hafas.XMLRequest.java

public static boolean containsError(String xml) {
    SASAbusXML parser = new SASAbusXML();

    Document doc = parser.getDomElement(xml);

    NodeList nl = doc.getElementsByTagName("Err");

    if (nl.getLength() == 0) {
        return false;
    }//from  w  ww. j  ava  2s .  com
    return true;

}

From source file:Main.java

public static Object getBean(String args) {
    try {/*  w w  w .j a v  a  2s .c  om*/
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc;
        doc = builder.parse(new File("config.xml"));
        NodeList nl = null;
        Node classNode = null;
        String cName = null;
        nl = doc.getElementsByTagName(args);

        classNode = nl.item(0).getFirstChild();
        cName = classNode.getNodeValue();

        Class c = Class.forName(cName);
        Object obj = c.newInstance();
        return obj;

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

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

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));

    NodeList nodeList = document.getDocumentElement().getChildNodes();
    Node reqName = document.getElementsByTagName("request-name").item(0);
    System.out.println(reqName.getTextContent());

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        System.out.println(i + "--" + node);
        if (node instanceof Element) {
            NodeList childNodes = node.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node cNode = childNodes.item(j);
                System.out.println(i + "--" + j + "--" + cNode);
                if (cNode instanceof Element) {
                    String content = cNode.getLastChild().getTextContent().trim();
                    System.out.println(i + "--" + j + "--" + content);
                }//from ww  w  .j ava2  s . co  m
            }
        }
    }
    /*
     * Do the parsing for reqname
     */
    return reqName.getTextContent();
}

From source file:com.amalto.core.initdb.InitDBUtil.java

private static void parseInitMap(InputStream in, DocumentBuilder builder, Map<String, List<String>> initMap)
        throws Exception {
    Document doc = builder.parse(in);
    NodeList nodelist = doc.getElementsByTagName("item"); //$NON-NLS-1$
    for (int i = 0; i < nodelist.getLength(); i++) {
        Node node = nodelist.item(i);
        NodeList list = node.getChildNodes();
        String name = null;/*from w ww.j  a  v  a 2  s.  c  om*/
        for (int j = 0; j < list.getLength(); j++) {
            Node n = list.item(j);
            if (n instanceof Element) {
                if ("name".equals(n.getNodeName())) { //$NON-NLS-1$
                    name = n.getTextContent();
                    if (initMap.get(name) == null) {
                        initMap.put(name, new ArrayList<String>());
                    }
                }
                if ("list".equals(n.getNodeName())) { //$NON-NLS-1$
                    if (n.getTextContent() == null || n.getTextContent().trim().length() == 0) {
                        continue;
                    }
                    List<String> lists = initMap.get(name);
                    String[] arr = n.getTextContent().split(";"); //$NON-NLS-1$
                    lists.addAll(Arrays.asList(arr));
                }
            }
        }
    }
}

From source file:Main.java

/**
 * Checks whether the specified XML text has one and only one tag of the specified tag name.
 * //  w  w w  .  ja v  a 2 s . c  o m
 * @param xmlText
 * @param tagName
 * @return True if the specified XML text has one and only one tag of the specified tag name;
 *         false otherwise.
 */
public static Boolean hasSingleTag(String xmlText, String tagName) {
    Document doc = getDomDocument(xmlText);
    if (doc == null) {
        return false;
    }
    NodeList nodes = doc.getElementsByTagName(tagName);

    return hasUniqueNode(nodes);
}

From source file:Main.java

public static Document removeFromRootElement(Document document, String nodeName, String attributeName,
        String attributeValue) {/*from w  w  w  .  ja v  a2s.c  o m*/
    try {
        Element rootElement = document.getDocumentElement();

        NodeList nl = document.getElementsByTagName(nodeName);
        Vector<Node> deletedNodes = new Vector<Node>();

        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            String noteAttributeValue = node.getAttributes().getNamedItem(attributeName).getNodeValue();

            if (noteAttributeValue.equals(attributeValue)) {
                deletedNodes.add(node);
            }

        }

        for (Node deletedNode : deletedNodes) {
            rootElement.removeChild(deletedNode);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return document;
}

From source file:me.willowcheng.makerthings.util.Util.java

public static List<OpenHABSitemap> parseSitemapList(Document document) {
    List<OpenHABSitemap> sitemapList = new ArrayList<OpenHABSitemap>();
    NodeList sitemapNodes = document.getElementsByTagName("sitemap");
    if (sitemapNodes.getLength() > 0) {
        for (int i = 0; i < sitemapNodes.getLength(); i++) {
            Node sitemapNode = sitemapNodes.item(i);
            OpenHABSitemap openhabSitemap = new OpenHABSitemap(sitemapNode);
            sitemapList.add(openhabSitemap);
        }/*from w  w w.j a v a 2s  .c o m*/
    }
    return sitemapList;
}

From source file:Main.java

private static void insertNode(String nodeName, String nodeValue) {
    File f = new File(System.getProperty("user.dir") + "\\DBConf.xml");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;//from w  w  w .j  av  a 2s. co  m
    try {
        builder = factory.newDocumentBuilder();
        Document doc = builder.parse(f);
        Element tempElem = doc.createElement(nodeName);
        Text child = doc.createTextNode(nodeValue);
        tempElem.appendChild(child);
        doc.getElementsByTagName("conf").item(0).appendChild(tempElem);
        DOMSource source = new DOMSource(doc);
        StreamResult Streamres = new StreamResult(new FileOutputStream(f));
        TransformerFactory.newInstance().newTransformer().transform(source, Streamres);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.connexta.arbitro.ctx.InputParser.java

/**
 * Tries to Parse the given output as a Context document.
 * //from   w  w w  . j  a va2s  . c o  m
 * @param input the stream to parse
 * @param rootTag either "Request" or "Response"
 * 
 * @return the root node of the request/response
 * 
 * @throws ParsingException if a problem occurred parsing the document
 */
public static Node parseInput(InputStream input, String rootTag) throws ParsingException {
    NodeList nodes = null;

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringComments(true);

        DocumentBuilder builder = null;

        // as of 1.2, we always are namespace aware
        factory.setNamespaceAware(true);

        if (ipReference == null) {
            // we're not validating
            factory.setValidating(false);

            builder = factory.newDocumentBuilder();
        } else {
            // we are validating
            factory.setValidating(true);

            factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
            factory.setAttribute(JAXP_SCHEMA_SOURCE, ipReference.schemaFile);

            builder = factory.newDocumentBuilder();
            builder.setErrorHandler(ipReference);
        }

        Document doc = builder.parse(input);
        nodes = doc.getElementsByTagName(rootTag);
    } catch (Exception e) {
        throw new ParsingException("Error tring to parse " + rootTag + "Type", e);
    }

    if (nodes.getLength() != 1)
        throw new ParsingException("Only one " + rootTag + "Type allowed " + "at the root of a Context doc");

    return nodes.item(0);
}

From source file:Main.java

public static NodeList findNodes(Document document, String nodeName) {
    if (document == null)
        throw new NullPointerException("Document cannot be null!");
    if (nodeName == null)
        throw new NullPointerException("Nodename cannot be null!");
    return document.getElementsByTagName(nodeName);
}