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:net.praqma.jenkins.rqm.model.TestSuiteExecutionRecord.java

@Override
public List<TestSuiteExecutionRecord> readMultiple(RqmParameterList parameters, BuildListener listener)
        throws IOException {
    List<TestSuiteExecutionRecord> tsers = new ArrayList<TestSuiteExecutionRecord>();
    RQMHttpClient client = null;// w  w  w  . j  a  v a2  s  . c o  m
    try {
        client = RQMUtilities.createClient(parameters);
    } catch (MalformedURLException ex) {
        log.logp(Level.SEVERE, this.getClass().getName(), "read",
                "Caught MalformedURLException in read throwing IO Exception", ex);
        throw new IOException("RqmMethodInvoker exception", ex);
    } catch (ClientCreationException cre) {
        log.logp(Level.SEVERE, this.getClass().getName(), "read",
                "Caught ClientCreationException in read throwing IO Exception", cre);
        throw new IOException("RqmMethodInvoker exception(ClientCreationException)", cre);
    }

    try {
        RQMGetRequest rq = new RQMGetRequest(client, parameters.requestString, parameters.parameterList);
        listener.getLogger()
                .println(String.format("Fetching test suite execution records using this url: %s", rq));
        Tuple<Integer, String> res = rq.executeRequest();
        listener.getLogger().println(String.format("Done fetching execution records"));

        if (res.t1 != 200) {
            throw new RequestException("Failed to load test suite execution record. Response written to log.",
                    res);
        }

        //Look for ns2:suiteexecutionrecord.
        //Add testplan and testsuite to the record
        Document doc = RqmObject.getDocumentReader(res.t2);

        NodeList n = doc.getElementsByTagName("ns2:suiteexecutionrecord");
        log.fine(String.format("Found %s test suite execution records", n.getLength()));

        for (int i = 0; i < n.getLength(); i++) {
            TestSuiteExecutionRecord record = new TestSuiteExecutionRecord();
            Node nl = n.item(i);
            if (nl.getNodeType() == Node.ELEMENT_NODE) {
                Element suiteElement = (Element) nl;
                String testSuiteExecutionRecordHref = suiteElement.getElementsByTagName("ns3:identifier")
                        .item(0).getTextContent();
                String tserTitle = suiteElement.getElementsByTagName("ns3:title").item(0).getTextContent();
                //TODO: This one can be null. Fix in the future, as you can get a null pointer exec
                Element testPlanHrefElement = ((Element) suiteElement.getElementsByTagName("ns2:testplan")
                        .item(0));
                if (testPlanHrefElement != null) {
                    record.setTestPlan(new TestPlan(testPlanHrefElement.getAttribute("href"))
                            .read(parameters, listener).get(0));
                } else {
                    record.setTestPlan(null);
                }
                String testSuiteHref = ((Element) suiteElement.getElementsByTagName("ns2:testsuite").item(0))
                        .getAttribute("href");
                /**
                 * Load the objects
                 */
                record.setRqmObjectResourceUrl(testSuiteExecutionRecordHref);
                record.setTestSuite(new TestSuite(testSuiteHref).read(parameters, listener).get(0));

                record.setTestSuiteExecutionRecordTitle(tserTitle);
            }

            tsers.add(record);
        }

    } catch (LoginException ex) {
        Logger.getLogger(TestSuiteExecutionRecord.class.getName()).log(Level.SEVERE, null, ex);
    } catch (RQMObjectParseException ex) {
        Logger.getLogger(TestSuiteExecutionRecord.class.getName()).log(Level.SEVERE, null, ex);
    }

    return tsers;
}

From source file:eu.guardiansystems.livesapp.android.ui.GMapDirections.java

public String getStartAddress(Document doc) {
    try {/*from   www. j  ava 2 s.com*/
        NodeList nl1 = doc.getElementsByTagName("start_address");
        Node node1 = nl1.item(0);

        return node1.getTextContent();
    } catch (Exception e) {
        return "-1";
    }

}

From source file:com.netscape.certsrv.client.PKIClient.java

public byte[] downloadCACertChain(String uri, String servletPath)
        throws ParserConfigurationException, SAXException, IOException {

    URL url = new URL(uri + servletPath);

    if (verbose)//from   w  ww .ja v a 2s  .  c o  m
        System.out.println("Retrieving CA certificate chain from " + url + ".");

    DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();

    Document document = documentBuilder.parse(url.openStream());
    NodeList list = document.getElementsByTagName("ChainBase64");
    Element element = (Element) list.item(0);

    String encodedChain = element.getTextContent();
    byte[] bytes = Utils.base64decode(encodedChain);

    if (verbose) {
        System.out.println("-----BEGIN PKCS7-----");
        System.out.print(new Base64(64).encodeToString(bytes));
        System.out.println("-----END PKCS7-----");
    }

    return bytes;
}

From source file:eu.guardiansystems.livesapp.android.ui.GMapDirections.java

public int getDurationValue(Document doc) {
    try {//w  w  w  .ja v a 2 s.  c  om
        NodeList nl1 = doc.getElementsByTagName("duration");
        Node node1 = nl1.item(0);
        NodeList nl2 = node1.getChildNodes();
        Node node2 = nl2.item(getNodeIndex(nl2, "value"));

        return Integer.parseInt(node2.getTextContent());
    } catch (Exception e) {
        return -1;
    }
}

From source file:com.cyc.corpus.nlmpaper.AIMedOpenAccessPaper.java

