Advertisement
CR7CR7

APIResponce

May 20th, 2022
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. public class APIResponseParser {
  2.    
  3.      /**
  4.      * Parses the input text and returns a Book instance containing
  5.      * the parsed data.
  6.      * @param response text to be parsed
  7.      * @return Book instance containing parsed data
  8.      */
  9.      public static Book parse(String response) {
  10.         Book book = new Book();
  11.         String endRule = "<";
  12.        
  13.         String startRule = "<title>";      
  14.         String title = parse(response, startRule, endRule);
  15.         book.setTitle(title);
  16.        
  17.         // Your code
  18.         return book;
  19.      }
  20.      
  21.      // write overloaded parse method with the 3 parameters response, startRule, endRule
  22.      
  23.      public static void main(String[] args) {
  24.         String response = "<work>" +
  25.                                 "<id type=\"integer\">2361393</id>" +
  26.                                 "<books_count type=\"integer\">813</books_count>" +
  27.                                 "<ratings_count type=\"integer\">1,16,315</ratings_count>" +
  28.                                 "<text_reviews_count type=\"integer\">3439</text_reviews_count>" +
  29.                                 "<original_publication_year type=\"integer\">1854</original_publication_year>" +
  30.                                 "<original_publication_month type=\"integer\" nil=\"true\"/>" +
  31.                                 "<original_publication_day type=\"integer\" nil=\"true\"/>" +
  32.                                 "<average_rating>3.79</average_rating>" +
  33.                                 "<best_book type=\"Book\">" +
  34.                                     "<id type=\"integer\">16902</id>" +
  35.                                     "<title>Walden</title>" +
  36.                                     "<author>" +
  37.                                         "<id type=\"integer\">10264</id>" +
  38.                                         "<name>Henry David Thoreau</name>" +
  39.                                     "</author>" +
  40.                                     "<image_url>" +
  41.                                         "http://images.gr-assets.com/books/1465675526m/16902.jpg" +
  42.                                     "</image_url>" +
  43.                                     "<small_image_url>" +
  44.                                         "http://images.gr-assets.com/books/1465675526s/16902.jpg" +
  45.                                     "</small_image_url>" +
  46.                                 "</best_book>" +
  47.                             "</work>";
  48.        
  49.         APIResponseParser.parse(response);
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement