org.xml.sax.SAXParseException: Current configuration of the parser doesn’t allow a maxOccurs attribute value to be set greater than the value 5,000


org.xml.sax.SAXParseException: Current configuration of the parser doesn't allow a maxOccurs attribute value to be set greater than the value 5,000.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaFatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSAttributeChecker.reportSchemaFatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSAttributeChecker.checkAttributes(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSAttributeChecker.checkAttributes(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseLocal(Unknown Source)

The solution for this problem; you should disable FEATURE_SECURE_PROCESSING

//Create SchemaFactory using XSD file
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING,false);
Schema schema = factory.newSchema(xsdSource);

Python lang. XML parsing using xml.dom.minidom and xml.parsers.expat


You can parse XML documents using Python language with the following code piece;

XML Data Content;

<?xml version="1.0" encoding="UTF-8"?>
<SERVERCONFIG>
<SERVERS>
<SERVER NAME="server1" ENABLED="Y">
<IP>10.1.2.1</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>12345</PASSWORD>
<DESCRIPTION>Application Server1</DESCRIPTION>
</SERVER>
<SERVER NAME="server2" ENABLED="Y">
<IP>10.1.2.2</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>54321</PASSWORD>
<DESCRIPTION>Application Server1</DESCRIPTION>
</SERVER>
<SERVER NAME="server3" ENABLED="N">
<IP>10.1.2.3</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>012345</PASSWORD>
<DESCRIPTION>Application Server3</DESCRIPTION>
</SERVER>
<SERVER NAME="server4" ENABLED="Y">
<IP>10.1.2.4</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>0123456</PASSWORD>
<DESCRIPTION>Application Server4</DESCRIPTION>
</SERVER>
<SERVER NAME="server5" ENABLED="N">
<IP>10.1.2.5</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>01234567</PASSWORD>
<DESCRIPTION>Application Server5</DESCRIPTION>
</SERVER>
</SERVERS>
</SERVERCONFIG>

XMLParsing.py


#Created on Jul 24, 2013

#@author: tuna

#XML Parsing example
from xml.dom.minidom import parse, parseString
import xml.parsers.expat

#loading XML to variable
XML = parse('C://config.xml')

domRunTimeList = parseString('<xmlRunTime><data>hi xml</data></xmlRunTime>')

#format XML runTime
print (domRunTimeList.toprettyxml("\t", "\n", None))

print ("\n") #new line

print ("ENABLED SERVER LIST\n")
for node in XML.getElementsByTagName('SERVER'):
 if node.getAttribute("ENABLED")=="Y":
 print ("IP --> ",str(node.getElementsByTagName("IP")[0].firstChild.nodeValue) , "NAME", node.getAttribute("NAME"))

print ("\n") #new line
print ("all IP LISTING STARTS\n")
for node in XML.getElementsByTagName('SERVER'):
 print ("IP --> ",str(node.getElementsByTagName("IP")[0].firstChild.nodeValue))

print ("\n") #new line

#XML parsing with on the fly events

# Handlers
def start(name, attrs):
 print ('Start Element:', name, attrs)
def end(name):
 print ('End Element:', name)
def text(txt):
 print ('Text:', str(txt))

#create a parser
parser = xml.parsers.expat.ParserCreate()

#map related handlers events
parser.StartElementHandler = start
parser.EndElementHandler = end
parser.CharacterDataHandler = text

parser.Parse("<xmlRunTime><data att='this is attribute1' att2='This is attribute 2'>hi xml</data></xmlRunTime>");

output;

<?xml version=”1.0″ ?>
<xmlRunTime>
<data>hi xml</data>
</xmlRunTime>

ENABLED SERVER LIST

IP –> 10.1.2.1 NAME server1
IP –> 10.1.2.2 NAME server2
IP –> 10.1.2.4 NAME server4
all IP LISTING STARTS

IP –> 10.1.2.1
IP –> 10.1.2.2
IP –> 10.1.2.3
IP –> 10.1.2.4
IP –> 10.1.2.5
Start Element: xmlRunTime {}
Start Element: data {‘att’: ‘this is attribute1’, ‘att2’: ‘This is attribute 2’}
Text: hi xml
End Element: data
End Element: xmlRunTime

How to read and write key/value pairs inside .ini file JAVA (XML store)


Friends the following udemy course created by me;

      Click here to attend Spring Framework 4.x and certification course with a discount

