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 project XMLTreeExploration.
- Open the src folder of this project and then open
(default package). As a starting point you should use ProgramWithIO.java.
Rename it XMLTreeExploration and delete the other files
from the project.
- In a browser window open the documentation for the XMLTree
component (here is the direct
link). 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 XMLTree variable, name it xml,
and initialize it with an object of XMLTree1 type
constructed from the URL http://web.cse.ohio-state.edu/software/2221/web-sw1/extras/instructions/xmltree-model/columbus-weather.xml:
- Two instance methods in the XMLTree
component are particularly useful when trying to visualize the
value of an XMLTree object: toString
which produces a textual representation of the XMLTree
and can be used to output it to an output stream (e.g., a
SimpleWriter) and display which opens a new window
and displays the XMLTree in 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 XMLTree
xml to the console using the open output stream and it also
displays the XMLTree in 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 XMLTree has a label which can be
either a tag or text. Two XMLTree instance
methods are useful in determining the nature of a node: isTag
which tells us whether the root node of the XMLTree is a
tag or not (i.e., text) and label which returns
the label (tag or text) of the root node of the XMLTree.
- Modify your program so that, using these methods, it
outputs whether the root of the XMLTree xml is a
tag or text and the label of the root of the XMLTree xml.
- Verify that the output matches your expectations by
checking the root node in the XMLTree display.
The Children
- Every tag node (a node whose label is a tag) in an XMLTree
can have 0 or more children, i.e., subtrees that are also XMLTrees.
Two XMLTree instance methods are used to access the
children of the root: numberOfChildren returns the
number of children of the root node of the XMLTree and child
returns a child of the root node of the XMLTree given the
index (starting at 0) of the child among all the children.
- Modify the program as follows:
- Declare a new XMLTree variable, name it results,
and initialize it to the results child of the root node
of the XMLTree xml (should be the first child,
but you need to verify that in the displayed tree).
- Declare a new XMLTree variable, name it channel,
and initialize it to the channel child of the XMLTree
results (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 XMLTree
channel.
- Then declare another XMLTree variable, name it
title, and initialize it to the title child of
the root node of the channel XMLTree (it
should be the second child, but you need to verify that in the
displayed tree).
- Declare a fourth XMLTree variable, name it titleText,
and initialize it to the text child of the root node of the title
XMLTree.
- Finally, output the label of the titleText XMLTree.
- Now for a little challenge: can you achieve the same result
of the previous step with a single statement directly from
XMLTree xml? 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.)
The Attributes
- Every tag node (a node whose label is a tag) in an XMLTree
can have 0 or more attributes, where each attribute has a name and
a value. Two XMLTree instance methods are used to access
individual attributes of the root: hasAttribute
which, given a String argument, returns whether the root
node of the XMLTree has an attribute with that name and attributeValue
which, given a String argument, returns the value of the
given attribute of the root node of the XMLTree.
- Modify the program as follows:
- Declare a new XMLTree variable, name it astronomy,
and initialize it to the yweather:astronomy child of
the root node of the channel XMLTree.
- Using the hasAttribute method, output whether
the root of the astronomy XMLTree has 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.
One More Challenge
- Implement the following static method that extracts the
middle child of the given XMLTree and 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 numberOfChildren method to determine the index
of the middle child.
- Modify the main program to output information about the
middle child of the channel XMLTree by calling the
printMiddleNode static 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 the XMLTree component, namely, attributeNames.
(If you are not sure how to do this, see Slides
#52-60.)
- Modify the main program to output information about the
forecast for February 2, 2017 as follows:
- Declare a new XMLTree variable, name it item,
and initialize it to the item child of the root node of
the channel XMLTree.
- Declare another XMLTree variable, name it forecast,
and initialize it to the yweather:forecast child of the
root node of the item XMLTree with the weather
forecast for February 2, 2017.
- Call the printRootAttributes static method to
output the attributes of the root node of the forecast
XMLTree.
- Verify in the displayed tree that the output is correct.