Thursday, February 19, 2009

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 "?";
}

}

No comments:

Post a Comment