Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Relative paths in Java , working with servlets, and XML

Status
Not open for further replies.

coderGirl

Programmer
Oct 21, 2003
22
AU
Hi

I have this servlet which runs on myPc at the moment and is executed via
anyway this servlet creats and XML Document.

Then i want to call another class which code iwill show below. this class reads the XML values and stores them in an array. the other class graps this infprmation and for now loops through it and displays it.

the problem is i can't seem to get the XML reader to find the file on the system.

well heres the code hope someone can help

OH AND ALSO THE DIRECTORY STRUCTURE ON MY SYSTEM IS
..\jsdk2.1 IS THE ROOT AND WHERE THE xml APPEARS
..\jsdk2.1\examples\WEB-INF\servlets THIS IS WHERE THE SERVLET AND OTHER CODE APPEARS

SERVLET
/*
*
*This servlet creates the web form through which you can insert a new record
*into the database.
*
*It will check to ensure that data is entered into all
*
*/
import java.io.*;

//import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;


public class Fiddle extends HttpServlet {

public ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
public String tagStart;
public String tableName = "none";
public String[] details;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(&quot;<html>&quot;);
out.println(&quot;<body>&quot;);
out.println(&quot;<head>&quot;);
out.println(&quot;<title>Insert Details in DB2</title>&quot;);
out.println(&quot;</head>&quot;);
out.println(&quot;<body>&quot;);
out.println(&quot;<h1>Database Input</h1>&quot;);
out.println(&quot;<table border=0><tr><td>&quot;);
out.println(&quot;<P>&quot;);

out.print(&quot;<form action=\&quot;&quot;);
out.print(&quot;Fiddle\&quot; &quot;);
out.println(&quot;method=POST>&quot;);

out.println(rb.getString(&quot;requestparams.DB_NAME&quot;));
out.println(&quot;<input type=text size=20 name=DB_NAME>&quot;);
out.println(&quot;<br>&quot;);

out.println(rb.getString(&quot;requestparams.NB_CLIENT&quot;));
out.println(&quot;<input type=text size=20 name=NB_CLIENT>&quot;);
out.println(&quot;<br>&quot;);

out.println(rb.getString(&quot;requestparams.TY_CLIENT&quot;));
out.println(&quot;<input type=text size=20 name=TY_CLIENT>&quot;);
out.println(&quot;<br>&quot;);

out.println(rb.getString(&quot;requestparams.TX_SRCE_ENTRY_CHNG&quot;));
out.println(&quot;<input type=text size=20 name=TX_SRCE_ENTRY_CHNG>&quot;);
out.println(&quot;<br>&quot;);

out.println(rb.getString(&quot;requestparams.NM_OPER_MAKNG_CHNG&quot;));
out.println(&quot;<input type=text size=20 name=NM_OPER_MAKNG_CHNG>&quot;);
out.println(&quot;<br>&quot;);

out.println(rb.getString(&quot;requestparams.DT_UPDATE&quot;));
out.println(&quot;<input type=text size=20 name=DT_UPDATE>&quot;);
out.println(&quot;<br>&quot;);

out.println(rb.getString(&quot;requestparams.TM_UPDATE&quot;));
out.println(&quot;<input type=text size=20 name=TM_UPDATE>&quot;);
out.println(&quot;<br>&quot;);

out.println(rb.getString(&quot;requestparams.CD_RSN_REMO_CLIENT&quot;));
out.println(&quot;<input type=text size=20 name=CD_RSN_REMO_CLIENT>&quot;);
out.println(&quot;<br>&quot;);

out.println(rb.getString(&quot;requestparams.DT_REMOVAL_CLIENT&quot;));
out.println(&quot;<input type=text size=20 name=DT_REMOVAL_CLIENT>&quot;);
out.println(&quot;<br>&quot;);

out.println(rb.getString(&quot;requestparams.MB_REPLACE_CLIENT&quot;));
out.println(&quot;<input type=text size=20 name=MB_REPLACE_CLIENT>&quot;);
out.println(&quot;<br>&quot;);

out.println(rb.getString(&quot;requestparams.NB_PHONETIC_NM&quot;));
out.println(&quot;<input type=text size=20 name=NB_PHONETIC_NM>&quot;);
out.println(&quot;<br>&quot;);

out.println(rb.getString(&quot;requestparams.NB_DRVR_LCNCE&quot;));
out.println(&quot;<input type=text size=20 name=NB_DRVR_LCNCE>&quot;);
out.println(&quot;<br>&quot;);

out.println(rb.getString(&quot;requestparams.IN_VIC_LICENCE&quot;));
out.println(&quot;<input type=text size=20 name=IN_VIC_LICENCE>&quot;);
out.println(&quot;<br>&quot;);

out.println(&quot;<input type=submit>&quot;);
out.println(&quot;</form>&quot;);

out.println(&quot;</td><td>&quot;);

String DB_NAME = request.getParameter(&quot;DB_NAME&quot;);
String NB_CLIENT = request.getParameter(&quot;NB_CLIENT&quot;);
String TY_CLIENT = request.getParameter(&quot;TY_CLIENT&quot;);
String TX_SRCE_ENTRY_CHNG = request.getParameter(&quot;TX_SRCE_ENTRY_CHNG&quot;);
String NM_OPER_MAKNG_CHNG = request.getParameter(&quot;NM_OPER_MAKNG_CHNG&quot;);
String DT_UPDATE = request.getParameter(&quot;DT_UPDATE&quot;);
String TM_UPDATE = request.getParameter(&quot;TM_UPDATE&quot;);
String CD_RSN_REMO_CLIENT = request.getParameter(&quot;CD_RSN_REMO_CLIENT&quot;);
String DT_REMOVAL_CLIENT = request.getParameter(&quot;DT_REMOVAL_CLIENT&quot;);
String MB_REPLACE_CLIENT = request.getParameter(&quot;MB_REPLACE_CLIENT&quot;);
String NB_PHONETIC_NM = request.getParameter(&quot;NB_PHONETIC_NM&quot;);
String NB_DRVR_LCNCE = request.getParameter(&quot;NB_DRVR_LCNCE&quot;);
String IN_VIC_LICENCE = request.getParameter(&quot;IN_VIC_LICENCE&quot;);

out.println(rb.getString(&quot;requestparams.params-in-req&quot;) + &quot;<br>&quot;);
out.println(&quot;<p>1</p><br>&quot;);

if (DB_NAME == null || NB_CLIENT == null || TY_CLIENT == null ||
TX_SRCE_ENTRY_CHNG == null || NM_OPER_MAKNG_CHNG == null ||
DT_UPDATE == null || TM_UPDATE == null ||CD_RSN_REMO_CLIENT == null ||
DT_REMOVAL_CLIENT == null || MB_REPLACE_CLIENT == null ||NB_PHONETIC_NM == null ||
NB_DRVR_LCNCE == null || IN_VIC_LICENCE == null)
{
out.println(&quot;<p>2</p><br>&quot;);
out.println(rb.getString(&quot;requestparams.no-params&quot;));
}
else if (DB_NAME.equals(&quot;&quot;) || NB_CLIENT.equals(&quot;&quot;) || TY_CLIENT.equals(&quot;&quot;) ||
TX_SRCE_ENTRY_CHNG.equals(&quot;&quot;) || NM_OPER_MAKNG_CHNG.equals(&quot;&quot;) ||
DT_UPDATE.equals(&quot;&quot;) || TM_UPDATE.equals(&quot;&quot;) ||CD_RSN_REMO_CLIENT.equals(&quot;&quot;) ||
DT_REMOVAL_CLIENT.equals(&quot;&quot;) || MB_REPLACE_CLIENT.equals(&quot;&quot;) ||NB_PHONETIC_NM.equals(&quot;&quot;) ||
NB_DRVR_LCNCE.equals(&quot;&quot;) || IN_VIC_LICENCE.equals(&quot;&quot;))
{
out.println(&quot;<p>2</p><br>&quot;);
out.println(rb.getString(&quot;requestparams.no-params&quot;));
}
else {
out.println(&quot;<p>3</p><br>&quot;);
out.println(rb.getString(&quot;requestparams.DB_NAME&quot;));
out.println(&quot; = &quot; + DB_NAME + &quot;<br>&quot;);
out.println(rb.getString(&quot;requestparams.NB_CLIENT&quot;));
out.println(&quot; = &quot; + NB_CLIENT + &quot;<br>&quot;);
out.println(rb.getString(&quot;requestparams.TY_CLIENT&quot;));
out.println(&quot; = &quot; + TY_CLIENT + &quot;<br>&quot;);
out.println(rb.getString(&quot;requestparams.TX_SRCE_ENTRY_CHNG&quot;));
out.println(&quot; = &quot; + TX_SRCE_ENTRY_CHNG + &quot;<br>&quot;);
out.println(rb.getString(&quot;requestparams.NM_OPER_MAKNG_CHNG&quot;));
out.println(&quot; = &quot; + NM_OPER_MAKNG_CHNG + &quot;<br>&quot;);
out.println(rb.getString(&quot;requestparams.DT_UPDATE&quot;));
out.println(&quot; = &quot; + DT_UPDATE + &quot;<br>&quot;);
out.println(rb.getString(&quot;requestparams.TM_UPDATE&quot;));
out.println(&quot; = &quot; + TM_UPDATE + &quot;<br>&quot;);
out.println(rb.getString(&quot;requestparams.CD_RSN_REMO_CLIENT&quot;));
out.println(&quot; = &quot; + CD_RSN_REMO_CLIENT + &quot;<br>&quot;);
out.println(rb.getString(&quot;requestparams.DT_REMOVAL_CLIENT&quot;));
out.println(&quot; = &quot; + DT_REMOVAL_CLIENT + &quot;<br>&quot;);
out.println(rb.getString(&quot;requestparams.MB_REPLACE_CLIENT&quot;));
out.println(&quot; = &quot; + MB_REPLACE_CLIENT + &quot;<br>&quot;);
out.println(rb.getString(&quot;requestparams.NB_PHONETIC_NM&quot;));
out.println(&quot; = &quot; + NB_PHONETIC_NM + &quot;<br>&quot;);
out.println(rb.getString(&quot;requestparams.NB_DRVR_LCNCE&quot;));
out.println(&quot; = &quot; + NB_DRVR_LCNCE + &quot;<br>&quot;);
out.println(rb.getString(&quot;requestparams.IN_VIC_LICENCE&quot;));
out.println(&quot; = &quot; + IN_VIC_LICENCE + &quot;<br>&quot;);
}
out.println(&quot;</td></tr></table>&quot;);
out.println(&quot;</body>&quot;);
out.println(&quot;</html>&quot;);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
write(createDocument(request));
RetreiveData ret = new RetreiveData();
ret.getData(&quot;output.xml/../../..&quot;);
String[] tem = ret.details;
String val;
PrintWriter out = response.getWriter();
if(tem != null){
for(int i = 0; i<tem.length;i++){

out.println(&quot;<p>&quot;+tem+&quot;</p><br>&quot;);
}
}
else{
out.println(&quot;no values entered&quot;);
}
doGet(request, response);
}


public Document createDocument(HttpServletRequest request) {
Document document = DocumentHelper.createDocument();
String DB_NAME = request.getParameter(&quot;DB_NAME&quot;);
String NB_CLIENT = request.getParameter(&quot;NB_CLIENT&quot;);
String TY_CLIENT = request.getParameter(&quot;TY_CLIENT&quot;);
String TX_SRCE_ENTRY_CHNG = request.getParameter(&quot;TX_SRCE_ENTRY_CHNG&quot;);
String NM_OPER_MAKNG_CHNG = request.getParameter(&quot;NM_OPER_MAKNG_CHNG&quot;);
String DT_UPDATE = request.getParameter(&quot;DT_UPDATE&quot;);
String TM_UPDATE = request.getParameter(&quot;TM_UPDATE&quot;);
String CD_RSN_REMO_CLIENT = request.getParameter(&quot;CD_RSN_REMO_CLIENT&quot;);
String DT_REMOVAL_CLIENT = request.getParameter(&quot;DT_REMOVAL_CLIENT&quot;);
String MB_REPLACE_CLIENT = request.getParameter(&quot;MB_REPLACE_CLIENT&quot;);
String NB_PHONETIC_NM = request.getParameter(&quot;NB_PHONETIC_NM&quot;);
String NB_DRVR_LCNCE = request.getParameter(&quot;NB_DRVR_LCNCE&quot;);
String IN_VIC_LICENCE = request.getParameter(&quot;IN_VIC_LICENCE&quot;);

if (DB_NAME.equals(&quot;&quot;) || NB_CLIENT.equals(&quot;&quot;) || TY_CLIENT.equals(&quot;&quot;) ||
TX_SRCE_ENTRY_CHNG.equals(&quot;&quot;) || NM_OPER_MAKNG_CHNG.equals(&quot;&quot;) ||
DT_UPDATE.equals(&quot;&quot;) || TM_UPDATE.equals(&quot;&quot;) ||CD_RSN_REMO_CLIENT.equals(&quot;&quot;) ||
DT_REMOVAL_CLIENT.equals(&quot;&quot;) || MB_REPLACE_CLIENT.equals(&quot;&quot;) ||NB_PHONETIC_NM.equals(&quot;&quot;) ||
NB_DRVR_LCNCE.equals(&quot;&quot;) || IN_VIC_LICENCE.equals(&quot;&quot;)) {

Element root = document.addElement( &quot;root&quot; );
Element author1 = root.addElement( &quot;author&quot; )
.addAttribute( &quot;name&quot;, &quot;James&quot; )
.addAttribute( &quot;location&quot;, &quot;UK&quot; )
.addText( &quot;James Strachan&quot; );

Element author2 = root.addElement( &quot;author&quot; )
.addAttribute( &quot;name&quot;, &quot;Bob&quot; )
.addAttribute( &quot;location&quot;, &quot;US&quot; )
.addText( &quot;Bob McWhirter&quot; );
return document;
}
else {
Element root = document.addElement( &quot;DB_NAME&quot;);

Element detail1 = root.addElement( &quot;NB_CLIENT&quot; )
.addText( &quot;&quot; + NB_CLIENT );

Element detail2 = root.addElement( &quot;TY_CLIENT&quot; )
.addText( &quot;&quot; + TY_CLIENT );

Element detail3 = root.addElement( &quot;TX_SRCE_ENTRY_CHNG&quot; )
.addText( &quot;&quot; + TX_SRCE_ENTRY_CHNG );

Element detail4 = root.addElement( &quot;NM_OPER_MAKNG_CHNG&quot; )
.addText( &quot;&quot; + NM_OPER_MAKNG_CHNG );

Element detail5 = root.addElement( &quot;DT_UPDATE&quot; )
.addText( &quot;&quot; + DT_UPDATE );

Element detail6 = root.addElement( &quot;TM_UPDATE&quot; )
.addText( &quot;&quot; + TM_UPDATE );

Element detail7 = root.addElement( &quot;CD_RSN_REMO_CLIENT&quot; )
.addText( &quot;&quot; + CD_RSN_REMO_CLIENT );

Element detail8 = root.addElement( &quot;DT_REMOVAL_CLIENT&quot; )
.addText( &quot;&quot; + DT_REMOVAL_CLIENT );

Element detail9 = root.addElement( &quot;MB_REPLACE_CLIENT&quot; )
.addText( &quot;&quot; + MB_REPLACE_CLIENT );

Element detail10 = root.addElement( &quot;NB_DRVR_LCNCE&quot; )
.addText( &quot;&quot; + NB_DRVR_LCNCE );

Element detail11 = root.addElement( &quot;NB_PHONETIC_NM&quot; )
.addText( &quot;&quot; + NB_PHONETIC_NM );

Element detail12 = root.addElement( &quot;IN_VIC_LICENCE&quot; )
.addText( &quot;&quot; + IN_VIC_LICENCE );
return document;
}
}
public void write(Document document) throws IOException {
// lets write to a file
FileWriter fw = new FileWriter(&quot;output.xml&quot;);
XMLWriter writer = new XMLWriter(fw);
writer.write( document );
writer.close();
}
}

