Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 – 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 Flow diagram of Part III 1 User post a query DB1 5 XML data internet CompareXML 3.1 3.3 2,3 check if need to CompareXML1 XML data XML data update CompareXML2 4 update price quote DB DB2 Product - CompareXML Product – CompareXML1 code Category On_hand Price code Category On_hand Price 111 Printer 10 600 111 Printer 20 800 abc Scanner 20 200 abc Scanner 20 150 def printer 10 800 defg Scanner 50 600 supplying Product code Supplier code price On_han d Quote 111 t1 500 30 12/11 def t2 300 50 12/10 1 2 5 supplier code name URL t1 company1 http://sparc68.cse.cuhk.edu.hk8080 /example/servlet/CompareXML1 t2 company2 http://sparc68.cse.cuhk.edu.hk8080 /example/servlet/CompareXML2 4 Get the URL and when the quote is updated 3 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 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 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) 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(); 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) XML for exchanging data Tag <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> Name Content nanoxml • How to interpret the XML return from other URL – Use XML parser – http://nanoxml.sourceforge.net/index.html version 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 or set 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() DOM Tree and XMLElement All nodes are XMLElement object products getTagName() returns the node’s name product id description getChildren() returns the child nodes contained in a Vector object category company price name getContents(), e.g. when call on price XMLElement it will return 150.3 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 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) 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) 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> 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> 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” 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 author (period) name nationality authors/*/name authors/author/* authors/author[nationality=‘Russian’]/name authors/author/[@period=“classical”] IE Setting Prompt or Enable 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/