Spring Framework and Core Spring Certification Udemy course with discount
Spring Framework and Core Spring Certification Udemy course with a discount coupon

Use the following code to store key and values inside a .ini file using Java Environment.

This program demonstrates the use of .ini file by implementing regular txt and XML storing methods.

To use the .ini file in this program you should create a folder called ini under the root folder of your project and you should put a file called example.ini under this folder.

/*
 * How to read and write value for an .ini
 * file on Java Environment Example
 */
package inifilereaderwriterexample;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * @author tunatore
 */
public class INIFileReaderWriterExample {

public static void main(String[] args) {

//appending to the file
//appending file functionality can be modified by changing true and false parameter of
//FileOutputStream object if the parameter is set to false
//it will remove all content of ini file
//of it is set to true it will append the new key/value pairs
INIFileReaderWriterExample.writeKeyValuePairsINIFile("java", "James Gosling");
INIFileReaderWriterExample.writeKeyValuePairsINIFile("linux", "Linus Torvalds");
INIFileReaderWriterExample.writeKeyValuePairsINIFile("microsoft", "Bill Gates");

INIFileReaderWriterExample.readINIFile();

Map<String, String> keyValueMapXML = new HashMap<String, String>();
keyValueMapXML.put("book1", "How to program Java");
keyValueMapXML.put("book2", "PL/SQL programming best practices");
keyValueMapXML.put("book3", "ORACLE Performance Tuning");

//storing key values a XML inside .ini file
INIFileReaderWriterExample.writeKeyValuePairsINIFileXML(keyValueMapXML);

//reading the XML key value pairs inside .ini file
INIFileReaderWriterExample.readINIFileXML();
}
private static void readINIFile() {

try {
Properties props = new Properties();
//loading the .ini file under ini folder
props.load(new FileInputStream(System.getProperty("user.dir")+"\\ini\\"+"example.ini"));
System.out.println("access with the key java = >> " + props.getProperty("java"));
System.out.println("access with the key linux = >> " + props.getProperty("linux"));
System.out.println("access with the key microsoft = >> " + props.getProperty("microsoft"));
props.list(System.out);

} catch (Exception ex) {
ex.printStackTrace();
}

}

private static void readINIFileXML() {

try {
Properties props = new Properties();
//loading the .ini file under ini folder
props.load(new FileInputStream(System.getProperty("user.dir")+"\\ini\\"+"example.ini"));
props.loadFromXML(new FileInputStream(System.getProperty("user.dir")+"\\ini\\"+"example.ini"));
System.out.println(props.getProperty("book3"));
System.out.println(props.getProperty("book2"));
System.out.println(props.getProperty("book1"));
} catch (Exception ex) {
ex.printStackTrace();
}

}

private static void writeKeyValuePairsINIFile(String key,String value) {

try {
Properties props = new Properties();
//loading the .ini file under ini folder
props.load(new FileInputStream(System.getProperty("user.dir") + "\\ini\\" + "example.ini"));
props.setProperty(key, value);
props.store(new FileOutputStream(System.getProperty("user.dir")
+ "\\ini\\" + "example.ini", true), "Here you can enter comment");
} catch (Exception ex) {
ex.printStackTrace();
}

}
private static void writeKeyValuePairsINIFileXML (Map<String, String> keyValuePairs) {

try {
Properties props = new Properties();
//loading the .ini file under ini folder
props.load(new FileInputStream(System.getProperty("user.dir") + "\\ini\\" + "example.ini"));
Iterator mapInterator = keyValuePairs.entrySet().iterator();
while (mapInterator.hasNext()) {
Map.Entry<String, String> pairs = (Map.Entry) mapInterator.next();
props.setProperty(pairs.getKey(), pairs.getValue());
}
props.storeToXML(new FileOutputStream(System.getProperty("user.dir")
+ "\\ini\\" + "example.ini", false), "Here you can enter comment");
} catch (Exception ex) {
ex.printStackTrace();
}

}

}

Output of the program

run:
access with the key java = >> James Gosling
access with the key linux = >> Linus Torvalds
access with the key microsoft = >> Bill Gates
— listing properties —
java=James Gosling
linux=Linus Torvalds
microsoft=Bill Gates
ORACLE Performance Tuning
PL/SQL programming best practices
How to program Java
BUILD SUCCESSFUL (total time: 0 seconds)