XML READER
/*
* Created on Oct 12, 2003
*
* To change the template for this generated file go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler ;
/**
* @author Nicole
*
* used to retreive details out of XML tages
* and store them as relevant details
* to be sent to a database
*
*/
public class RetreiveData extends DefaultHandler{
public String tagStart;
public String tableName = &quot;none&quot;;
public String[] details;
public RetreiveData ss ;
public RetreiveData(){

}
// public static void main(String[]args)
public void getData(String path)
{
try
{
Class c = Class.forName(&quot;org.apache.xerces.parsers.SAXParser&quot;);
XMLReader reader = (XMLReader)c.newInstance();
ss = new RetreiveData();
reader.setContentHandler(ss);
reader.parse(path);
}
catch(Exception e){System.out.println(e);}
}

public void startElement(String uri, String local_name, String raw_name, Attributes amap)
throws SAXException
{
//System.out.println(&quot;start &quot; + local_name + &quot; found &quot;);
tagStart = local_name;
if(tagStart.equalsIgnoreCase(&quot;SP0100&quot;)){
System.out.println(&quot;\tNew SP0100&quot;);
tableName = tagStart;
details = new String[12];
}
}

public void endElement(String uri, String local_name, String raw_name)
throws SAXException
{
//System.out.println(&quot;end &quot; + local_name + &quot; found&quot;)
if(local_name.equalsIgnoreCase(&quot;SP0100&quot;)){
System.out.println(&quot;\tEnd SP0100&quot;);
}
}

public void startDocument() throws SAXException
{
System.out.println(&quot;start document&quot;);
}

public void endDocument() throws SAXException
{
System.out.println(&quot;end document&quot;);
if(tableName.equalsIgnoreCase(&quot;none&quot;)){
System.out.println(&quot;No such Table&quot;);
}
else{
System.out.println(&quot;\n&quot; + tableName);
for(int i = 0; i <details.length; i++){
System.out.print(details + &quot;, &quot;);
}
new PopulateD0100B(details, tableName);
}
}
public void characters(char[] ch, int start, int length)
throws SAXException
{
if(tableName.equalsIgnoreCase(&quot;SP0100&quot;)){
if(tagStart.equalsIgnoreCase(&quot;NB_CLIENT&quot;)){
System.out.println(&quot;\t\tNB_CLIENT - &quot; +new String(ch,start,length));
details[0]= new String(ch,start,length);
}
else if(tagStart.equalsIgnoreCase(&quot;TY_CLIENT&quot;)){
System.out.println(&quot;\t\tTY_CLIENT - &quot; +new String(ch,start,length));
details[1]= new String(ch,start,length);
}
else if(tagStart.equalsIgnoreCase(&quot;TX_SRCE_ENTRY_CHNG&quot;)){
System.out.println(&quot;\t\tTX_SRCE_ENTRY_CHNG - &quot; +new String(ch,start,length));
details[2]= new String(ch,start,length);
}
else if(tagStart.equalsIgnoreCase(&quot;NM_OPER_MAKNG_CHNG&quot;)){
System.out.println(&quot;\t\tNM_OPER_MAKNG_CHNG - &quot; +new String(ch,start,length));
details[3]= new String(ch,start,length);
}
else if(tagStart.equalsIgnoreCase(&quot;DT_UPDATE&quot;)){
System.out.println(&quot;\t\tDT_UPDATE - &quot; +new String(ch,start,length));
details[4]= new String(ch,start,length);
}
else if(tagStart.equalsIgnoreCase(&quot;TM_UPDATE&quot;)){
System.out.println(&quot;\t\tTM_UPDATE - &quot; +new String(ch,start,length));
details[5]= new String(ch,start,length);
}
else if(tagStart.equalsIgnoreCase(&quot;CD_RSN_REMO_CLIENT&quot;)){
System.out.println(&quot;\t\tCD_RSN_REMO_CLIENT - &quot; +new String(ch,start,length));
details[6]= new String(ch,start,length);
}
else if(tagStart.equalsIgnoreCase(&quot;DT_REMOVAL_CLIENT&quot;)){
System.out.println(&quot;\t\tDT_REMOVAL_CLIENT - &quot; +new String(ch,start,length));
details[7] = new String(ch,start,length);
}
else if(tagStart.equalsIgnoreCase(&quot;MB_REPLACE_CLIENT&quot;)){
System.out.println(&quot;\t\tMB_REPLACE_CLIENT - &quot; +new String(ch,start,length));
details[8]= new String(ch,start,length);
}
else if(tagStart.equalsIgnoreCase(&quot;NB_PHONETIC_NM&quot;)){
System.out.println(&quot;\t\tNB_PHONETIC_NM - &quot; +new String(ch,start,length));
details[9]= new String(ch,start,length);
}
else if(tagStart.equalsIgnoreCase(&quot;NB_DRVR_LCNCE&quot;)){
System.out.println(&quot;\t\tNB_DRVR_LCNCE - &quot; +new String(ch,start,length));
details[10]= new String(ch,start,length);
}
else if(tagStart.equalsIgnoreCase(&quot;IN_VIC_LICENCE&quot;)){
System.out.println(&quot;\t\tIN_VIC_LICENCE - &quot; +new String(ch,start,length));
details[11]= new String(ch,start,length);
}
}
tagStart=&quot;&quot;;
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top