"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Monday, May 21, 2012
Tuesday, May 1, 2012
JAVA XML Parser Program
Hi Guys,
Here is a sample Java XML Perser Program.
Knowledge Base: http://www.rgagnon.com/javadetails/java-0573.html
Here is a sample Java XML Perser Program.
Knowledge Base: http://www.rgagnon.com/javadetails/java-0573.html
import javax.xml.parsers.*;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
import java.io.*;
public class ParseXMLString {
public static void main(String arg[]) {
String xmlRecords =
"<data>" +
" <employee>" +
" <name>John</name>" +
" <title>Manager</title>" +
" </employee>" +
" <employee>" +
" <name>Sara</name>" +
" <title>Clerk</title>" +
" </employee>" +
"</data>";
try {
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlRecords));
Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("employee");
// iterate the employees
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList name = element.getElementsByTagName("name");
Element line = (Element) name.item(0);
System.out.println("Name: " + getCharacterDataFromElement(line));
NodeList title = element.getElementsByTagName("title");
line = (Element) title.item(0);
System.out.println("Title: " + getCharacterDataFromElement(line));
}
}
catch (Exception e) {
e.printStackTrace();
}
/*
output :
Name: John
Title: Manager
Name: Sara
Title: Clerk
*/
}
public static String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "?";
}
}
Monday, April 30, 2012
Can we Zoom in / Zoom out a web page using Java Script and HTML
Hi,
A lot of websites on internet provide users with an option to increase the font size of the page for better reading experience. Although nowadays almost all the web browsers supports zoom in/out functionality using mouse wheel scroll or other shortcut keys, it is sometimes a good idea to give user a way to increase/decrease the font size of web page.
Let us implement a simple zoomer code in JavaScript that will increase/decrease the font size of the web page at client side without reloading the page from server. A small JavaScript snippet will be used to change the font size of the web page at client side.
Knowledge Base:
http://viralpatel.net/blogs/2009/03/implement-simple-font-zoomer-in-javascript-html.html
In
the above JavaScript code, a global variable fontSize is defined with
value 1. This variable’s value will be changed whenever user tries to
increase or decrease the font size. Then we have two functions zoomIn() and zoomOut()
to perform zoom in and zoom out respectively. The font size of the web
page is changed by changing the attribute fontSize in
document.body.style.
A lot of websites on internet provide users with an option to increase the font size of the page for better reading experience. Although nowadays almost all the web browsers supports zoom in/out functionality using mouse wheel scroll or other shortcut keys, it is sometimes a good idea to give user a way to increase/decrease the font size of web page.
Let us implement a simple zoomer code in JavaScript that will increase/decrease the font size of the web page at client side without reloading the page from server. A small JavaScript snippet will be used to change the font size of the web page at client side.
Knowledge Base:
http://viralpatel.net/blogs/2009/03/implement-simple-font-zoomer-in-javascript-html.html
JavaScript code
Let us check the following JavaScript code.var fontSize = 1;function zoomIn() { fontSize += 0.1; document.body.style.fontSize = fontSize + "em";}function zoomOut() { fontSize -= 0.1; document.body.style.fontSize = fontSize + "em";} |
Calling JavaScript from HTML
You can place simple icons to zoom in and zoom out the text and call the zoomIn() and zoomOut() methods on click event. Or can place buttons to zoom in/out.<input type="button" value="+" onClick="zoomIn()"/><input type="button" value="-" onClick="zoomOut()"/> Thanks, Rajesh Kumar yuvaraj |
Open A New Window without Back Button
Hi Guys,
Following are few tricks that can be used to disable the back button in browser. Please note that we do not literally “disable” the back button, but just nullify its effect is some case and hide it altogether in others.
Knowledge Base: http://viralpatel.net/blogs/2009/11/disable-back-button-browser-javascript.html
Also it is possible to disable the right click on any webpage using Javascript. Add following code in the webpage.
Following are few tricks that can be used to disable the back button in browser. Please note that we do not literally “disable” the back button, but just nullify its effect is some case and hide it altogether in others.
Knowledge Base: http://viralpatel.net/blogs/2009/11/disable-back-button-browser-javascript.html
window.open ("http://viralpatel.net/blogs/","mywindow","status=1,toolbar=0");
1
| <body oncontextmenu="return false;"> Disable Back functionality using history.forwardThis is another technique to disable the back functionality in any webpage. We can disable the back navigation by adding following code in the webpage. Now the catch here is that you have to add this code in all the pages where you want to avoid user to get back from previous page. For example user follows the navigation page1 -> page2. And you want to stop user from page2 to go back to page1. In this case all following code in page
|
Thursday, April 26, 2012
Convert Arrays to Set in Java
Here is a example which demonstrates conversion of Array to Set in Java
Example: Java Array to Set
String [] countires = {"India", "Switzerland", "Italy"}; Set<String> set = new HashSet<String>(Arrays.asList(countires));System.out.println(set); |
[Italy, Switzerland, India]Creating a ZIP file using JavaScript
Hi Guys,
We have 'n' number of tools which generates ZIP files but creating the ZIP files using JavaScript is different. To create a ZIP file using Java script we need to download the
In
above code snippet, we have a button “Create Zip” which calls
javascript function create_zip(). This function calls jszip API to
generate ZIP file.
We have 'n' number of tools which generates ZIP files but creating the ZIP files using JavaScript is different. To create a ZIP file using Java script we need to download the
jszip.js and include it to your html file.
Knowledge Base :
Sample Code: http://viralpatel.net/blogs/2012/01/create-zip-file-javascript.htmlStep 1. Import jszip JavaScript
Include the jszip javascript file in the HTML document where you want to generate ZIP files. You can download the jszip package and include it in HTML or directly include the jszip javascript through git repository.<script type="text/javascript" src="https://raw.github.com/Stuk/jszip/master/jszip.js"></script> |
Step 2. Generate Hello world ZIP
Following code snippet is HTML file which contains Javascript code to generate ZIP file.<HTML><HEAD> <script type="text/javascript" src="jszip.js"></script></HEAD><BODY> <input type="button" onclick="create_zip()" value="Create Zip"></input></BODY><SCRIPT>function create_zip() { var zip = new JSZip(); zip.add("hello1.txt", "Hello First World\n"); zip.add("hello2.txt", "Hello Second World\n"); content = zip.generate(); location.href="data:application/zip;base64," + content;}</SCRIPT></HTML> |
Create/Manipulate PDF files using JAVA
Hi Guys,
Do you know that we can generate the PDF files on the fly in JAVA. To achieve this we need to use the iText.jar file. Just set the iText.jar file to your class path and use below program to generate a PDF file.
Knowledge Base : http://www.lowagie.com/iText/download.html.
Download Link : http://sourceforge.net/projects/itext/files/
Sample Program
-----------------
A Test.pdf file will be generated in "
Do you know that we can generate the PDF files on the fly in JAVA. To achieve this we need to use the iText.jar file. Just set the iText.jar file to your class path and use below program to generate a PDF file.
Knowledge Base : http://www.lowagie.com/iText/download.html.
Download Link : http://sourceforge.net/projects/itext/files/
Sample Program
-----------------
import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import java.util.Date;import com.lowagie.text.Document;import com.lowagie.text.Paragraph;import com.lowagie.text.pdf.PdfWriter;public class GeneratePDF { public static void main(String[] args) { try { OutputStream file = new FileOutputStream(new File("C:\\Test.pdf")); Document document = new Document(); PdfWriter.getInstance(document, file); document.open(); document.add(new Paragraph("Hello Kiran")); document.add(new Paragraph(new Date().toString())); document.close(); file.close(); } catch (Exception e) { e.printStackTrace(); } }}C:\\" drive.
Subscribe to:
Posts (Atom)