List of usage examples for org.w3c.dom Document getElementsByTagName
public NodeList getElementsByTagName(String tagname);
NodeList
of all the Elements
in document order with a given tag name and are contained in the document. From source file:cloudMe.CloudMeAPI.java
private double getXMLvalue(String xml) throws ParserConfigurationException, SAXException, IOException { double freeSpace; DocumentBuilder newDocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = newDocumentBuilder.parse(new ByteArrayInputStream(xml.getBytes())); String usedSize = doc.getElementsByTagName("currentSize").item(0).getTextContent(); String maxSize = doc.getElementsByTagName("quotaLimit").item(0).getTextContent(); freeSpace = Double.valueOf(maxSize) - Double.valueOf(usedSize); return freeSpace; }
From source file:org.urbanstew.soundcloudapi.test.RequestTest.java
private String getStreamUrl(HttpResponse response) throws Exception { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document dom = db.parse(response.getEntity().getContent()); return dom.getElementsByTagName("stream-url").item(0).getFirstChild().getNodeValue(); }
From source file:com.hyunnyapp.yahooweathergatter.WOEIDUtils.java
private String getTextContentByTagName(Document srcDoc, String tagName) { NodeList nodeListDescription = srcDoc.getElementsByTagName(tagName); if (nodeListDescription.getLength() > 0) { Node node = nodeListDescription.item(0); return node.getTextContent(); }//from w ww.j a v a2 s . c o m return null; }
From source file:com.lyncode.xoai.serviceprovider.iterators.SetIterator.java
private void harvest() throws InternalHarvestException, NoRecordsMatchException, BadResumptionTokenException, NoSetHierarchyException {//www. j av a 2s . c o m HttpClient httpclient = new DefaultHttpClient(); String url = makeUrl(); log.info("Harvesting: " + url); HttpGet httpget = new HttpGet(url); httpget.addHeader("User-Agent", HarvesterManager.USERAGENT); httpget.addHeader("From", HarvesterManager.FROM); HttpResponse response = null; if (this.proxyIp != null && this.proxyPort > -1) { HttpHost proxy = new HttpHost(this.proxyIp, this.proxyPort); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } try { response = httpclient.execute(httpget); StatusLine status = response.getStatusLine(); log.debug(response.getStatusLine()); if (status.getStatusCode() == 503) // 503 Status (must wait) { org.apache.http.Header[] headers = response.getAllHeaders(); for (org.apache.http.Header h : headers) { if (h.getName().equals("Retry-After")) { String retry_time = h.getValue(); try { Thread.sleep(Integer.parseInt(retry_time) * 1000); } catch (NumberFormatException e) { log.warn("Cannot parse " + retry_time + " to Integer", e); } catch (InterruptedException e) { log.debug(e.getMessage(), e); } httpclient.getConnectionManager().shutdown(); httpclient = new DefaultHttpClient(); response = httpclient.execute(httpget); } } } HttpEntity entity = response.getEntity(); InputStream instream = entity.getContent(); Document doc = XMLUtils.parseDocument(instream); XMLUtils.checkListSets(doc); NodeList listSets = doc.getElementsByTagName("set"); for (int i = 0; i < listSets.getLength(); i++) _queue.add(XMLUtils.getSet(listSets.item(i))); resumption = XMLUtils.getText(doc.getElementsByTagName("resumptionToken")); log.debug("RESUMPTION: " + resumption); } catch (IOException e) { throw new InternalHarvestException(e); } catch (ParserConfigurationException e) { throw new InternalHarvestException(e); } catch (SAXException e) { throw new InternalHarvestException(e); } }
From source file:ua.kiev.doctorvera.utils.SMSGateway.java
@SuppressWarnings("deprecation") public ArrayList<String> send(String phone, String sms) { ArrayList<String> result = new ArrayList<String>(); final String MESSAGE = "<message><service id='single' source='" + FROM + "'/><to>" + phone + "</to><body content-type='text/plain'>" + sms + "</body></message>"; @SuppressWarnings("resource") HttpClient httpclient = new DefaultHttpClient(); String xml = null;//from w w w . j ava2 s . c o m try { HttpPost httpPost = new HttpPost(SMS_SEND_URL); StringEntity entity = new StringEntity(MESSAGE, "UTF-8"); entity.setContentType("text/xml"); entity.setChunked(true); httpPost.setEntity(entity); httpPost.addHeader( BasicScheme.authenticate(new UsernamePasswordCredentials(LOGIN, PASS), "UTF-8", false)); HttpResponse response = httpclient.execute(httpPost); HttpEntity resEntity = response.getEntity(); LOG.info("Sending SMS: " + (response.getStatusLine().getStatusCode() == 200)); xml = EntityUtils.toString(resEntity); } catch (Exception e) { LOG.severe("" + e.getStackTrace()); } finally { httpclient.getConnectionManager().shutdown(); } //parsing xml result Document doc = loadXMLFromString(xml); NodeList nl = doc.getElementsByTagName("status"); Element status = (Element) nl.item(0); result.add(0, status.getAttribute("id").toString()); //tracking id at position 0 result.add(1, status.getAttribute("date").toString()); //date at position 1 result.add(2, getElementValue(status.getFirstChild())); //state at position 2 return result; }
From source file:com.nanocrawler.contentparser.HtmlContentParser.java
private String getBaseUrl(Document doc) { String baseUrl = null;/*from w w w . j ava 2s . c o m*/ try { if (doc.getElementsByTagName(BASE_ELEMENT).getLength() > 0 && doc.getElementsByTagName(BASE_ELEMENT).item(0).hasAttributes()) { baseUrl = doc.getElementsByTagName(BASE_ELEMENT).item(0).getAttributes().getNamedItem(HREF_ATTRIB) .getNodeValue().trim(); } } catch (Exception ex) { baseUrl = null; } return baseUrl; }
From source file:com.zimbra.qa.unittest.TestUserServlet.java
private void checkResultOrder(ZMailbox mbox, String uri, List<String> expectedOrder) throws Exception { InputStream is = mbox.getRESTResource(uri); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(is); NodeList nodes = doc.getElementsByTagName("m"); assertEquals(expectedOrder.size(), nodes.getLength()); List<String> results = new ArrayList<String>(); for (int i = 0; i < nodes.getLength(); i++) { Element node = (Element) nodes.item(i); String id = node.getAttribute("id"); results.add(id);/* w w w . java 2 s. c o m*/ } assertEquals(expectedOrder, results); }
From source file:org.alfresco.cms.documentlibrary.evaluator.MimetypeSubComponentEvaluator.java
private String parseReponse(Response response) { try {/* w w w .jav a 2 s.com*/ Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(response.getResponseStream()); NodeList list = dom.getElementsByTagName("cmis:propertyString"); int len = list.getLength(); for (int i = 0; i < len; i++) { Element element = (Element) list.item(i); String propertyName = element.getAttribute("propertyDefinitionId"); String objectMimeTypeId = null; if (propertyName.equals("cmis:contentStreamMimeType")) { objectMimeTypeId = element.getElementsByTagName("cmis:value").item(0).getTextContent(); } if (objectMimeTypeId == null) { continue; } return objectMimeTypeId; } } catch (Exception exc) { exc.printStackTrace(); } return null; }
From source file:edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview.CustomListViewConfigFile.java
private List<Element> getElements(Document doc, String tagName) { List<Element> list = new ArrayList<Element>(); NodeList nodes = doc.getElementsByTagName(tagName); if (nodes != null) { for (int i = 0; i < nodes.getLength(); i++) { list.add((Element) nodes.item(i)); }//from w w w. jav a 2s. c om } return list; }
From source file:com.linuxbox.enkive.filter.EnkiveFiltersBean.java
protected void startup() { if (LOGGER.isTraceEnabled()) LOGGER.trace("Loading Enkive filters"); try {//from www.jav a 2 s. com Resource res = new ClassPathResource(ENKIVE_FILTERS_FILENAME); FileInputStream filterFile = new FileInputStream(res.getFile()); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(filterFile); NodeList filters = doc.getElementsByTagName(FILTER); doc.getElementsByTagName(DEFAULT_ACTION).item(0).getTextContent(); if (doc.getElementsByTagName(DEFAULT_ACTION).item(0).getTextContent().toLowerCase().equals(DENY)) defaultAction = FilterAction.DENY; for (int i = 0; i < filters.getLength(); i++) { Element filter = (Element) filters.item(i); if (filter.getAttribute(ENABLED).equals(FILTER_TRUE)) { int filterAction = 0; Node action = filter.getElementsByTagName(ACTION).item(0); if (action.getTextContent().toLowerCase().equals(ALLOW)) filterAction = FilterAction.ALLOW; if (action.getTextContent().toLowerCase().equals(DENY)) filterAction = FilterAction.DENY; Node header = filter.getElementsByTagName(HEADER).item(0); Node value = filter.getElementsByTagName(VALUE).item(0); int filterType = 0; int filterComparator = 0; if (((Element) value).getAttribute(TYPE).toLowerCase().equals(INTEGER)) filterType = FilterType.INTEGER; else if (((Element) value).getAttribute(TYPE).toLowerCase().equals(STRING)) filterType = FilterType.STRING; else if (((Element) value).getAttribute(TYPE).toLowerCase().equals(ADDRESS)) filterType = FilterType.ADDRESS; else if (((Element) value).getAttribute(TYPE).toLowerCase().equals(FLOAT)) filterType = FilterType.FLOAT; else if (((Element) value).getAttribute(TYPE).toLowerCase().equals(DATE)) filterType = FilterType.DATE; if (((Element) value).getAttribute(COMPARISON).toLowerCase().equals(IS_GREATER_THAN)) filterComparator = FilterComparator.IS_GREATER_THAN; else if (((Element) value).getAttribute(COMPARISON).toLowerCase().equals(IS_LESS_THAN)) filterComparator = FilterComparator.IS_LESS_THAN; else if (((Element) value).getAttribute(COMPARISON).toLowerCase().equals(CONTAINS)) filterComparator = FilterComparator.CONTAINS; else if (((Element) value).getAttribute(COMPARISON).toLowerCase().equals(DOES_NOT_CONTAIN)) filterComparator = FilterComparator.DOES_NOT_CONTAIN; else if (((Element) value).getAttribute(COMPARISON).toLowerCase().equals(MATCHES)) filterComparator = FilterComparator.MATCHES; else if (((Element) value).getAttribute(COMPARISON).toLowerCase().equals(DOES_NOT_MATCH)) filterComparator = FilterComparator.DOES_NOT_MATCH; filterSet.add(new EnkiveFilter(header.getTextContent(), filterAction, filterType, value.getTextContent(), filterComparator)); if (LOGGER.isTraceEnabled()) LOGGER.info("Enkive filtering by header " + header.getTextContent()); } } filterFile.close(); } catch (FileNotFoundException e) { LOGGER.fatal("Could not find enkive-filters.xml, Filters not initialized"); } catch (IOException e) { LOGGER.fatal("Could not read file enkive-filters.xml, Filters not initialized"); } catch (ParserConfigurationException e) { LOGGER.fatal("Could not initialize parser for enkive-filters.xml, Filters not initialized"); } catch (SAXException e) { LOGGER.fatal("Could not parse enkive-filters.xml, Filters not initialized"); } }