XSLT 1 URL: XSLT 2 URL:"> XSLT 1 URL: XSLT 2 URL:">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. SC Cheung (HKUST), Prof. Francis Lau (HKU) Reference: XML How To Program, Deitel, Prentice Hall 2001 CSIT600b:

Similar presentations


Presentation on theme: "1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. SC Cheung (HKUST), Prof. Francis Lau (HKU) Reference: XML How To Program, Deitel, Prentice Hall 2001 CSIT600b:"— Presentation transcript:

1 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. SC Cheung (HKUST), Prof. Francis Lau (HKU) Reference: XML How To Program, Deitel, Prentice Hall 2001 CSIT600b: XML Programming Getting Started 3

2 Dickson Chiu 2004CSIT600b 01-2 Parsing a Web page with Jtidy Better parser than the standard Java library Clean up malformed and faulty HTML More functions Installation Download from http://jtidy.sourceforge.net/http://jtidy.sourceforge.net/ Put Tidy.jar into H:\Sun\AppServer\lib Edit H:\Sun\AppServer\j2eetutorial14\examples\common\targets.xml Add red line for library

3 Dickson Chiu 2004CSIT600b 01-3 Front Page Interface CSIT600B HTML to DOM Demo HTML to DOM Demo Source URL: <input name="sourceURL“ value="http://finance.yahoo.com/q/cp?s=^HSI" type="text" size="50"> XSLT 1 URL: XSLT 2 URL:

4 Dickson Chiu 2004CSIT600b 01-4 Parsing the Input import javax.servlet.http.*;import javax.servlet.*; import java.io.*;import java.net.*; import org.w3c.tidy.*;import org.w3c.dom.*; import javax.xml.transform.*;import javax.xml.transform.stream.*; import javax.xml.transform.dom.*; public class Html2Dom extends HttpServlet { public Html2Dom() { } protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String sourceUrlStr = req.getParameter("sourceURL"); // get the parameter String xslt1UrlStr = req.getParameter("xslt1URL"); String xslt2UrlStr = req.getParameter("xslt2URL"); URL sourceURL = new URL(sourceUrlStr); // open the connection with that url URLConnection cn = sourceURL.openConnection(); Tidy tidy = new Tidy(); // parse the html file into dom tidy.setIndentContent(true); tidy.setXHTML(true); tidy.setWraplen(Integer.MAX_VALUE); Document doc = tidy.parseDOM(cn.getInputStream(), null);

5 Dickson Chiu 2004CSIT600b 01-5 Output with XSLT if (xslt1UrlStr.equals("")) tidy.pprint(doc, resp.getOutputStream()); else { try { // create the transformer factory object TransformerFactory factory = TransformerFactory.newInstance(); Transformer t = factory.newTransformer(); // create the transformer for output URL xslt1URL = new URL(xslt1UrlStr); // open connection with xslt1 URLConnection xslt1cn = xslt1URL.openConnection(); // create the transformer for xslt1 Transformer t1 = factory.newTransformer(new StreamSource(xslt1cn. getInputStream())); DOMResult result1 = new DOMResult(); t1.transform(new DOMSource(doc), result1); // transformation with xslt1 org.w3c.dom.Node result = result1.getNode(); // get result node if (!xslt2UrlStr.equals("")){ URL xslt2URL = new URL(xslt2UrlStr); // open connection with xslt2 URLConnection xslt2cn = xslt2URL.openConnection(); // create the transformer for xslt2 Transformer t2 = factory.newTransformer(new StreamSource(xslt2cn.getInputStream())); DOMResult result2 = new DOMResult(); t2.transform(new DOMSource(result), result2); // transformation with xslt2 result = result2.getNode(); } t.transform(new DOMSource(result), new StreamResult(resp.getOutputStream())); } catch (TransformerException ex1) { } } } } Different types – Different purpose URL connection of XSLT

6 Dickson Chiu 2004CSIT600b 01-6 WAR Structure Duplicate directory of example hello2 for testing Include Tidy.jar Alias /result for the servlet Context root /html2dom

7 Dickson Chiu 2004CSIT600b 01-7 Where to place the XSLT Manually put the XSLT file at the docroot: H:\Sun\AppServer\domains\domain1\docroot Or put it in the application directory Actually can be any URL


Download ppt "1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. SC Cheung (HKUST), Prof. Francis Lau (HKU) Reference: XML How To Program, Deitel, Prentice Hall 2001 CSIT600b:"

Similar presentations


Ads by Google