1
Building XMLFile
  • Get user data from Request and store in Session for later use
  • private void fetchStudentPayment(HttpServletRequest request,HttpServletResponse response) throws Exception
    {
    HttpSession session = request.getSession();

    try
    {
    UserData userData = (UserData)session.getAttribute(LookUpConstants.USER_DATA);

    int studentId = userData.getId();

    String street1 = request.getParameter("street1");
    String street2 = request.getParameter("street2");
    String city = request.getParameter("city");
    String state = request.getParameter("state");
    String zip = request.getParameter("zip");
    String cardNumber = request.getParameter("cardNumber");
    String cardType = request.getParameter("cardType");
    String expiryMonth = request.getParameter("expiryMonth");
    String expiryYear = request.getParameter("expiryYear");
    String expiryDate = expiryMonth + expiryYear;
    String courseFee = request.getParameter("courseFee");
    String securityCode = request.getParameter("securityCode");

    AddressData addressData = new AddressData();

    addressData.setStreet1(street1);
    if (street2 != null)
    {
    addressData.setStreet2(street2);
    }
    addressData.setCity(city);
    addressData.setState(state);
    addressData.setZipcode(zip);

    userData.setAddressData(addressData);

    CreditCardPaymentData creditCardPaymentData = new CreditCardPaymentData();

    creditCardPaymentData.setAmount(courseFee);
    creditCardPaymentData.setCardNumber(cardNumber);
    creditCardPaymentData.setCardType(cardType);
    creditCardPaymentData.setExpiryDate(expiryDate);
    creditCardPaymentData.setSecurityCode(securityCode);

    session.setAttribute(LookUpConstants.CREDIT_CARD_PAYMENT_DATA,creditCardPaymentData);

    }
    catch(Exception e)
    {
    throw e;
    }

    }

     
  • Build XML File by reading data from Session
  • public void buildXmlFile(HttpServletRequest req, HttpServletResponse resp) throws Exception
    {
    try
    {
    HttpSession session = req.getSession();

    File xmlFile = new File("C:\\Program Files\\Apache Group\\jakarta-tomcat-4.1.27\\webapps\\cs554project\\WEB-INF\\paymentfile.xml");
    BufferedWriter out = new BufferedWriter(new FileWriter(xmlFile));
    java.util.Date currentTime = new java.util.Date();
    SimpleDateFormat formatter = new SimpleDateFormat ("yyyy/MM/dd");

    UserData userData = (UserData) session.getAttribute(LookUpConstants.USER_DATA);
    CreditCardPaymentData creditCardpaymentData = (CreditCardPaymentData) session.getAttribute(LookUpConstants.CREDIT_CARD_PAYMENT_DATA);

    StringBuffer stringBuffer = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

    stringBuffer.append("<creditcardpayment xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" payerid=\"123456789\" >");
    stringBuffer.append("<paymentamount>"+(creditCardpaymentData.getAmount())+"</paymentamount>");
    stringBuffer.append("<paymentdate>"+(formatter.format(currentTime))+"</paymentdate>");
    stringBuffer.append("<paymentdesc>This is payment for student fee</paymentdesc>");
    stringBuffer.append("<customer>\n");
    stringBuffer.append("<firstname>"+userData.getFirstName()+"</firstname>");
    stringBuffer.append("<lastname>"+userData.getLastName()+"</lastname>");
    stringBuffer.append("<address>\n");
    stringBuffer.append("<street1>"+(userData.getAddressData()).getStreet1()+"</street1>");
    stringBuffer.append("<street2>"+((userData.getAddressData()).getStreet2()!= null? (userData.getAddressData()).getStreet2() : "")+"</street2>");
    stringBuffer.append("<city>"+((userData.getAddressData()).getCity())+"</city>");
    stringBuffer.append("<state>"+((userData.getAddressData()).getState())+"</state>");
    stringBuffer.append("<zip>"+((userData.getAddressData()).getZipcode())+"</zip>");
    stringBuffer.append("</address>");
    stringBuffer.append("</customer>");
    stringBuffer.append("<creditcardinfo>");
    stringBuffer.append("<cardnumber>"+(creditCardpaymentData.getCardNumber())+"</cardnumber>");
    stringBuffer.append("<cardtype>"+(creditCardpaymentData.getCardType())+"</cardtype>");
    stringBuffer.append("<expirydate>"+(creditCardpaymentData.getExpiryDate())+"</expirydate>");
    stringBuffer.append("<securitycode>"+(creditCardpaymentData.getSecurityCode())+"</securitycode>");
    stringBuffer.append("</creditcardinfo>");
    stringBuffer.append("</creditcardpayment>");

    System.out.println("Inside buildXmlFile method");
    System.out.println("stringBuffer.toString()"+stringBuffer.toString());
    out.write(stringBuffer.toString());
    out.close();

    System.out.println("Xml File built successfully");

    }
    catch(Exception e)
    {
    e.printStackTrace();
    throw e;
    }
    }

 

  • Questions to consider
  • Any other way to generate the XML file ?
  • Yes. JAXB -
  • The Java Architecture for XML Binding (JAXB) is a Java technology that enables you to generate Java classes from XML schemas. As part of this process, the JAXB technology also provides methods for unmarshalling an XML instance document into a content tree of Java objects, and then marshalling the content tree back into an XML document. JAXB provides a fast and convenient way to bind an XML schema to a representation in Java code, making it easy for Java developers to incorporate XML data and processing functions in Java applications without having to know much about XML itself.

    Data Binding Process

     

  • File Name and path of file is hard coded. This has a limitation that if multiple users use the application, the file will  be overwritten.

    This can be easily modified by appending an unique number to the file so that multiple users can be supported. The path can be set in a properties file.

    References :

    http://java.sun.com/webservices/docs/1.3/tutorial/doc/IntroWS6.html#wp72426