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:co.edu.uniajc.vtf.utils.GMapV2Direction.java

public int getDistanceValue(Document doc) {
    NodeList nl1 = doc.getElementsByTagName("distance");
    Node node1 = nl1.item(0);/*w w  w.ja va  2s . co  m*/
    NodeList nl2 = node1.getChildNodes();
    Node node2 = nl2.item(getNodeIndex(nl2, "value"));
    Log.i("DistanceValue", node2.getTextContent());
    return Integer.parseInt(node2.getTextContent());
}

From source file:com.k42b3.neodym.Services.java

private void request(String url) throws Exception {
    // request//from w ww  .j a v a  2 s .  c om
    Document doc = http.requestXml(Http.GET, url);

    // parse services
    NodeList serviceList = doc.getElementsByTagName("Service");

    for (int i = 0; i < serviceList.getLength(); i++) {
        Node serviceNode = serviceList.item(i);
        Element serviceElement = (Element) serviceNode;

        NodeList typeElementList = serviceElement.getElementsByTagName("Type");
        Element uriElement = (Element) serviceElement.getElementsByTagName("URI").item(0);

        if (typeElementList.getLength() > 0 && uriElement != null) {
            ArrayList<String> types = new ArrayList<String>();

            for (int j = 0; j < typeElementList.getLength(); j++) {
                types.add(typeElementList.item(j).getTextContent());
            }

            String uri = uriElement.getTextContent();

            services.add(new ServiceItem(uri, types));
        }
    }

    logger.info("Found " + services.size() + " services");
}

From source file:de.jcup.egradle.sdk.builder.model.OriginXMLDSlTypeInfoImporter.java

public XMLDSLTypeDocumentation collectDSL(File file) throws IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    File lastParsedFile = null;/*ww  w  .j a  va 2s  .  c o m*/
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();

        lastParsedFile = file;
        try (FileInputStream fis = new FileInputStream(file)) {

            XMLDSLTypeDocumentation target = new XMLDSLTypeDocumentation();

            Document document = builder.parse(fis);

            NodeList section1s = document.getElementsByTagName("section");
            for (int i = 0; i < section1s.getLength(); i++) {
                Element section1 = (Element) section1s.item(i);
                NodeList section2s = section1.getElementsByTagName("section");
                for (int j = 0; j < section2s.getLength(); j++) {
                    Element section2 = (Element) section2s.item(j);
                    if (section2 != null) {
                        scanSecton2(section2, target);
                    } else {
                        System.err.println("- no section2 found in" + file);
                    }
                }
            }
            return target;
        }
    } catch (SAXException e) {
        throw new IOException("Parsing failed - last parsed file:" + lastParsedFile);
    } catch (ParserConfigurationException e) {
        throw new IOException("Parsing failed because parser wrong configured");
    }
}

From source file:WSpatern.MetaData.java

