Question
please read carefully I want to learn how to do it not just the solution. the code at the bottom of the page please explain
please read carefully I want to learn how to do it not just the solution.
the code at the bottom of the page please explain each step and use java
The Problem
RSS (Really Simple Syndication) is an XML application for distributing web content that changes frequently. Many news-related sites, weblogs and other online publishers syndicate their content as an RSS Feed to whoever wants it. In this lab, you will write code that extracts information from an RSS (version 2.0) document loaded into an XMLTree object.
RSS 2.0 documents have the following format:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Title goes here Link goes here Description goes here Optional title goes here Optional description goes here Optional link goes here Optional publication date goes here Optional source goes here ... ... |
Note the following properties of RSS 2.0 XML documents:
- The children of the tag and of the tag can occur in any order; do not assume they will appear in the order above. Furthermore there can be other children of other types not listed above.
- , , and are required children of the tag, i.e., you should assume they are present. However, and may be blank, i.e., they may not have any text child.
- All the children of tag are optional, i.e., do not assume they are present; but, either or must be present. However, the and/or tags, even if present, may be blank, i.e., they may not have any text child.
- If a tag appears as a child of an tag, it must have a url attribute.
Setup
Follow these steps to set up a project for this lab.
- Create a new Eclipse project by copying ProjectTemplate. Name the new project RSSProcessing.
- Open the src folder of this project and then open (default package). As a starting point you can use any of the Java files. Rename it RSSProcessing and delete the other files from the project.
- Follow the link to RSSProcessing.java, select all the code on that page (click and hold the left mouse button at the start of the program and drag the mouse to the end of the program) and copy it to the clipboard (right-click the mouse on the selection and chooseCopyfrom the contextual pop-up menu), then come back to this page and continue with these instructions.
- Finally in Eclipse, open the RSSProcessing.java file; select all the code in the editor, right-click on it and selectPastefrom the contextual pop-up menu to replace the existing code with the code you copied in the previous step. Save your file.
Method
- 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) {...} |
- Review the main method skeleton and modify it to output thetitle,description, andlinkof the RSS channel. Each element in the output should be preceded by a descriptive label, e.g.,
3. Title: Yahoo! News - Latest News & Headlines
4. 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/.
- 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:
7. Title: Tropical Storm Leslie churns northward in Atlantic
8. Link: http://news.yahoo.com/storm-churns-northward-winds-buffeting-bermuda-144218080.html
- 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.
10. import components.simplereader.SimpleReader;
11. import components.simplereader.SimpleReader1L;
12. import components.simplewriter.SimpleWriter;
13. import components.simplewriter.SimpleWriter1L;
14. import components.xmltree.XMLTree;
15. import components.xmltree.XMLTree1;
16.
17. /**
18. * This program inputs an XML RSS (version 2.0) feed from a given URL and
19. * outputs various elements of the feed to the console.
20. *
21. * @author Put your name here
22. *
23. */
24. public final class RSSProcessing {
25.
26. /**
27. * Private constructor so this utility class cannot be instantiated.
28. */
29. private RSSProcessing() {
30. }
31.
32. /**
33. * Finds the first occurrence of the given tag among the children of the
34. * given {@code XMLTree} and return its index; returns -1 if not found.
35. *
36. * @param xml
37. * the {@code XMLTree} to search
38. * @param tag
39. * the tag to look for
40. * @return the index of the first child of the {@code XMLTree} matching the
41. * given tag or -1 if not found
42. * @requires [the label of the root of xml is a tag]
43. * @ensures
44. * getChildElement =
45. * [the index of the first child of the {@code XMLTree} matching the
46. * given tag or -1 if not found]
47. *
48. */
49. private static int getChildElement(XMLTree xml, String tag) {
50. assert xml != null : "Violation of: xml is not null";
51. assert tag != null : "Violation of: tag is not null";
52. assert xml.isTag() : "Violation of: the label root of xml is a tag";
53. /*
54. * TODO: #1 - fill in body
55. */
56.
57. }
58.
59. /**
60. * Processes one news item and outputs the title, or the description if the
61. * title is not present, and the link (if available) with appropriate
62. * labels.
63. *
64. * @param item
65. * the news item
66. * @param out
67. * the output stream
68. * @requires
69. * [the label of the root of item is an tag] and out.is_open
70. * @ensures
71. * out.content = #out.content * [the title (or description) and link]
72. */
73. private static void processItem(XMLTree item, SimpleWriter out) {
74. assert item != null : "Violation of: item is not null";
75. assert out != null : "Violation of: out is not null";
76. assert item.isTag() && item.label().equals("item") : ""
77. + "Violation of: the label root of item is an tag";
78. assert out.isOpen() : "Violation of: out.is_open";
79. /*
80. * TODO: #3 - fill in body
81. */
82.
83. }
84.
85. /**
86. * Main method.
87. *
88. * @param args
89. * the command line arguments; unused here
90. */
91. public static void main(String[] args) {
92. /*
93. * Open I/O streams.
94. */
95. SimpleReader in = new SimpleReader1L();
96. SimpleWriter out = new SimpleWriter1L();
97. /*
98. * Input the source URL.
99. */
100. out.print("Enter the URL of an RSS 2.0 news feed: ");
101. String url = in.nextLine();
102. /*
103. * Read XML input and initialize XMLTree. If input is not legal XML,
104. * this statement will fail.
105. */
106. XMLTree xml = new XMLTree1(url);
107. /*
108. * Extract element.
109. */
110. XMLTree channel = xml.child(0);
111. /*
112. * TODO: #2 - output title, link, and description
113. */
114.
115. /*
116. * TODO: #4 - for each item, output title (or description, if title is
117. * not available) and link (if available)
118. */
119.
120. /*
121. * Close I/O streams.
122. */
123. in.close();
124. out.close();
125. }
126.
127. }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started