Question: 1. Implement the following static method that, given an XMLTree and a tag name (a String), searches the children of the XMLTree for the given

1. Implement the following static method that, given an XMLTree and a tag name (a String), searches the children of the XMLTree for the given tag and returns the index of the first occurrence of the tag or -1 if the tag does not exist.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

/**

* Finds the first occurrence of the given tag among the children of the

* given {@code XMLTree} and return its index; returns -1 if not found.

*

* @param xml

* the {@code XMLTree} to search

* @param tag

* the tag to look for

* @return the index of the first child of the {@code XMLTree} matching the

* given tag or -1 if not found

* @requires [the label of the root of xml is a tag]

* @ensures

 

* getChildElement =

* [the index of the first child of the {@code XMLTree} matching the

* given tag or -1 if not found]

*

*/

private static int getChildElement(XMLTree xml, String tag) {...}

2. Review the main method skeleton and modify it to output the title, description, and link of the RSS channel. Each element in the output should be preceded by a descriptive label, e.g.,

Title: Yahoo! News - Latest News & Headlines Description: The latest news and headlines from Yahoo! News. Link: http://news.yahoo.com/ 

Run the program and test your implementation. As input you can use any URL of a valid RSS 2.0 feed, e.g., https://news.yahoo.com/rss/.

3. Once you are confident that your implementations above are correct, implement the following static method that, given an XMLTree whose root is an tag and an output stream, outputs the title (or the description, if the title is not available) and the link, if available.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

/**

* Processes one news item and outputs the title, or the description if the

* title is not present, and the link (if available) with appropriate

* labels.

*

* @param item

* the news item

* @param out

* the output stream

* @updates out.content

* @requires [the label of the root of item is an tag] and out.is_open

* @ensures out.content = #out.content * [the title (or description) and link]

*/

private static void processItem(XMLTree item, SimpleWriter out) {...}

Here is an example of what the output might look like:

Title: Tropical Storm Leslie churns northward in Atlantic Link: http://news.yahoo.com/storm-churns-northward-winds-buffeting-bermuda-144218080.html 

4. Back in the main method, add code so that it prints all items in the RSS channel by repeatedly calling processItem. Then run and test your code to make sure it works as intended.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!