private void parseXML(String line) {
    codAtribut.clear();/*w  ww. java  2 s.  co m*/
    valoare.clear();
    try {
        org.w3c.dom.Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new StringReader(line)));
        NodeList response = doc.getElementsByTagName("docAttrLink");
        if (response.getLength() > 0) {
            for (int i = 0; i < response.getLength(); i++) {
                Element err = (Element) response.item(i);
                if (err.getElementsByTagName("value").getLength() > 0) {
                    valoare.add(err.getElementsByTagName("value").item(0).getTextContent());
                } else {
                    valoare.add("");
                }

                if (err.getElementsByTagName("label").getLength() > 0) {
                    codAtribut.add(err.getElementsByTagName("label").item(0).getTextContent());
                } else {
                    codAtribut.add("");
                }

            }

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

From source file:adapter.sos.BasicSensorObservationServiceClient.java

private void examineReponse(HttpEntity entity) throws SOSException {
    try {/*from www . ja v  a 2  s .c o m*/
        if (entity != null) {
            String xmlString = EntityUtils.toString(entity);

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = factory.newDocumentBuilder();
            InputSource inStream = new InputSource();
            inStream.setCharacterStream(new StringReader(xmlString));
            Document doc = db.parse(inStream);

            String value;

            NodeList nlExc = doc.getElementsByTagName("ows:ExceptionText");
            for (int i = 0; i < nlExc.getLength(); i++) {
                if (nlExc.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                    org.w3c.dom.Element nameElement = (org.w3c.dom.Element) nlExc.item(i);
                    value = nameElement.getFirstChild().getNodeValue().trim();
                    throw new SOSException(value, "Error in response from SOS service.");
                }
            }

            NodeList nl;
            nl = doc.getElementsByTagName("sos:AssignedObservationId");
            for (int i = 0; i < nl.getLength(); i++) {
                if (nl.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                    org.w3c.dom.Element nameElement = (org.w3c.dom.Element) nl.item(i);
                    value = nameElement.getFirstChild().getNodeValue().trim();
                    logger.info("AssignedObservationId [" + value + "]");
                }
            }
        }
    } catch (ParseException | DOMException | ParserConfigurationException | IOException | SAXException e) {
        e.printStackTrace();
    }
}

From source file:fr.redteam.dressyourself.plugins.weather.yahooWeather.WOEIDUtils.java

private String getFirstMatchingWOEID(Document srcDoc) {
    Log.d(TAG, "parserWOEID");

    try {/*from w ww. ja  v a  2 s .c  o  m*/
        NodeList nodeListDescription = srcDoc.getElementsByTagName("woeid");
        if (nodeListDescription.getLength() > 0) {
            Node node = nodeListDescription.item(0);
            return node.getTextContent();
        } else {
            return WOEID_NOT_FOUND;
        }
    } catch (Exception e) {
        return WOEID_NOT_FOUND;
    }

}

From source file:com.uber.jenkins.phabricator.coverage.CoberturaXMLParser.java

public Map<String, List<Integer>> parse(File... files)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db;//  w ww.  j  a  v a 2  s .c om
    Map<NodeList, List<String>> coverageData = new HashMap<NodeList, List<String>>();

    for (File file : files) {
        InputStream is = null;
        try {
            is = new FileInputStream(file);
            db = dbf.newDocumentBuilder();
            Document doc = db.parse(is);
            NodeList classes = doc.getElementsByTagName(TAG_NAME_CLASS);
            List<String> sourceDirs = getSourceDirs(doc);
            coverageData.put(classes, sourceDirs);
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }

    return parse(coverageData);
}

From source file:fr.iphc.grid.jobmonitor.CeList.java

public static void BilanCe(String OutDir, ArrayList<URL> serviceURL, String TableSql)
        throws ParserConfigurationException, SAXException, IOException, DOMException, ParseException {
    File directory = new File(OutDir);
    ArrayList<CeDefinition> bilan = new ArrayList<CeDefinition>();
    Iterator<URL> i = serviceURL.iterator();

    //      XPathExpression expr = null;
    Document doc = null;
    CeDefinition ce = null;/*from   w w  w  . ja v a  2 s.c om*/
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    while (i.hasNext()) {
        URL url = i.next();
        String file = url.getHost() + "_" + url.getPath().replaceAll("/cream-", "");
        File entry = new File(directory, file + ".out");
        if (entry.exists()) {
            doc = builder.parse(entry);
            ce = new CeDefinition();
            doc.getDocumentElement().normalize();
            NodeList nList = doc.getElementsByTagName("CeHost");
            SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    ce.setDate(
                            dateFormat.parse(eElement.getElementsByTagName("Start").item(0).getTextContent()));
                    // System.out.println("Start : " +
                    // eElement.getElementsByTagName("Start").item(0).getTextContent());
                    ce.setCe(eElement.getElementsByTagName("Ce").item(0).getTextContent());
                    ce.setPath(eElement.getElementsByTagName("Path").item(0).getTextContent());
                    ce.setJobid(eElement.getElementsByTagName("Jobid").item(0).getTextContent());
                    ce.setState(eElement.getElementsByTagName("Status").item(0).getTextContent());
                    ce.setTemps(
                            Integer.parseInt(eElement.getElementsByTagName("Time").item(0).getTextContent()));
                    ce.setMsg(eElement.getElementsByTagName("Desc").item(0).getTextContent());
                }
            }
            if (ce != null)
                bilan.add(ce);
        }
        entry = new File(directory, file + ".err");
        if (entry.exists()) {
            doc = builder.parse(entry);
            ce = new CeDefinition();
            doc.getDocumentElement().normalize();
            NodeList nList = doc.getElementsByTagName("CeHost");
            SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    ce.setDate(
                            dateFormat.parse(eElement.getElementsByTagName("Start").item(0).getTextContent()));
                    ce.setCe(eElement.getElementsByTagName("Ce").item(0).getTextContent());
                    ce.setPath(eElement.getElementsByTagName("Path").item(0).getTextContent());
                    ce.setState(eElement.getElementsByTagName("Status").item(0).getTextContent());
                    ce.setTemps(
                            Integer.parseInt(eElement.getElementsByTagName("Time").item(0).getTextContent()));
                    ce.setMsg(eElement.getElementsByTagName("Desc").item(0).getTextContent());
                }
            }
            if (ce != null)
                bilan.add(ce);
        }
        entry = new File(directory, file + ".lock");
        ce = new CeDefinition();
        if (entry.exists()) {
            doc = builder.parse(entry);
            ce = new CeDefinition();
            doc.getDocumentElement().normalize();
            NodeList nList = doc.getElementsByTagName("CeHost");
            SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    ce.setDate(
                            dateFormat.parse(eElement.getElementsByTagName("Start").item(0).getTextContent()));
                    ce.setCe(eElement.getElementsByTagName("Ce").item(0).getTextContent());
                    ce.setPath(eElement.getElementsByTagName("Path").item(0).getTextContent());
                    ce.setState(eElement.getElementsByTagName("Status").item(0).getTextContent());
                    ce.setTemps(
                            Integer.parseInt(eElement.getElementsByTagName("Time").item(0).getTextContent()));
                    ce.setMsg(eElement.getElementsByTagName("Desc").item(0).getTextContent());
                }
            }
            if (ce != null)
                bilan.add(ce);
        }
    }
    MySQLAccess sql = new MySQLAccess();
    sql.connectDataBase();
    Iterator<CeDefinition> k = bilan.iterator();
    while (k.hasNext()) {
        ce = k.next();
        // System.out.println(ce.getCe() + "\t" + ce.getPath() + "\t" +
        // ce.getDate()
        // + "\t" + ce.getState() + "\t" + ce.getTemps() + "\t" +
        // ce.getMsg()
        // + "\t" + ce.getImplementation());
        sql.writeDataBase(ce, TableSql);
    }
    sql.closeDataBase();
}

