Tải bản đầy đủ - 0 (trang)
5 Dynamic Example XML/XSL Web Interface with WSAD

5 Dynamic Example XML/XSL Web Interface with WSAD

Tải bản đầy đủ - 0trang

//Element

timeSheetElem=doc.createElement("TimeSheet");

doc.appendChild(timeSheetElem);

timeSheetElem.setAttribute("weekEnding",

"04/25/2003");//
name="Leonard,Elouise"

//deptName="Programming"/>

ElementemployeeElem=

doc.createElement("Employee");

timeSheetElem.appendChild(employeeElem);

employeeElem.setAttribute("id","257899");

employeeElem.setAttribute("name","Leonard,Elouise");

employeeElem.setAttribute("deptName",

"Programming");if

(showDetails.equalsIgnoreCase("on")){

//

ElemententriesElem=doc.createElement("Entries");

timeSheetElem.appendChild(entriesElem);//Entry1

//

ElemententryElem=doc.createElement("Entry");

entriesElem.appendChild(entryElem);//

4/21/2003ElementdateElem=



doc.createElement("Date");NodetextNode=

doc.createTextNode("4/21/2003");

dateElem.appendChild(textNode);

entryElem.appendChild(dateElem);//

8.5ElementhoursElem=

doc.createElement("Hours");textNode=

doc.createTextNode("8.5");

hoursElem.appendChild(textNode);

entryElem.appendChild(hoursElem);

hoursElem.setNodeValue("8.5");//
name="ACMEAP"/>ElementprojectElem=

doc.createElement("Project");

entryElem.appendChild(projectElem);

projectElem.setAttribute("id","4567");

projectElem.setAttribute("name","ACMEAP");//...

Additionalentriesnotshown}

//

ElementsummaryElem=

doc.createElement("Summary");

timeSheetElem.appendChild(summaryElem);

summaryElem.setAttribute("totalHours","45.5");

summaryElem.setAttribute("overTimeHours","5.5");//

ElementprojectTotalsElem=

doc.createElement("ProjectTotals");

summaryElem.appendChild(projectTotalsElem);//




//totalHours="37.5"/>

Elementproject1Elem=doc.createElement("Project");

projectTotalsElem.appendChild(project1Elem);

project1Elem.setAttribute("id","4567");

project1Elem.setAttribute("name","ACMEAP");

project1Elem.setAttribute("totalHours","37.5");//


//totalHours="8"/>

Elementproject2Elem=doc.createElement("Project");

projectTotalsElem.appendChild(project2Elem);

project2Elem.setAttribute("id","2345");

project2Elem.setAttribute("name","ACMEHR");

project2Elem.setAttribute("totalHours","8");returndoc;

}

}

packagecom.wsbook.xmlxsl.sample;

importjava.io.InputStream;

publicclassClassPathURIResolverimplements



javax.xml.transform.URIResolver{

publicjavax.xml.transform.Sourceresolve(Stringhref,

Stringbase)

throwsjavax.xml.transform.TransformerException{

javax.xml.transform.stream.StreamSourceresult=null;

Stringpath=href;

try{

InputStreaminputStream=getClass().

getResourceAsStream(path);

System.out.println("ClassPathURIResolver.resolve()path

="

+path);

if(inputStream!=null){

result=new

javax.xml.transform.stream.StreamSource();

result.setInputStream(inputStream);}

}catch(Exceptionexc){



System.out.println("Exceptionon-->href="

+href+"base="+base+"Exception="+exc);}

System.out.println("resolvedpath="+path+"href=

"

+href+"base="+base);returnresult;

}

}

InputStreaminputStream=

getClass().getResourceAsStream(path);

packagecom.wsbook.xmlxsl.sample;

importjava.io.IOException;

importjava.io.Writer;

importjava.util.Properties;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;import



javax.servlet.http.HttpServletResponse;import

javax.xml.transform.Source;

importjavax.xml.transform.Transformer;

importjavax.xml.transform.TransformerFactory;import

org.apache.xalan.serialize.SerializerToXML;import

org.w3c.dom.Document;

publicclassTimeSheetXSLServletextendsHttpServlet{

publicstaticfinalStringXSL_FILE=

"/timesheet2.xsl";publicvoiddoGet(HttpServletRequest

req,HttpServletResponseresp)throwsServletException,

IOException{

StringshowDetails=req.getParameter("showDetails");

if(showDetails==null){

showDetails="off";

}

Stringtransform=req.getParameter("transform");if

(transform==null){

transform="off";



}

TimeSheetXMLGeneratorgenerator=new

TimeSheetXMLGenerator();DocumentxmlDoc=

generator.generateXML(showDetails);if

(transform.equalsIgnoreCase("on")){

render(req,resp,xmlDoc,newProperties());}else{

dumpXML(xmlDoc,resp.getWriter());}

}

publicvoidrender(HttpServletRequestreq,

HttpServletResponseresp,DocumentxmlDoc,Properties

xslParms){

try{

Sourcesrc=new

javax.xml.transform.dom.DOMSource(xmlDoc);

TransformerFactorytFactory=

TransformerFactory.newInstance();

tFactory.setURIResolver(newClassPathURIResolver());

Transformertransformer=



tFactory.newTransformer(tFactory.getURIResolver().

resolve(XSL_FILE,null));javax.xml.transform.Result

result=newjavax.xml.transform.stream.StreamResult(

resp.getWriter());

transformer.clearParameters();java.util.Enumeration

parmNames=xslParms.keys();while

(parmNames.hasMoreElements()){

StringparmName=(String)

parmNames.nextElement();StringparmValue=(String)

xslParms.get(parmName);

transformer.setParameter(parmName,parmValue);

}

transformer.transform(src,result);}catch(Exceptione)

{

System.out.println(

getClass().getName()+

"Exceptionoccurred-"+e);e.printStackTrace();

}



}

publicvoiddumpXML(org.w3c.dom.NodexmlTree,

Writerwriter){

try{

SerializerToXMLs2x=newSerializerToXML();

s2x.setWriter(writer);java.util.Propertiesprops=new

java.util.Properties();

props.setProperty(javax.xml.transform.OutputKeys.INDENT,

"yes");

s2x.setOutputFormat(props);s2x.serialize(xmlTree);}

catch(IOExceptionioExc){

}

}

}

tFactory.setURIResolver(newClassPathURIResolver());

Transformertransformer=

tFactory.newTransformer(tFactory.getURIResolver().

resolve(XSL_FILE,null));




4.01Transitional//EN">



index.html





TesttheXMLXSLExample






action="/XMLXSLExample/TimeSheetXSLServlet"

name="XMLXSLTransformer"target="_blank">Show

Details?


checked="true">


Transform?


checked="true">








value="Submit">





Totestyourwork,simplyrunindex.htmlonaWebSphere

5.0TEserver.Selecttheindex.htmlfileandthenfrom

thecontextmenu,selectRunonServer.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

5 Dynamic Example XML/XSL Web Interface with WSAD

Tải bản đầy đủ ngay(0 tr)

×