public AIMedArticle(String xmlFileContents) {
    taggedProteins = new HashSet<>();

    String[] lines = xmlFileContents.split("\n");

    try {//from  www  .j av a  2  s  .  co  m
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        for (String line : lines) {
            InputSource is = new InputSource(new StringReader(line));
            Document document = docBuilder.parse(is);
            NodeList nodeList = document.getElementsByTagName("*");

            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);

                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    final String nodeName = node.getNodeName();
                    final String textContent = node.getTextContent();

                    if (nodeName.equalsIgnoreCase("ArticleTitle")) {
                        titleSectionText = textContent;
                    } else if (nodeName.equalsIgnoreCase("AbstractText")) {
                        abstractSectionText = textContent;
                    } else if (nodeName.equalsIgnoreCase("prot")) {
                        taggedProteins.add(textContent);
                    }
                }
            }
        }
    } catch (DOMException | IOException | ParserConfigurationException | SAXException ex) {
        Logger.getLogger(AIMedOpenAccessPaper.class.getName()).log(Level.SEVERE, null, ex);
        //throw new RuntimeException(ex);
    }
}

From source file:Importers.ImportReportCompiler.java

@Override
public DefaultMutableTreeNode readFile(File importFile) {
    System.out.println("==ImportReportCompiler=readFile");
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("vulns");
    try {/*from  w  w  w .ja  va2  s.co  m*/

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(importFile);
        doc.normalize();

        NodeList vulns_list = doc.getElementsByTagName("vuln");
        for (int i = 0; i < vulns_list.getLength(); i++) {
            Node vuln_node = vulns_list.item(i);
            Vulnerability vuln = getVuln(vuln_node);
            root.add(new DefaultMutableTreeNode(vuln));
        }

    } catch (ParserConfigurationException ex) {
        Logger.getLogger(NessusV2XMLImporter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SAXException ex) {
        Logger.getLogger(NessusV2XMLImporter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(NessusV2XMLImporter.class.getName()).log(Level.SEVERE, null, ex);
    }

    return root;
}

From source file:de.ingrid.mdek.mapping.ScriptImportDataMapper.java

/** Set current xml exchange format from Versioning in import-export.jar ! */
private void setDocumentExchangeFormat(Document docTarget) {
    NodeList nodeList = docTarget.getElementsByTagName(XMLKeys.IGC);

    // !!! NOTICE !!!
    // Will be linked STATICALLY at compile time (all frontend class files) !!!
    // So will not be read from import-export jar at runtime !!!
    String exchangeFormat = Versioning.CURRENT_IMPORT_EXPORT_VERSION;

    setAttributesInNodeList(nodeList, XMLKeys.EXCHANGE_FORMAT, exchangeFormat);
}

From source file:eu.guardiansystems.livesapp.android.ui.GMapDirections.java

public String getDurationText(Document doc) {
    try {/*from  w w  w  .ja  va2  s . c  o  m*/

        NodeList nl1 = doc.getElementsByTagName("duration");
        Node node1 = nl1.item(0);
        NodeList nl2 = node1.getChildNodes();
        Node node2 = nl2.item(getNodeIndex(nl2, "text"));

        return node2.getTextContent();
    } catch (Exception e) {
        return "0";
    }
}

From source file:lv.coref.io.MmaxReaderWriter.java

public void readCoreferences(Text text, String fileName) {
    try {/*from w w w . j av a 2s. com*/
        File file = new File(fileName);
        DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = dBuilder.parse(file);
        NodeList markables = doc.getElementsByTagName("markable");
        int mentionId = 1;
        int twinlessMCId = 10000; // ids for twinless mentions
        for (int i = 0; i < markables.getLength(); i++) {
            Node markable = markables.item(i);
            String spanString = (markable.getAttributes().getNamedItem("span") != null)
                    ? markable.getAttributes().getNamedItem("span").getNodeValue()
                    : null;
            String category = (markable.getAttributes().getNamedItem("category") != null)
                    ? markable.getAttributes().getNamedItem("category").getNodeValue()
                    : null;
            String type = (markable.getAttributes().getNamedItem("type") != null)
                    ? markable.getAttributes().getNamedItem("type").getNodeValue()
                    : null;
            String rule = (markable.getAttributes().getNamedItem("rule") != null)
                    ? markable.getAttributes().getNamedItem("rule").getNodeValue()
                    : null;
            String headString = (markable.getAttributes().getNamedItem("heads") != null)
                    ? markable.getAttributes().getNamedItem("heads").getNodeValue()
                    : "";
            headString = StringEscapeUtils.unescapeXml(headString);
            String corefString = markable.getAttributes().getNamedItem("coref_class").getNodeValue();

            String id = null;
            if (corefString.startsWith("set_"))
                id = corefString.substring(4);
            else
                id = Integer.toString(twinlessMCId++);
            Pair<Integer, Integer> span = getSpanFromString(spanString, "word_");
            int start = span.first - 1;
            int end = span.second - 1;
            Token startToken = text.getToken(start);
            Token endToken = text.getToken(end);
            List<Token> tokens = startToken.getSentence().subList(startToken.getPosition(),
                    endToken.getPosition() + 1);
            List<Token> heads = getHeadsFromString(headString, tokens);
            Mention m = new Mention(Integer.toString(mentionId++), tokens, heads);
            m.setCategory(category);
            m.setType(Constants.Type.valueOf(type.toUpperCase()));

            startToken.getSentence().addMention(m);
            MentionChain mc = text.getMentionChain(id);
            if (mc == null) {
                mc = new MentionChain(id, m);
                startToken.getSentence().getText().addMentionChain(mc);
            } else {
                mc.add(m);
            }
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, "Error reading " + fileName, e);
    }
}

From source file:org.urbanstew.soundcloudapi.test.RequestTest.java

private int getId(HttpResponse response) throws Exception {
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document dom = db.parse(response.getEntity().getContent());

    return Integer.parseInt(dom.getElementsByTagName("id").item(0).getFirstChild().getNodeValue());
}