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:de.cuseb.bilderbuch.images.DeviantArtImageSearch.java

@Override
public List<Image> searchImages(String query) throws ImageSearchException {

    List<Image> result = new ArrayList<Image>();

    try {/*  w w w .j a va2s.c o  m*/
        HttpClient client = httpClientProviderService.getHttpClient();
        HttpUriRequest request = RequestBuilder.get().setUri("http://backend.deviantart.com/rss.xml")
                .addParameter("limit", "10").addParameter("q", "in=photography " + query).build();
        HttpResponse response = client.execute(request);

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(response.getEntity().getContent());

        NodeList items = document.getElementsByTagName("item");
        for (int i = 0; i < items.getLength(); i++) {
            Element item = (Element) items.item(i);
            NodeList title = item.getElementsByTagName("title");
            NodeList content = item.getElementsByTagName("media:content");
            if (content.getLength() > 0) {
                Node url = content.item(0).getAttributes().getNamedItem("url");
                if (url != null) {
                    Image image = new Image();
                    image.setSource("deviantart");
                    image.setUrl(url.getTextContent());
                    image.setTitle(title.item(0).getFirstChild().getTextContent());
                    result.add(image);
                }
            }
        }
    } catch (Exception e) {
        log.error("searchImages", e);
    }
    return result;
}

From source file:org.ambraproject.article.service.ArticleDocumentServiceTest.java

@Test(dataProvider = "blob")
public void testParseBlob(Blob blob, Document expectedXml) throws Exception {
    Document xml = articleDocumentService.getDocument(blob);
    assertNotNull(xml, "returned null xml");
    assertEquals(xml.getElementsByTagName("*").getLength(), expectedXml.getElementsByTagName("*").getLength(),
            "returned xml with incorrect number of tags");
}

From source file:WSpatern.WorkFlow.java

private void parseXML(String line) {
    Flowid.clear();//from w ww .  j av  a  2s  . c om
    Flowname.clear();
    try {
        org.w3c.dom.Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new StringReader(line)));
        NodeList response = doc.getElementsByTagName("flowFileList");
        if (response.getLength() > 0) {
            for (int i = 0; i < response.getLength(); i++) {
                Element err = (Element) response.item(i);
                Flowid.add(err.getElementsByTagName("flowId").item(0).getTextContent());
                Flowname.add(err.getElementsByTagName("flowName").item(0).getTextContent());

            }

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

From source file:org.ambraproject.solr.SolrHttpServiceTest.java

@Test(dataProvider = "requestParameters")
public void testMakeRequest(final Map<String, String> params) throws InterruptedException, SolrException {
    //define behaviour for the http server
    httpEndpoint.whenAnyExchangeReceived(new Processor() {
        @Override//  www  .  j a va2 s.  co m
        public void process(Exchange exchange) throws Exception {
            //check that we got sent the correct headers
            Map<String, Object> headers = exchange.getIn().getHeaders();
            if (!params.containsKey("q")) {
                assertEquals(headers.get("q"), "*:*", "http request didn't get correct default 'q' parameter");
            }
            for (String key : params.keySet()) {
                assertTrue(headers.containsKey(key), "Http request didn't contain parameter: " + key);
                assertEquals(headers.get(key), params.get(key),
                        "Http request didn't have correct value for header: " + key);
            }
            //return a valid xml response so the bean can parse it
            exchange.getOut().setBody(testSolrXml);
        }
    });
    Document result = solrHttpService.makeSolrRequest(params);
    assertNotNull(result, "returned null document");
    assertEquals(result.getElementsByTagName("doc").getLength(), 10,
            "didn't parse xml for correct number of result nodes");
}

From source file:com.universalmind.core.components.mockup.loremipsum.LoremIpsumService.java

public String parseLoremIpsum(String body) throws Exception {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

    InputStream is = new ByteArrayInputStream(body.getBytes());
    Document doc = dBuilder.parse(is);

    doc.getDocumentElement().normalize();

    NodeList nList = ((Element) doc.getElementsByTagName("feed").item(0)).getElementsByTagName("lipsum");

    return nList.item(0).getTextContent();
}

From source file:grandroid.geo.MapRoute.java

public int getDurationValue(Document doc) {
    NodeList nl1 = doc.getElementsByTagName("duration");
    Node node1 = nl1.item(nl1.getLength() - 1);
    NodeList nl2 = node1.getChildNodes();
    Node node2 = nl2.item(getNodeIndex(nl2, "value"));
    Log.i("DurationValue", node2.getTextContent());
    return Integer.parseInt(node2.getTextContent());
}

From source file:grandroid.geo.MapRoute.java

public int getDistanceValue(Document doc) {
    NodeList nl1 = doc.getElementsByTagName("distance");
    Node node1 = nl1.item(nl1.getLength() - 1);
    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.diversityarrays.dalclient.DalUtil.java

/**
 * Return the named attribute from the specified element in the (XML) document.
 * @param doc/*from  w w w . j a  v a 2  s.c o m*/
 * @param tagName
 * @param attributeName
 * @return the attribute value or null if either the element or the attribute are missing
 */
static public String getElementAttributeValue(Document doc, String tagName, String attributeName) {
    return getAttributeValue(doc.getElementsByTagName(tagName), attributeName);
}

From source file:WSpatern.DownloadLink.java

private void parseXML(String line) {

    try {//from  w  w w . ja  v a  2  s .  c  om
        org.w3c.dom.Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new StringReader(line)));
        NodeList response = doc.getElementsByTagName("fileLink");
        if (response.getLength() > 0) {
            for (int i = 0; i < response.getLength(); i++) {
                Element err = (Element) response.item(i);
                download_link = err.getElementsByTagName("downloadLink").item(0).getTextContent();
                System.out.println("Download " + download_link);

            }

        }
    } 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:WSpatern.FileCategory.java

private void parseXML(String line) {
    files.removeAllElements();//from  www .j a  v a2 s. c o m
    files_Id.removeAllElements();
    try {
        org.w3c.dom.Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new StringReader(line)));
        NodeList response = doc.getElementsByTagName("categoryLink");
        if (response.getLength() > 0) {
            for (int i = 0; i < response.getLength(); i++) {
                Element err = (Element) response.item(i);
                category = err.getElementsByTagName("category").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);
    }
}