Title: CSC3530 Software Technology
1CSC3530 Software Technology
- Tutorial 10
- Assignment Two (III) Demo
- Update to Assignment One
- java.net package
- nanoxml
- XSLT,XPath
2Assignment Two Part III
- Demo link
- http//sparc68.cse.cuhk.edu.hk8080/examples/servl
et/CompareXML - http//sparc68.cse.cuhk.edu.hk8080/examples/servl
et/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
3Flow 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
4Product - CompareXML
Product CompareXML1
code Category On_hand Price
111 Printer 20 800
abc Scanner 20 150
defg Scanner 50 600
code Category On_hand Price
111 Printer 10 600
abc Scanner 20 200
def printer 10 800
supplying
1
4
Product code Supplier code price On_hand Quote
111 t1 500 30 12/11
def t2 300 50 12/10
2
Get the URL and whenthe quote is updated
5
supplier
3
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
5Update 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
6Suggested SQL
- select SU.url, S.code from supplying SU, supplier
S where SU.supplier_codeS.code and
SU.product_codexxx and (SYSDATE-SU.quote)2460
gt 30 - Find those suppliers price quote URL which
supply product xxx to us and the price quote is
not update for 30 minutes - update supplying set price100, quoteSYSDATE
where supplier_codexxx and product_codeyyy - 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
7java.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)
8Code fragment
- Example
- http//www.cse.cuhk.edu.hk/kcsia/csc3530/Query.ja
va - Please run in unix machine, and make sure sparc68
is up - URL urlnew URL(http//sparc68.cse.cuhk.edu.hk80
80/examples/servlet/CompareXML) - URLConnection urlconnectionurl.openConnection()
- urlconnection.setDoOutput(true)
- PrintWriter outnew PrintWriter(urlconnection.getO
utputStream()) - out.print(fieldidquery111)
- out.close()
- BufferedReader innew BufferedReader(new
InputStreamReader(urlconnection.getInputStream())
- String temp
- do
- tempin.readLine()
- if (temp!null)
- System.out.println(temp)
- else
- break
-
9Explanation
- 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)
10XML for exchanging data
Tag Name
- ltproductsgt
- ltproductgt
- ltidgtABCDlt/idgt
- ltdescriptiongtrogue spearlt/descriptiongt
- ltcategorygtcomputer gameslt/categorygt
- ltcompanygt
- ltnamegtAmazonlt/namegt
- ltpricegt200.2lt/pricegt
- ltonhandgt50lt/onhandgt
- lt/companygt
- lt/productgt
- ltproductgt
- ltidgtEFGHlt/idgt
- ltdescriptiongtblack thornlt/descriptiongt
- ltcategorygtcomputer gameslt/categorygt
- ltcompanygt
- ltnamegtAmazonlt/namegt
- ltpricegt150.3lt/pricegt
- ltonhandgt200lt/onhandgt
Content
11nanoxml
- 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()
12DOM Tree and XMLElement
All nodes are XMLElement object
getTagName() returns the nodes name
getChildren() returns the childnodes contained
in a Vectorobject
getContents(), e.g. when call on price
XMLElementit will return 150.3
13How to use XML
- Sample http//www.cse.cuhk.edu.hk/kcsia/csc3530/
QueryXML.java -
- String xmlltproductsgtltproductgtltcompanygtltpricegt200
lt/pricegtlt/companygtlt/productgtlt/productsgt - XMLElement rootnew XMLElement()
- root.parseString(xml)
- XMLElement productfindTag(root,"product")
- if (product!null)
- XMLElement companyfindTag(product,"company")
- if (company!null)
- XMLElement pricefindTag(company,"price")
- if (price!null)
- System.out.println("The price
is price.getContents()) -
-
-
-
- // findTag is a function that you have written
14How to use XML
- private static XMLElement findTag(XMLElement
src,String str) - Vector v src.getChildren()
- for (int i 0 i lt 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)
15XSLT, XPath
- How to present the XML in a browser?
- Specify a XSL file in the XML
- lt?xmlstylesheet type"text/xsl"
href"http//www.cse.cuhk.edu.hk/kcsia/display.xs
l"?gt - XSL - eXtensible Stylesheet Language
- To transform XML document to HTML (mainly)
- XSL has two standard versions
- ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" version"1.0"gt - 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 - ltxslstylesheet xmlnsxsl"http//www.w3.org/TR/WD
-xsl"gt - Working draft (supported by IE 5.5)
16Sample XSL file
- ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" version"1.0"gt - ltxsltemplate match"/"gt
- ltHTMLgt
- ltHEADgt
- ltTITLEgtQuery Resultlt/TITLEgt
- lt/HEADgt
- ltBODYgt
- ltH1gtQuery Resultlt/H1gt
- ltxslfor-each select"/products/product"gt
- Product ID ltxslvalue-of select"id"/gtltBR/gt
- Product Code ltxslvalue-of select"description"/
gtltBR/gt - Product Category ltxslvalue-of
select"category"/gtltBR/gt - ltTABLE BORDER"1"gt
- ltTRgt
- ltTHgtcompany namelt/THgt
- ltTHgtpricelt/THgt
- ltTHgton handlt/THgt
- lt/TRgt
17Sample XSL file
- ltxslfor-each select"company"gt
- ltxslsort select"price" data-type"number"
order"ascending" /gt - ltTRgt
- ltTDgtltxslvalue-of select"name"/gtlt/TDgt
- ltTDgtltxslvalue-of select"price"/gtlt/TDgt
- ltTDgtltxslvalue-of select"onhand"/gtlt/TDgt
- lt/TRgt
- lt/xslfor-eachgt
- lt/TABLEgtltBR/gt
- lt/xslfor-eachgt
- lt/BODYgt
- lt/HTMLgt
- lt/xsltemplategt
- lt/xslstylesheetgt
18XPath
- 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 - ltxsltemplate match"/"gt
- Xsl will process the whole xml file, / means the
root - ltxslfor-each select"/products/product"gt
- 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 - ltxslsort select"id" data-type"text"
order"ascending" /gt - Sort the selected nodes according to the content
in child node id - Sort function is supported in XSLT version 1.0
- ltxslvalue-of select"name"/gt
- Display the value (content) in node named name
19XPath
- 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/authornationalityRussian/name
authors/author/_at_periodclassical
20IE Setting
Prompt or Enable
21Reference
- TopXML
- http//www.topxml.com/default.asp
- Servlet 2.1 Documentation
- http//java.sun.com/products/servlet/2.1/api/packa
ges.html - JDK1.3 Documentation
- http//java.sun.com/j2se/1.3/docs/api/index.html
- Java Tutorial
- http//java.sun.com/docs/books/tutorial/