1 / 21

CSC3530 Software Technology

CSC3530 Software Technology. Tutorial 10 Assignment Two (III) Demo Update to Assignment One java.net package nanoxml XSLT,XPath. Assignment Two Part III. Demo link http://sparc68.cse.cuhk.edu.hk:8080/examples/servlet/CompareXML

nydia
Download Presentation

CSC3530 Software Technology

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC3530 Software Technology Tutorial 10 Assignment Two (III) Demo Update to Assignment One java.net package nanoxml XSLT,XPath

  2. Assignment Two Part III • Demo link • http://sparc68.cse.cuhk.edu.hk:8080/examples/servlet/CompareXML • http://sparc68.cse.cuhk.edu.hk:8080/examples/servlet/CompareXML1 • Flow of part III • 1.Post a query to servlet – CompareXML (with check flag on) • 2.For each product found in product table, find those corresponding supplier in supplying table • 3.Check if the price quote from supplier exceeds a certain value • 3.1 If yes, post a query to that supplier • 3.2 The URL is obtain in a field URL of supplier table (URL of CompareXML1) • 3.3 Parse the XML return from query • 4.Update the price quote • 5.Presents the user with details of products • Price lower than the current site should not be display

  3. DB1 DB2 Flow diagram of Part III 1 User post a query 5 XML data internet CompareXML1 XML data 3.1 CompareXML 3.3 XML data 2,3 check if need to update CompareXML2 4 update price quote DB

  4. Product - CompareXML Product – CompareXML1 supplying 1 4 2 Get the URL and whenthe quote is updated 5 supplier 3

  5. Update to assignment one database schema • Reuse of table • Originally, you have • Supplying(supplier_code,product_code,price) • Supplier(code,name,address,e-mail,tel) • Product(code,name,category,on_hand,low_limit) • Update • alter table supplying add (quote date,on_hand int) • Indicate when the price quote is updated • How many product the supplier has • alter table supplier add url varchar2(100) • Store the price quote servlet URL of supplier • alter table product add price number(10,2) • Store the price quote of current company

  6. Suggested SQL • select SU.url, S.code from supplying SU, supplier S where SU.supplier_code=S.code and SU.product_code=‘xxx’ and (SYSDATE-SU.quote)*24*60 > 30; • Find those supplier’s price quote URL which supply product xxx to us and the price quote is not update for 30 minutes • update supplying set price=100, quote=SYSDATE where supplier_code=‘xxx’ and product_code=‘yyy’; • Update the price quote of product yyy supplied by supplier xxx and set the time to current time • You should not show supplier whose price quote is lower that your company • SYSDATE is the current date/time • Date arithmetic is in number of days

  7. java.net package • How to post query to a CGI in java program? • Use java.net.URL, java.net.URLConnection • Java will open an http connection for your program • No need to do socket programming • To use • import java.net.*; • Import java.io.*; (for reader and writer) • Key objects • URL – an object to model the url (http://……) • URLConnection – an object to model connection between server and client • PrintWriter – an object for you to post request to a CGI URL • BufferedReader – and object for you to read the CGI output (html page)

  8. Code fragment • Example • http://www.cse.cuhk.edu.hk/~kcsia/csc3530/Query.java • Please run in unix machine, and make sure sparc68 is up URL url=new URL(“http://sparc68.cse.cuhk.edu.hk:8080/examples/servlet/CompareXML”); URLConnection urlconnection=url.openConnection(); urlconnection.setDoOutput(true); PrintWriter out=new PrintWriter(urlconnection.getOutputStream()); out.print(“field=id&query=111”); out.close(); BufferedReader in=new BufferedReader(new InputStreamReader(urlconnection.getInputStream()); String temp; do { temp=in.readLine(); if (temp!=null) System.out.println(temp); else break; } in.close();

  9. Explanation • Construct an URL object, using the URL of price quote CGI • openConnection() - to obtain an URLConnection object • setDoOutput(true) - to enable sending data to CGI • new PrintWriter – obtain an writer object for sending request to CGI • out.println() - send query data to CGI • out.close() – close the writer • … getInputStream() - get the stream for reading CGI output (xml data) • in.close() – close the reader • Parse the XML … (use nanoXML)

  10. XML for exchanging data Tag Name <products> <product> <id>ABCD</id> <description>rogue spear</description> <category>computer games</category> <company> <name>Amazon</name> <price>200.2</price> <onhand>50</onhand> </company> </product> <product> <id>EFGH</id> <description>black thorn</description> <category>computer games</category> <company> <name>Amazon</name> <price>150.3</price> <onhand>200</onhand> </company> </product> </products> Content

  11. nanoxml • How to interpret the XML return from other URL • Use XML parser • http://nanoxml.sourceforge.net/index.htmlversion 1.6.8 • http://www.cse.cuhk.edu.hk/~kcsia/csc3530/nanoxml.jar • To use • Place nanoxml.jar in the same directory with your code orset CLASSPATH to include nanoxml.jar • import nanoxml.*; • XMLElement (a class in nanoxml) • It models a node in the DOM tree • Methods to use • parseString() • getChildren() • getTagName() • getContents()

  12. products product id category company description price name DOM Tree and XMLElement All nodes are XMLElement object getTagName() returns the node’s name getChildren() returns the childnodes contained in a Vectorobject getContents(), e.g. when call on price XMLElementit will return 150.3

  13. How to use XML Sample: http://www.cse.cuhk.edu.hk/~kcsia/csc3530/QueryXML.java … String xml=“<products><product><company><price>200</price></company></product></products>”; XMLElement root=new XMLElement(); root.parseString(xml); XMLElement product=findTag(root,"product"); if (product!=null) { XMLElement company=findTag(product,"company"); if (company!=null) { XMLElement price=findTag(company,"price"); if (price!=null) { System.out.println("The price is:“+ price.getContents()); } } } } // findTag is a function that you have written

  14. How to use XML private static XMLElement findTag(XMLElement src,String str) { Vector v = src.getChildren(); for (int i = 0; i < v.size(); i++) { XMLElement e = (XMLElement)(v.elementAt(i)); if (e.getTagName().compareTo(str) == 0) { return e; } } return null; } • To use Vector, you should import java.util.*; • getChildren() is a method of XMLElement • If you call product.getChildren, it will return a vector containing XMLElement: id, description, category and company. • getTagName() and getContents() (refer to page 9)

  15. XSLT, XPath • How to present the XML in a browser? • Specify a XSL file in the XML • <?xml:stylesheet type="text/xsl" href="http://www.cse.cuhk.edu.hk/~kcsia/display.xsl"?> • XSL - eXtensible Stylesheet Language • To transform XML document to HTML (mainly) • XSL has two standard versions • <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> • version 1.0 (supported by IE 6, need to install msxml 3.0 in replace mode) • http://www.cse.cuhk.edu.hk/~kcsia/csc3530/XmlInst.exe • <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> • Working draft (supported by IE 5.5)

  16. Sample XSL file <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <HTML> <HEAD> <TITLE>Query Result</TITLE> </HEAD> <BODY> <H1>Query Result</H1> <xsl:for-each select="/products/product"> Product ID: <xsl:value-of select="id"/><BR/> Product Code: <xsl:value-of select="description"/><BR/> Product Category: <xsl:value-of select="category"/><BR/> <TABLE BORDER="1"> <TR> <TH>company name</TH> <TH>price</TH> <TH>on hand</TH> </TR>

  17. Sample XSL file <xsl:for-each select="company"> <xsl:sort select="price" data-type="number" order="ascending" /> <TR> <TD><xsl:value-of select="name"/></TD> <TD><xsl:value-of select="price"/></TD> <TD><xsl:value-of select="onhand"/></TD> </TR> </xsl:for-each> </TABLE><BR/> </xsl:for-each> </BODY> </HTML> </xsl:template> </xsl:stylesheet>

  18. XPath • XPath is to enable the addressing of, or navigation to, chosen part of XML document • XSL use XPath for testing whether or not an node matches a pattern • <xsl:template match="/"> • Xsl will process the whole xml file, / means the root • <xsl:for-each select="/products/product"> • XPath: /products/product • Find the nodes named product, with parent node named products • For-each loop will take out these node and do an iteration • <xsl:sort select="id" data-type="text" order="ascending" /> • Sort the selected nodes according to the content in child node id • Sort function is supported in XSLT version 1.0 • <xsl:value-of select="name"/> • Display the value (content) in node named “name”

  19. authors author (period) name nationality XPath • Describe a path through the XML hierarchy with a slash-separated list of child element names • Identify all the elements that match the path • Simple query mechanism authors/*/name authors/author/* authors/author[nationality=‘Russian’]/name authors/author/[@period=“classical”]

  20. IE Setting Prompt or Enable

  21. Reference • TopXML • http://www.topxml.com/default.asp • Servlet 2.1 Documentation • http://java.sun.com/products/servlet/2.1/api/packages.html • JDK1.3 Documentation • http://java.sun.com/j2se/1.3/docs/api/index.html • Java Tutorial • http://java.sun.com/docs/books/tutorial/

More Related