![]() |
![]() |
Here's
your Final Exam:
Solve them and post the working versions and the source code
under protected
.
Post the answers on your web sites by Fri Dec 14, 7pm.
Please include comments in your code or annotate the code as we do in lecture notes.
Group Project Information
If you have a project please come and turn it in in person.
Same procedure about making an appointment.
Please note:
For completeness I will provide the remaining of the shopping cart and all of the notes for the Java networked games on the web site next week. Use them if you need them but I am afraid they will be posted too late for you to use them to build anything that would count towards your grade this semester. But you could use them regardless.
For grading purposes it's either your project or Lab Fourteen now.
Today in class we will go over JSP and also present a brief XML tutorial.
Tutorial first. XML is a data description language. Here's an example:
Let's see a picture of this:<?xml version="1.0" standalone="yes"?> <document> <customer> <name> <last_name>Bird</last_name> <first_name>Larry</first_name> </name> <date>October 15, 2001</date> <orders> <item> <product>Tomatoes</product> <number>8</number> <price>$1.25</price> </item> <item> <product>Oranges</product> <number>24</number> <price>$4.98</price> </item> </orders> </customer> <customer> <name> <last_name>Jordan</last_name> <first_name>Michael</first_name> </name> <date>October 20, 2001</date> <orders> <item> <product>Bread</product> <number>12</number> <price>$14.95</price> </item> <item> <product>Apples</product> <number>6</number> <price>$1.50</price> </item> </orders> </customer> <customer> <name> <last_name>Kukoc</last_name> <first_name>Tony</first_name> </name> <date>October 25, 2001</date> <orders> <item> <product>Asparagus</product> <number>12</number> <price>$2.95</price> </item> <item> <product>Lettuce</product> <number>6</number> <price>$11.50</price> </item> </orders> </customer> </document>
What's in black represents a syntactic category.
What's in red is a terminal, a literal value.
In blue we have unexpanded categories (lack of space).
So this is a partial picture of this.
How do we access this structure?
I call this file customer.xml
I place it in /u/dgerman/practice
.
I also create a file One.java
.
Let's go through it step by step.
We first import the classes that we need. These classes support the W3C DOM interfaces such asburrowww.cs.indiana.edu% cat One.java import org.w3c.dom.*; import org.apache.xerces.parsers.DOMParser; public class One { public static void main(String[] args) { } } burrowww.cs.indiana.edu% javac One.java burrowww.cs.indiana.edu%
Node
and Element
.
We also need the class that implements the DOM parser.
To parse an XML document we need a DOMParser object, a parser, which I will callburrowww.cs.indiana.edu% cat One.java import org.w3c.dom.*; import org.apache.xerces.parsers.DOMParser; public class One { public static void main(String[] args) { DOMParser parser = new DOMParser(); } } burrowww.cs.indiana.edu% javac One.java burrowww.cs.indiana.edu%
parser
. DOMParser is derived from XMLParser, which in turn is based on the Object class.
To actually parse an XML document you use the parse method of the parser object.burrowww.cs.indiana.edu% cat One.java import org.w3c.dom.*; import org.apache.xerces.parsers.DOMParser; public class One { public static void main(String[] args) { DOMParser parser = new DOMParser(); try { parser.parse(args[0]); } catch (Exception e) { e.printStackTrace(System.err); } } } burrowww.cs.indiana.edu% javac One.java burrowww.cs.indiana.edu% java One customer.xml burrowww.cs.indiana.edu%
Check the documentation for the parse method now. It is in XMLParser and overloaded. We use the one that starts from a String (called a System ID). I will let the user specify the name of the document to parse on the command line by parsing args[0].
Note that you don't need to pass the name of a local file to the parse method - you can pass the URL of a document on the Internet, and the parse method wil retrive that document.
That document could be a file or the output of a script, it doesn't matter.
Since parse throws exceptions we try to run it and catch the exceptions if any.
If the document is successfully parsed you can get a Document object based on the W3C DOM, corresponding to the parsed document, using the parser's getDocument method.burrowww.cs.indiana.edu% cat One.java import org.w3c.dom.*; import org.apache.xerces.parsers.DOMParser; public class One { public static void main(String[] args) { DOMParser parser = new DOMParser(); try { parser.parse(args[0]); Document doc = parser.getDocument(); } catch (Exception e) { e.printStackTrace(System.err); } } } burrowww.cs.indiana.edu% javac One.java burrowww.cs.indiana.edu% java One customer.xml burrowww.cs.indiana.edu%
The Document interface is described on-line. Note that parse returns an object that belongs to a class that implements Document.
The Document interface is based on the Node interface which supports the W3C Node object.
A Node represents a single node in the document tree.
The Node interface has many methods that you can use to work with nodes.
One can use
One can also use
A Node object also has data members that hold constant values corresponding to the various node types in the W3C DOM.
The types of nodes we'll look at are mentioned in the indenting parser of last time.
At this point we have access to the root node of the document.burrowww.cs.indiana.edu% cat One.java import org.w3c.dom.*; import org.apache.xerces.parsers.DOMParser; public class One { public static void main(String[] args) { DOMParser parser = new DOMParser(); try { parser.parse(args[0]); Document doc = parser.getDocument(); NodeList nodeList = doc.getElementsByTagName(args[1]); } catch (Exception e) { e.printStackTrace(System.err); } } } burrowww.cs.indiana.edu% javac One.java burrowww.cs.indiana.edu% java One customer.xml CUSTOMER burrowww.cs.indiana.edu%
Our goal is to check how many CUSTOMER elements the document has so we use the
getElementsByTagName (from Document)method to get a
NodeListobject containing a list of all CUSTOMER elements.
Note that the process is case-sensitive.burrowww.cs.indiana.edu% cat One.java import org.w3c.dom.*; import org.apache.xerces.parsers.DOMParser; public class One { public static void main(String[] args) { DOMParser parser = new DOMParser(); try { parser.parse(args[0]); Document doc = parser.getDocument(); NodeList nodeList = doc.getElementsByTagName(args[1]); int len = nodeList.getLength(); System.out.println(args[0] + " has " + len + " " + args[1] + " elements."); } catch (Exception e) { e.printStackTrace(System.err); } } } burrowww.cs.indiana.edu% javac One.java burrowww.cs.indiana.edu% java One customer.xml CUSTOMER customer.xml has 0 CUSTOMER elements. burrowww.cs.indiana.edu% java One customer.xml customer customer.xml has 3 customer elements. burrowww.cs.indiana.edu%
NodeList also has an API, and we use
getLength (returning an int)from its API to find out how many CUSTOMER elements we have in the document.
Let's now review the type of nodes we could have.
At the beginning of the document is the XML declaration, and the type of this node matches the constant Node.DOCUMENT_NODE defined in the Node interface. To get the document element of the document we're parsing (the root element) we call the getDocumentElement method. To handle nodes of type Node.ELEMENT_NODE we simply report the element's name.
If an element node has attributes we can run the getAttributes() method to obtain a NodeList that holds all the attributes, stored as Attr objects.
Because the Attr interface is built on the Node interface you can can use either the getNodeName and getNodeValue methods to get to the attribute's name and value.
To get to the child elements of an element node you call getChildNodes.
Handling text nodes we can use getNodeValue().
We also need to take care of closing tags for element nodes.
import org.w3c.dom.*; import org.apache.xerces.parsers.DOMParser; public class One { public static void main(String[] args) { DOMParser parser = new DOMParser(); try { parser.parse("customer.xml"); Document doc = parser.getDocument(); Node one = doc.getDocumentElement(); System.out.println(one.getNodeName()); NodeList nodeList = one.getChildNodes(); Node two = nodeList.item(1); System.out.println(two.getNodeName()); } catch (Exception e) { e.printStackTrace(System.err); } } }