Lab: XMLTree Methods
Objectives
The objective of this lab is to familiarize yourself with the XMLTree
methods and how they can be used to extract information from an
XMLTree object.
The Problem
For this lab, you will load an XMLTree object from an XML URL; then,
from this object, you will extract various pieces of information using
the XMLTree methods and you will output that information to the
console. Make sure you successfully and correctly complete each task
before moving on to the next.
Setup
Follow these steps to set up a project for this lab.
- Create a new Eclipse project by copying
ProjectTemplate. Name the new projectXMLTreeExploration. - Open the
srcfolder of this project and then open(default package). As a starting point you should useProgramWithIO.java. Rename itXMLTreeExplorationand delete the other files from the project. - In a browser window open the documentation for the
XMLTreecomponent . You will need to refer to it throughout the lab.
Method
Edit XMLTreeExploration.java to perform the following tasks. Note that
at the beginning you will hard code specific method calls to solve
specific tasks that rely on the input being the specific input you are
manipulating.
Getting Started: Constructing and Displaying an XMLTree
-
Declare an
XMLTreevariable, name it xml, and initialize it with an object ofXMLTree1type constructed from the URLhttps://cse22x1.engineering.osu.edu/2221/web-sw1/extras/instructions/xmltree-model/columbus-weather.xml:XMLTree xml = new XMLTree1("https://cse22x1.engineering.osu.edu/2221/web-sw1/" + "extras/instructions/xmltree-model/columbus-weather.xml"); -
Two instance methods in the
XMLTreecomponent are particularly useful when trying to visualize the value of anXMLTreeobject:toStringwhich produces a textual representation of theXMLTreeand can be used to output it to an output stream (e.g., a SimpleWriter) anddisplaywhich opens a new window and displays theXMLTreein a way similar to the XMLTree Viewer application you used in the last lab. See the documentation for the complete description of these methods. -
Modify your program so that it outputs the
XMLTreexml to the console using the open output stream and it also displays theXMLTreein a new window. -
Run your current program and compare the textual XML output with the tree representation in the displayed window. Once you have verified that the two views essentially provide the same information, you can comment out the statement that produces the textual output to the console (you won't need it for the rest of the lab).
The Root
- A node in an
XMLTreehas a label which can be either a tag or text. TwoXMLTreeinstance methods are useful in determining the nature of a node:isTagwhich tells us whether the root node of theXMLTreeis a tag or not (i.e., text) andlabelwhich returns the label (tag or text) of the root node of theXMLTree. - Modify your program so that, using these methods, it outputs whether
the root of the
XMLTreexml is a tag or text and the label of the root of theXMLTreexml. - Verify that the output matches your expectations by checking the
root node in the
XMLTreedisplay.
The Children
- Every tag node (a node whose label is a tag) in an
XMLTreecan have 0 or more children, i.e., subtrees that are alsoXMLTrees. TwoXMLTreeinstance methods are used to access the children of the root:numberOfChildrenreturns the number of children of the root node of theXMLTreeandchildreturns a child of the root node of theXMLTreegiven the index (starting at 0) of the child among all the children. - Modify the program as follows:
- Declare a new
XMLTreevariable, name it results, and initialize it to the results child of the root node of theXMLTreexml (should be the first child, but you need to verify that in the displayed tree). - Declare a new
XMLTreevariable, name it channel, and initialize it to the channel child of theXMLTreeresults (should be the first child, but you need to verify that in the displayed tree). - Output the number of children of the root of the
XMLTreechannel. - Then declare another
XMLTreevariable, name it title, and initialize it to the title child of the root node of the channelXMLTree(it should be the second child, but you need to verify that in the displayed tree). - Declare a fourth
XMLTreevariable, name it titleText, and initialize it to the text child of the root node of the titleXMLTree. - Finally, output the label of the titleText
XMLTree.
- Declare a new
- Now for a little challenge: can you achieve the same result of the
previous step with a single statement directly from
XMLTreexml? In other words, output the same string of characters with just one statement involving variable xml. (Note: this is not necessarily good programming practice, but it is worth knowing that you can do it.) Add the new statement; do not delete or comment out those you entered earlier. (If you are not sure how to do this, see Slide #50 in XMLTree Methods.)
The Attributes
- Every tag node (a node whose label is a tag) in an
XMLTreecan have 0 or more attributes, where each attribute has a name and a value. TwoXMLTreeinstance methods are used to access individual attributes of the root:hasAttributewhich, given aStringargument, returns whether the root node of theXMLTreehas an attribute with that name andattributeValuewhich, given aStringargument, returns the value of the given attribute of the root node of theXMLTree. - Modify the program as follows:
- Declare a new
XMLTreevariable, name it astronomy, and initialize it to the yweather:astronomy child of the root node of the channelXMLTree. - Using the
hasAttributemethod, output whether the root of the astronomyXMLTreehas an attribute named"sunset"and whether it has an attribute named"midday". - Output the values of attributes sunrise and sunset of the
root of the astronomy
XMLTree.
- Declare a new
One More Challenge
-
Implement the following static method that extracts the middle child of the given
XMLTreeand outputs to the given output stream- the middle child's label,
- whether the middle child's label is a tag or text, and
- if the middle child's label is a tag, its number of children
Use the
numberOfChildrenmethod to determine the index of the middle child./** * Output information about the middle child of the given {@code XMLTree}. * * @param xt * the {@code XMLTree} whose middle child is to be printed * @param out * the output stream * @updates out.content * @requires * [the label of the root of xt is a tag] and * [xt has at least one child] and out.is_open * @ensures * out.content = #out.content * [the label of the middle child * of xt, whether the root of the middle child is a tag or text, * and if it is a tag, the number of children of the middle child] */ private static void printMiddleNode(XMLTree xt, SimpleWriter out) {...} -
Modify the main program to output information about the middle child of the channel
XMLTreeby calling theprintMiddleNodestatic method. -
Verify in the displayed tree that the output is correct.
Additional Activities
One Last Challenge
-
Implement the following static method that outputs all the attributes (names and values) of the root of a given
XMLTree. For this task, you will need to use the last instance method provided by theXMLTreecomponent, namely,attributeNames. (If you are not sure how to do this, see Slides #52-60 in XMLTree Methods.)/** * Output all attributes names and values for the root of the given * {@code XMLTree}. * * @param xt * the {@code XMLTree} whose root's attributes are to be printed * @param out * the output stream * @updates out.content * @requires [the label of the root of xt is a tag] and out.is_open * @ensures * out.content = * #out.content * [the name and value of each attribute of the root of xt] */ private static void printRootAttributes(XMLTree xt, SimpleWriter out) {...} -
Modify the main program to output information about the forecast for February 2, 2017 as follows:
- Declare a new
XMLTreevariable, name it item, and initialize it to the item child of the root node of the channelXMLTree. - Declare another
XMLTreevariable, name it forecast, and initialize it to the yweather:forecast child of the root node of the itemXMLTreewith the weather forecast for February 2, 2017. - Call the
printRootAttributesstatic method to output the attributes of the root node of the forecastXMLTree.
- Declare a new
-
Verify in the displayed tree that the output is correct.