Thursday, February 19, 2009

XML Parsing

Below code an be used to parse an XML string
See the sample XMl given below:
<entry>
<dc:creator>Jane Kirkland</dc:creator>
<dc:creator>Dorothy Burke</dc:creator>
<dc:date>2005-11-02</dc:date>
<dc:description>
An essential guide and reference tool to Lotus Notes 7 with practical answers for people who need fast results.
</dc:description>
<dc:format>366 pages</dc:format>
<dc:identifier>9FEdL2eH6t8C</dc:identifier>
<dc:identifier>ISBN:0672328003</dc:identifier>
<dc:identifier>ISBN:9780672328008</dc:identifier>
<dc:publisher>Sams Publishing</dc:publisher>
<dc:subject>Computers</dc:subject>
<dc:title>Sams Teach Yourself Lotus Notes 7 in 10 Minutes</dc:title>
</entry>


public static void parseME(String xmlRecords) {


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("entry");
NodeList name = null;
// iterate the entries
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);

if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element element = (Element) node;

NodeList nameNode = element.getElementsByTagName("dc:creator");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Name = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:date");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Date = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:description");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Description = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:format");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Format = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:identifier");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Identifier = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:publisher");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Publisher = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:subject");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Subject = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:title");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Title = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}

HTTP Get Request and XML parsing

Below is a sample code for processing a HTTP GET request .
The below code will use the google search API for searching a word 'lotus notes'.
The result will be a XML string which will be parsed using ParseMe function




import java.net.*;
import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
import java.io.*;

public class CopyOfURLReader {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://books.google.com/books/feeds/volumes?q=lotus+notes&start-index=1&max-results=20");
BufferedReader in = new BufferedReader(
new InputStreamReader(
yahoo.openStream()));

String inputLine;
String finalLine="";
while ((inputLine = in.readLine()) != null)
{
System.out.println(inputLine);
finalLine += inputLine;
}
in.close();
System.out.println(finalLine);

// Parse the file using the handler
parseME(finalLine);

}

//Parsing the result

public static void parseME(String xmlRecords) {


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("entry");
NodeList name = null;
// iterate the entries
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);

if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element element = (Element) node;

NodeList nameNode = element.getElementsByTagName("dc:creator");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Name = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:date");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Date = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:description");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Description = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:format");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Format = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:identifier");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Identifier = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:publisher");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Publisher = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:subject");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Subject = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:title");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Title = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "?";
}

}

HTTP Post Request and XML Parsing


Below is a sample code which demonstrate sending HTTP POSt request using java
It also assumes that , if you are getting the putput in XML format, the parseMe() will help you to parse the XML file correctly.



import java.io.BufferedReader;

import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
import java.io.*;

public class HTTPPostTest{

public static void main(String args[]){
try {
// Construct data
String data = URLEncoder.encode("<Key1>", "UTF-8") + "=" + URLEncoder.encode("
<Value1>", "UTF-8");
data += "&" + URLEncoder.encode("
<Key2>", "UTF-8") + "=" + URLEncoder.encode("<Value2>", "UTF-8");

// Send data
URL url = new URL("
<URL>");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
String finalLine="";
while ((line = rd.readLine()) != null) {
// Print the output text as it is
System.out.println(line);
finalLine += line;
}
wr.close();
rd.close();

parseME(finalLine); //calling the function to parse the XML file

} catch (Exception e) {
}

}

/*
*Suppose the XML structure is like this
//
// <xml>
//<person>
//<name>TEST</name>
//
<age> 23</age>
//
<ph>23456</ph>
//
<ph> 6789</ph>
//
</person>
//
</xml>


This code will parse multiple entires with the same tag like 'ph'


*/

public static void parseME(String xmlRecords) {


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("person");
NodeList name = null;
// iterate the entries
for (int i = 0; i <>
Node node = nodes.item(i);

if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element element = (Element) node;

NodeList nameNode = element.getElementsByTagName("name");
for(int iIndex=0; iIndex<>
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Name = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("ph");
for(int iIndex=0; iIndex<>
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Date = " +nameElement.getFirstChild().getNodeValue().trim());
}
}

}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "?";
}

}

Tuesday, February 10, 2009

Elijah went up by a whirlwind into heaven.

As told in TPM CHURCH, ROYAPETTA 08-FEB-2009 SUNDAY SERVICE

2 Kings 2: 11 "And it came to pass, as they still went on, and talked, that, behold, there appeared a chariot of fire, and horses of fire, and parted them both asunder; and Elijah went up by a whirlwind into heaven."

Before Elijah had been taken to heaven he had to go/visit four places; according to the scriptures. These four places reveals the four important phases in our spiritual lives that we have to undergo before we caught up in the soon coming of Jesus Christ.
2 Kings 2:1 "And it came about when the LORD was about to take up Elijah by a whirlwind to heaven, that Elijah went with Elisha from Gilgal". Gilgal is the place where God removed reproaches of Egypt from the people of Israel. This shows our repentance. The Lord Jesus had shown his unconditional love towards us and saved us from the dung of sin.
2 Kings 2:2 "“Stay here please, for the LORD has sent me as far as Bethel."
Meaning of the word Bethel is the House of God. The next thing we have to do is to come to the House of God. What does it shows? It shows our savings in the Christ Jesus.All the love of the world is based on some 'give and take' condition. But the love Jesus showed up on to us is unconditional.
Here the place Bethel shows our savings in the name of Jesus Christ and accepting him us the LORD. Bethel also shows anointing up on a child of GOD.
2 Kings 2:4 Elijah said to him, “Elisha, please stay here, for the LORD has sent me to Jericho". At the time of the ministry of JESUS, while he was crossing through Jericho, there was a man who was blind itself from the womb of his mother. On hearing that JESUS is passing thru the street, he cried aloud and said "O, SON OF DAVID ! HAVE MERCY ON ME". He is a blind man, he had never seen JESUS, HE never seen the miracles did by JESUS, but how he came to know that JESUS is the son of DAVID and only he can help him from his blindness. He had a very sharp ear and he heard from others about the miracle work done by jesus.
In our life we should always have a very eager to know who JESUS and and let him be MERCY ON YOU
2 Kings 2:6 Then Elijah said to him, “Please stay here, for the LORD has sent me to the Jordan”. Jordan is a river which is always be violent throughout the year.In our life also there are situations where we will face problems like Jordan. But he still on LORD and know that he is able to work for you and deliver you from all your trouble. For that what you have to do? You should be still and silent. Let allow the LORD to work / fight for you.
After crossing the JORDAN only , the ELIAJ was caught up in the whirlwind.

Dear child of GOD, these four places shows the four path a child of GOD have to travel in his life. After successfully passing thru all these ways only you can caught up with JESUS.
Let the GOD help us in our ways.
PRAISE THE LORD.