XML inside example.ini file

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Here you can enter comment</comment>
<entry key="java">James Gosling</entry>
<entry key="book3">ORACLE Performance Tuning</entry>
<entry key="microsoft">Bill Gates</entry>
<entry key="linux">Linus Torvalds</entry>
<entry key="book2">PL/SQL programming best practices</entry>
<entry key="book1">How to program Java</entry>
</properties>

Source code of the program

How to use <x:parse> tag of JSTL for parsing XML documents


If you want to parse a XML document, you can use x:parse tag like the following example;

Before using this example don’t forget to include standard.jar and jstl.jar inside lib folder.

Here is the content for the XML file used in this example

<?xml version="1.0" encoding="UTF-8"?>
<SERVERCONFIG>
<SERVERS>
<SERVER NAME="server1" ENABLED="Y">
<IP>10.1.2.1</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>12345</PASSWORD>
<DESCRIPTION>Application Server1</DESCRIPTION>
</SERVER>
<SERVER NAME="server2" ENABLED="Y">
<IP>10.1.2.2</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>54321</PASSWORD>
<DESCRIPTION>Application Server1</DESCRIPTION>
</SERVER>
<SERVER NAME="server3" ENABLED="N">
<IP>10.1.2.3</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>012345</PASSWORD>
<DESCRIPTION>Application Server3</DESCRIPTION>
</SERVER>
<SERVER NAME="server4" ENABLED="Y">
<IP>10.1.2.4</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>0123456</PASSWORD>
<DESCRIPTION>Application Server4</DESCRIPTION>
</SERVER>
<SERVER NAME="server5" ENABLED="N">
<IP>10.1.2.5</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>01234567</PASSWORD>
<DESCRIPTION>Application Server5</DESCRIPTION>
</SERVER>
</SERVERS>
</SERVERCONFIG>

And the JSP page for processing XML content

index.jsp file

<%--
Document   : index
Created on : 16.Eyl.2011, 11:35:25
Author     : tunatore
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body style="background-color: aliceblue">
<center>
<h1>&lt;x:parse&gt; example</h1>
<!--c:import var="server" url="http://localhost:8080/JSTLxparseTagExample/servers.xml"/>-->
<c:import var="server" url="servers.xml"/>
<x:parse xml="${server}" var="output"/>
<table>
<tr>
<td align="center"><b> Getting the first server's information using XPath evaluation of &lt;x:out&gt; tag </b></td>
</tr>
<tr>
<td align="center">
<table border="2">
<tr><td><b>IP:</b></td><td><b><x:out select="$output/SERVERCONFIG/SERVERS/SERVER[1]/IP" /></b></td></tr>
<tr><td><b>USERNAME:</b></td><td><b><x:out select="$output/SERVERCONFIG/SERVERS/SERVER[1]/USERNAME" /></b></td></tr>
<tr><td><b>PASSWORD:</b></td><td><b><x:out select="$output/SERVERCONFIG/SERVERS/SERVER[1]/PASSWORD" /></b></td></tr>
<tr><td><b>DESCRIPTION:</b></td><td><b><x:out select="$output/SERVERCONFIG/SERVERS/SERVER[1]/DESCRIPTION" /></b></td></tr>
</table>
</td>
</tr>
<tr>
<td align="center"><b> Getting all servers using &lt;x:forEach&gt; tag  </b></td>
</tr>
<tr>
<td align="center">
<table border="2">
<x:forEach select="$output/SERVERCONFIG/SERVERS/SERVER"  >
<tr><td><b>SERVER--></b></td><td><b><x:out select="@NAME" /></b></td></tr>
<tr><td><b>IP:</b></td><td><b><x:out select="IP" /></b></td></tr>
<tr><td><b>USERNAME:</b></td><td><b><x:out select="USERNAME" /></b></td></tr>
<tr><td><b>PASSWORD:</b></td><td><b><x:out select="PASSWORD" /></b></td></tr>
<tr><td><b>DESCRIPTION:</b></td><td><b><x:out select="DESCRIPTION" /></b></td></tr>
</x:forEach>
</table>
</td>
</tr>
</table>
</center>
</body>
</html>

And this is the output for the program;

Download sample project

javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/apache/xpath/VariableStack solution


