Lab: Recursion on XMLTree
Objective
In this lab you will practice recursion on XMLTrees by implementing
the tagCount and outputTextNodes static methods.
Setup
Follow these steps to set up a project for this lab.
- Create a new Eclipse project by copying
ProjectTemplate. Name the new projectXMLTreeRecursion. - Open the
srcfolder of this project and then open(default package). As a starting point you can use any of the Java files. Rename itXMLTreeRecursionand delete the other files from the project. - Follow the link to
XMLTreeRecursion.java, select all the code on that page and copy it to the clipboard; then open theXMLTreeRecursion.javafile in Eclipse and paste the code to replace the file contents. Save the file.
Method
-
Complete the body of the
tagCountstatic method specified as follows:/** * Returns the number of occurrences of the given tag in the given * {@code XMLTree}. * * @param xml * the {@code XMLTree} * @param tag * the tag name * @return the number of occurrences of the given tag in the given * {@code XMLTree} * @ensures <pre> * tagCount = * [the number of occurrences of the given tag in the given {@code XMLTree}] * </pre> */ private static int tagCount(XMLTree xml, String tag) {...} -
Run the program and test your implementation of
tagCount. For testing purposes you can use any XML URL or file. You may want to open it in the XMLTreeViewer application you used in an earlier lab to check the correctness of the output. (If you need some URL samples, click on the XML Input drop-down menu in the XMLTreeViewer application.) -
Complete the body of the
outputTextNodesstatic method specified as follows:/** * Outputs the text nodes in the given {@code XMLTree} on separate lines. * * @param xml * the {@code XMLTree} * @param out * the output stream * @updates out.content * @requires out.is_open * @ensures <pre> * out.content = #out.content * [the text nodes of xml on separate lines] * </pre> */ private static void outputTextNodes(XMLTree xml, SimpleWriter out) {...} -
Run the program and test your implementation of
outputTextNodes.
Additional Activities
-
Complete the body of the
outputXMLstatic method specified as follows:/** * Output the XML textual representation of the given {@code XMLTree}. * * @param xml * the {@code XMLTree} * @param out * the output stream * @param indentationLevel * the level of indentation * @updates out.content * @requires out.is_open and indentationLevel >= 0 * @ensures <pre> * out.content = #out.content * [the XML textual representation of xml] * </pre> */ private static void outputXML(XMLTree xml, SimpleWriter out, int indentationLevel) {...}Basically this method is intended to produce the same output as the
Stringreturned byXMLTree's methodtoString, but of course, you should use recursion and cannot usetoString. To complete this task you may want to implement a couple of "helper" methods,outputSpacesandoutputAttributes, (themselves not involving recursion) specified inXMLTreeRecursion.java. -
Run the program and test your implementation of
outputXML.