From source file:net.frontlinesms.plugins.forms.FormsPluginController.java

/**
 * Fabaris_a.zanchi This method extracts the Form Id from the "id" attribute
 * of <data> tag//w  ww .  j  a  v a2s .  com
 *
 * @param xmlContent String who contains the xml
 * @return a String representing the form ID
 * @throws Exception
 */
public static String getFormIdFromXml(String xmlContent) throws Exception {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    InputSource inStream = new InputSource();
    inStream.setCharacterStream(new StringReader(xmlContent));
    org.w3c.dom.Document xmlDoc = docBuilder.parse(inStream);
    xmlDoc.getDocumentElement().normalize();
    NodeList nodeList = xmlDoc.getElementsByTagName("id");
    Node dataNode = nodeList.item(0);
    Element elem = (Element) dataNode;
    // String id = elem.getAttribute("id");
    String id = elem.getFirstChild().getNodeValue();
    return id;

}

From source file:WSpatern.ProprestiesFile.java

private void parseXML(String line) {
    prop.clear();/*  ww w.  j av a  2  s  .  c o  m*/
    try {
        org.w3c.dom.Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new StringReader(line)));
        NodeList response = doc.getElementsByTagName("docAttrLink");
        if (response.getLength() > 0) {

            for (int i = 0; i < response.getLength(); i++) {

                Element err = (Element) response.item(i);
                //   System.out.println("VAlues" + err.getElementsByTagName("position").item(0).getTextContent());

                if (err.getElementsByTagName("value").getLength() > 0) {
                    prop.add(err.getElementsByTagName("value").item(0).getTextContent());
                } else {
                    prop.add("");
                }

                if (err.getElementsByTagName("label").getLength() > 0) {
                    label.add(err.getElementsByTagName("label").item(0).getTextContent());
                } else {
                    label.add("");
                }

            }

            //    Root_folder_Id=err.getElementsByTagName("id").item(0).getTextContent();
        }
    } catch (ParserConfigurationException ex) {
        Logger.getLogger(LoginWS.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SAXException ex) {
        Logger.getLogger(LoginWS.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(LoginWS.class.getName()).log(Level.SEVERE, null, ex);
    }
}