Friday, April 4, 2008

XML (Extensible Markup Language)

1. Introduction

XML (Extensible Markup Language) is a markup language that is used as a standard format for data. Since XML is structured, platform independent, textual information it is chosen as the best choice for data exchange in Web applications. XML serves the purpose as the tool for data exchange between the disparate applications in our project operating on different platforms. In our project IBM’s XML4J parser for XML has been used for parsing XML documents since it is totally written in Java and hence compatible with the architecture of our project. Our application creates an XML document with credit card information.

The credit card XML document is validated against a Document Type Definition, for all Creditcard information exchange, by a third party application like a dummy banking application. If the XML document is found to be valid, the Banking application retrieves data from the XML document and prints it out in HTML format.


2. Writing XML document through a Servlet


2.1 The user Interface

The data, which is to be written into an XML document is furnished by the user in a screen, meant for the data entry. The Screen is a JSP page that has text fields for entering Credit card Information like


· Credit card Name

· Name on the Credit card

· Creditcard Number

· Expiration Month

· Expiration Year

The user enters the data and presses “Next” button to move ahead in the Registration process (This Screen is a part of New User Registration in our project). But the validity of the data, the user has furnished, need to be validated for verifying the authenticity of the Information provided. Hence the Creditcard Information is sent to a Banking Application. First of all, Let us see how the data entered by the user is put into an XML document.



2.2 Writing the XML Document

The data entered from the page above are posted to a Servlet, CreditcardServlet.class file, the code for which is given below. The comments inserted between the code explain the functionality of the Servlet.

CreditcardServlet
/****************************************************************************************
* Class : CreditcardServlet
*
* Author : Lakshmanan .K
*
* Functionality : This class gets credit card data posted from the
Creditcard details entry screen
and writes the data into an XML file.
*************************************************************************************/

/************************************
Import the required packages for the servlet
*************************************/
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class CreditcardServlet extends HttpServlet {

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
/*******Get the data from Creditcard Entry Details******/
/******* Gets data for Creditcard Type **************/
String type = request.getParameter ("creditCardType");

/******* Gets data for Creditcard Name **************/
String name = request.getParameter ("creditCardName");

/******* Gets data for Creditcard Number **************/
String cardNumber = request.getParameter ("creditCardNumber");

/******* Gets data for Expiration month of the card **************/
String month = request.getParameter ("expirationMonth");

/******* Gets data for Expiration year of the card **************/
String year = request.getParameter ("expirationYear");

/*******Writes the data posted from the JSP
Page into an XML file ************/

/******* Create a new output file ************/
File outputFile= new File ("examples/CREDITCARD.xml");

/******* Create a new output stream ************/
FileOutputStream fos= new FileOutputStream(outputFile);

/******* The string “s” holds all the contents of the XML document
The XML document is written with conformance to a DTD, the Document
Type definition for the XML document. The discussion about the DTD is
Given at the end of the Servlet code *********************/
String s= new String ("\n"+
/***** DTD Declaration *****/
"\n"+
/***** Root element for the XML Document *******/
"\n"+
/***** USER tag which encapsulates all the other
credit card information for a single user *****/
"\n"+

/*** Card type data put in between CARDTYPE XML tags ***/
""+type+"\n"+

/*** Card name data put in between CARDNAME XML tags ***/
""+name+"\n"+

/*** Card Number data put in between CARDNUMBER XML tags ***/
""+cardNumber+"\n"+

/*** Expiration month data put in between EXPIRATIONMONTH XML tags ***/
""+month+"\n"+

/*** Expiration year data put in between EXPIRATIONYEAR XML tags ***/
""+year+"\n"+

/*** End the user element tag ***/
"\n"+

/*** End the Root element CREDITCARD tag ***/
"");

/***** New string reader initialized *****/
StringReader sr= new StringReader(s);
int c;
/***** Ouputstream writes the string into the file *****/
while((c=sr.read())!=-1)
fos.write(c);

//calls the next page which gives links to the XML document created and
//and the XML to HTML functionality getServletConfig().getServletContext().getRequestDispatcher("/jsp/auction/XMLCreated.jsp").forward(request, response);
}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}

/**
Return servlet information

@return message about this servlet
*/
public String getServletInfo(){
return "CreditcardServlet";
}

}

The servlet , as explained clearly in the comments, writes the posted data into an XML document. While the XML document is written, it is taken care that the document confirms to the Document Type Definition for Credit card XML documents.

The XML document has to be validated against a standard to verify its validity and make it shareable with disparate applications. The DOCTYPE declaration in the XML document refers to a Document Type Definition that is stored in a specific URL.

An XML document is valid if its content conforms to the rules in its DTD. A DTD is a grammar that describes what tags and attributes are valid in an XML document and in what context they are valid. The Document type declaration can make reference to an external DTD or include the DTD internally.

The Document Type Definition (DTD) written exclusively for this credit card information sharing application is given below.

CREDITCARD.dtd


The Document Type Definition is kept in a URL common to the Applications, which share the data. The Document type definition can be In-line too, i.e. it can be part of the XML document itself. The DTD starts with the XML declaration statement. Then it defines the Root element, which encapsulates all other elements. Subsequently all other elements are defined, the content of the elements, their attributes, the other elements which they encapsulate, etc.,

Let us see how the XML document created by the Creditcard servlet looks like

The picture above shows the XML file created, in IE 5.0 Browser. IE 5.0 has an in-built XML parser which does the validation against the document type definition for that XML document and displays the tree structure XML document. If the XML document is not well-formed, i.e. the document is not syntactically correct, or if the XML document is not valid, i.e. it does not confirm with the Document Type Definition, the Parser throws out an error and the IE browser does not display the XML document. It simply displays the error message.

0 comments: