/** * Reads the written data from a specific database and a specific item. * * @param type * @param name * @throws IOException */ public Visual readWrittenData(String type, String name) throws IOException { String id; Document doc = null; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); // Decides from where you want to read. if (type.equals("series")) { doc = builder.parse("resources/series.xml"); } else if (type.equals("movies")) { doc = builder.parse("resources/movies.xml"); } NodeList personList = doc.getElementsByTagName(name); for (int i = 0; i < personList.getLength(); i++) { Node p = personList.item(i); if (p == null) { nodecheck = false; } if (p.getNodeType() == Node.ELEMENT_NODE) { Element movie = (Element) p; // Gets the attribute from the specified element. visual.setTitle(movie.getAttribute("title")); visual.setType(movie.getAttribute("type")); visual.setDate(movie.getAttribute("year")); visual.setLenght(movie.getAttribute("runtime")); visual.setGenre(movie.getAttribute("genre")); visual.setSynopsis(movie.getAttribute("plot")); visual.setLanguage(movie.getAttribute("language")); visual.setDirector(movie.getAttribute("director")); visual.setActors(movie.getAttribute("actors")); System.out.println("Archivo leido."); } } } catch (Exception ex) { System.out.println("Please, write a correct database name."); // If the database´s name isn´t correct, the method will launch again. readWrittenData(api.lookForType(), api.lookForName()); } return visual; }
/** Here is the main menu, where the user will interact. */ public void menu() { System.out.println("Insert the option number to execute it:"); System.out.println("-------------------------"); System.out.println("1 -Get data online and read it:"); System.out.println("2 -Get online data and write it into the local database:"); System.out.println("3 -Look for a specific record in the local database:"); while (check) { getOption(); } switch (str) { case "1": { Visual visual; visual = getDataToSee(api.transformXML()); if (checkNode()) { System.out.println(visual.toString()); System.out.println("-------------------------"); System.out.println("Want to save it? Y/N :"); str = sc.next(); if (str.equals("Y") || str.equals("y")) { System.out.println("Info saved."); System.out.println(""); submenu(); } else if (str.equals("N") || str.equals("n")) { submenu(); } } else { submenu(); } submenu(); } case "2": { getDataToSee(api.transformXML()); if (checkNode()) { System.out.println( "Received data: Now choose in what database you want it, and the name of the register:"); getDataToWrite(api.getDocu(), api.enterType(), api.enterName()); submenu(); } else { submenu(); } } case "3": { Visual visual = null; try { visual = readWrittenData(api.lookForType(), api.lookForName()); if (checkNode()) { System.out.println(visual.toString()); } else { System.out.println("Sorry, couldn´t find the data."); } } catch (IOException e) { System.out.println("Error. The file that you were trying to read doesn´t exist."); submenu(); } submenu(); } } }