If you get the following exception while using JSTL parse tag you should download the xalan-2.7.1.jar and put into the lib folder.
You can download the xalan-2.7.1.jar from here http://www.findjar.com/jar/mule/dependencies/maven2/xalan/xalan/2.7.1/xalan-2.7.1.jar.html

org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/apache/xpath/VariableStack
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:502)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:412)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause
javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/apache/xpath/VariableStack
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:865)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:794)
org.apache.jsp.index_jsp._jspService(index_jsp.java:99)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

How to get an Oracle table as a XML Document using XMLType


Here is the table creation script;

CREATE TABLE EMPLOYEE
(
ID               NUMBER,
EMPLOYEENAME     VARCHAR2(20 BYTE),
EMPLOYEESURNAME  VARCHAR2(20 BYTE),
GENDER           VARCHAR2(20 BYTE),
SALARY           VARCHAR2(5 BYTE),
EMPLOYEEID       VARCHAR2(20 BYTE)
)

And lets enter some data inside it;

INSERT INTO EMPLOYEE (
ID, EMPLOYEENAME, EMPLOYEESURNAME,
GENDER, SALARY, EMPLOYEEID)
VALUES ( 1, 'tuna' , 'tore' ,
'M',  '1000', 1)
INSERT INTO EMPLOYEE (
ID, EMPLOYEENAME, EMPLOYEESURNAME,
GENDER, SALARY, EMPLOYEEID)
VALUES ( 2, 'linus' , 'torvalds' ,
'M',  '1000', 2)
INSERT INTO EMPLOYEE (
ID, EMPLOYEENAME, EMPLOYEESURNAME,
GENDER, SALARY, EMPLOYEEID)
VALUES ( 3, 'bill' , 'gates' ,
'M',  '1000', 3)

By using XMLType Oracle build-in function we can convert the whole table and get the data inside as a XML document in the following example;

SELECT
XMLTYPE
(
CURSOR
(
SELECT * FROM EMPLOYEE
)
)
FROM
DUAL;

Here is the result;

<?xml version="1.0"?>
<ROWSET>
<ROW>
<ID>1</ID>
<EMPLOYEENAME>tuna</EMPLOYEENAME>
<EMPLOYEESURNAME>tore</EMPLOYEESURNAME>
<GENDER>M</GENDER>
<SALARY>1000</SALARY>
<EMPLOYEEID>1</EMPLOYEEID>
</ROW>
<ROW>
<ID>2</ID>
<EMPLOYEENAME>linus</EMPLOYEENAME>
<EMPLOYEESURNAME>torvalds</EMPLOYEESURNAME>
<GENDER>M</GENDER>
<SALARY>1000</SALARY>
<EMPLOYEEID>2</EMPLOYEEID>
</ROW>
<ROW>
<ID>3</ID>
<EMPLOYEENAME>bill</EMPLOYEENAME>
<EMPLOYEESURNAME>gates</EMPLOYEESURNAME>
<GENDER>M</GENDER>
<SALARY>1000</SALARY>
<EMPLOYEEID>3</EMPLOYEEID>
</ROW>
</ROWSET>

How to produce a HTML output using XML and XSL transformation


In this post, I will show how to produce a HTML file using XML and XSL transformation;

XSL files are used to get (iterate) the required data from XML files.  Lets examine the following web application’s source code;

XML file; bank.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<?xml-stylesheet type=”text/xsl” href=”bank.xsl”?>
<bnk:Bank
xmlns:bnk=”http://www.tunatore.wordpress.com/bank&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://www.tunatore.wordpress.com/bank bank.xsd”>

    <bnk:BankName>IT Bank of Turkey</bnk:BankName>
    <bnk:Branch>
        <bnk:branchID>11111</bnk:branchID>
        <bnk:branchName>Istanbul Branch</bnk:branchName>
        <bnk:branchLocation>Istanbul</bnk:branchLocation>
    </bnk:Branch>

    <bnk:Branch>
        <bnk:branchID>12345</bnk:branchID>
        <bnk:branchName>New York Branch</bnk:branchName>
        <bnk:branchLocation>New York</bnk:branchLocation>
    </bnk:Branch>

    <bnk:Branch>
        <bnk:branchID>222222</bnk:branchID>
        <bnk:branchName>Miami Branch</bnk:branchName>
        <bnk:branchLocation>Miami</bnk:branchLocation>
    </bnk:Branch>

    <bnk:Employee>
        <bnk:employeeID>12345678</bnk:employeeID>
        <bnk:name>Tuna</bnk:name>
        <bnk:surname>TORE</bnk:surname>
        <bnk:profession>IOC Advocate, Abstract Developer</bnk:profession>
    </bnk:Employee>

    <bnk:Employee>
        <bnk:employeeID>987654321</bnk:employeeID>
        <bnk:name>James</bnk:name>
        <bnk:surname>Gosling</bnk:surname>
        <bnk:profession>Java GURU</bnk:profession>
    </bnk:Employee>

    <bnk:Employee>
        <bnk:employeeID>11111111</bnk:employeeID>
        <bnk:name>Linus</bnk:name>
        <bnk:surname>Torvalds</bnk:surname>
        <bnk:profession>Linux GURU</bnk:profession>
    </bnk:Employee>

