Posts

Showing posts from May, 2010

Java - Write a XML DOM Document (org.w3c.dom.Document) in to a file

Very useful when you need to backup XML data at client side.
// This method writes a DOM document to a file public static void writeXmlFile(Document doc, String filename) { try { // Prepare the DOM document for writing Source source = new DOMSource(doc); // Prepare the output file File file = new File(filename); Result result = new StreamResult(file); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result); } catch (TransformerConfigurationException e) { } catch (TransformerException e) { } }

Java - Convert String to Date

import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; public java.util.Date stringToDate(String datestring) { DateFormat df = new SimpleDateFormat("dd-MMM-yyyy"); java.util.Date date=null; try { date = df.parse(datestring); } catch (ParseException ex) { ex.printStackTrace(); } return date; To know more about working with strings in different date formats read the complete documentation for the SimpleDateFormat.

Networking - Using Java to login to a website with the POST method of http

HttpClient was started in 2001 as a subproject of the Jakarta Commons, based on code developed by the Jakarta Slide project. It was promoted out of the Commons in 2004, graduating to a separate Jakarta project. In 2005, the HttpComponents project at Jakarta was created, with the task of developing a successor to HttpClient 3.x and to maintain the existing codebase until the new one is ready to take over. The Commons project, cradle of HttpClient, left Jakarta in 2007 to become an independent Top Level Project. Later in the same year, the HttpComponents project also left Jakarta to become an independent Top Level Project, taking the responsibility for maintaining HttpClient with it.
Using Apache's HttpClient we can easily implement a client side application that interacts with a web application or any HTTP based web services.
Below is a sample code posted by lobhater @ forums.sun.com

There are many another examples available at Apache's HttpComponents project page also.


package …