</bnk:Bank>

XSL file bank.xsl

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
  <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform&#8221;
   xmlns:bnk=”http://www.tunatore.wordpress.com/bank&#8221; exclude-result-prefixes=”xhtml” >
  <xsl:output indent=”yes” method=”html” doctype-public=”-//W3C//DTD XHTML 1.0 Transitional//EN”
    doctype-system=”http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”/&gt;
  <xsl:template match=”bnk:Bank”>
    <html xmlns=”http://www.w3.org/1999/xhtml”&gt;
    <head>
    <title>COMPANY</title>
    </head>
    <body bgcolor=”#CEF6CE”>
        <strong><h1><xsl:value-of select=”bnk:BankName”/></h1></strong>
         <table>
            <tr>
               <td>
               <h2>BRANCHES</h2>
               <table width=”500px” cellpadding=”0″ cellspacing=”0″ border=”1″>
                <tr>
                  <th>branchID</th>
                  <th>branchName</th>
                  <th>branchLocation</th>
                </tr>
                <xsl:for-each select=”//bnk:Branch”>
                  <tr>
                    <td><xsl:value-of select=”bnk:branchID”/></td>
                    <td><xsl:value-of select=”bnk:branchName”/></td>
                    <td><xsl:value-of select=”bnk:branchLocation”/></td>
                  </tr>
                </xsl:for-each>
                </table>
                </td>
             </tr>
             <tr>
               <td width=”500px”>
               <h2>EMPLOYEES</h2>
               <table width=”500px” cellpadding=”0″ cellspacing=”0″ border=”1″>
                <tr>
                  <th>employeeID</th>
                  <th>name</th>
                  <th>surname</th>
                  <th>profession</th>
                </tr>
                <xsl:for-each select=”//bnk:Employee”>
                  <tr>
                    <td><xsl:value-of select=”bnk:employeeID”/></td>
                    <td><xsl:value-of select=”bnk:name”/></td>
                    <td><xsl:value-of select=”bnk:surname”/></td>
                    <td><xsl:value-of select=”bnk:profession”/></td>
                  </tr>
                </xsl:for-each>
                </table>
                </td>
             </tr>
      </table>
    </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Servlet class: TransformXML.java

package xmlxslwebexample;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import java.io.File;
import java.io.PrintWriter;

/**
 *
 * @author tunatore
 */
public class TransformXML extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType(“text/html;charset=UTF-8”);
        PrintWriter out = response.getWriter();
        try {

            //getting XML and XSL files
            File sourceFile = new File(TransformXML.class.getResource(“bank.xml”).getFile().replace(“%20″, ” “));
            File xsltFile = new File(TransformXML.class.getResource(“bank.xsl”).getFile().replace(“%20″, ” “));

            Source xsltSource = new StreamSource(xsltFile);
            Source xmlSource = new StreamSource(sourceFile);

            TransformerFactory factory = TransformerFactory.newInstance();            
            Transformer transformer = factory.newTransformer(xsltSource);
            //producing html output by using XML and XSL transformation
            transformer.transform(xmlSource, new StreamResult(out));

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}

Jsp file: index.jsp

<%–
    Document   : index
    Created on : May 24, 2011, 1:04:01 AM
    Author     : tunatore
–%>

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
   “http://www.w3.org/TR/html4/loose.dtd”&gt;

<html>
    <head>
        <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
        <title>JSP Page</title>
    </head>
    <body>
        <jsp:forward page=”/TransformXML”></jsp:forward>
    </body>
</html>

Sample output:

 

Download Source