Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.06 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6. import java.sql.CallableStatement;
  7. import java.sql.Connection;
  8. import java.util.ArrayList;
  9. import java.util.Iterator;
  10. import java.util.List;
  11.  
  12. import javax.annotation.Resource;
  13. import javax.sql.DataSource;
  14. import javax.xml.parsers.DocumentBuilder;
  15. import javax.xml.parsers.DocumentBuilderFactory;
  16. import javax.xml.parsers.ParserConfigurationException;
  17.  
  18. import org.w3c.dom.Document;
  19. import org.w3c.dom.Element;
  20. import org.w3c.dom.Node;
  21. import org.w3c.dom.NodeList;
  22. import org.xml.sax.SAXException;
  23.  
  24. import com.google.gson.JsonObject;
  25.  
  26. import java.sql.*;
  27.  
  28. import java.io.IOException;
  29. import java.util.ArrayList;
  30. import java.util.Iterator;
  31. import java.util.List;
  32.  
  33. import javax.xml.parsers.ParserConfigurationException;
  34. import javax.xml.parsers.SAXParser;
  35. import javax.xml.parsers.SAXParserFactory;
  36.  
  37. import org.xml.sax.Attributes;
  38. import org.xml.sax.SAXException;
  39.  
  40. import org.xml.sax.helpers.DefaultHandler;
  41.  
  42. public class DomXmlParser {
  43. List<Movie> myMov;
  44. List<Star> myStar;
  45. Document domMov;
  46. Document domStar;
  47.  
  48. public DomXmlParser() {
  49. myMov = new ArrayList<>();
  50. myStar = new ArrayList<>();
  51. }
  52.  
  53. private void parseXmlFile() {
  54. //get the factory
  55. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  56.  
  57. try {
  58.  
  59. //Using factory get an instance of document builder
  60. DocumentBuilder db = dbf.newDocumentBuilder();
  61.  
  62. //parse using builder to get DOM representation of the XML file
  63. domMov = db.parse("mains243.xml");
  64. domStar = db.parse("actors63.xml");
  65. }
  66. catch (ParserConfigurationException pce) {
  67. pce.printStackTrace();
  68. } catch (SAXException se) {
  69. se.printStackTrace();
  70. } catch (IOException ioe) {
  71. ioe.printStackTrace();
  72. }
  73. }
  74.  
  75.  
  76. private void parseDocument() throws FileNotFoundException {
  77.  
  78. PrintWriter writer = new PrintWriter(new File("outputMovie.txt"));
  79.  
  80. //get the root elememt
  81. Element docEle = domMov.getDocumentElement();
  82.  
  83. //get a nodelist of <employee> elements
  84. // <film> tag contains all the movies.
  85. NodeList nl = docEle.getElementsByTagName("film");
  86. if (nl != null && nl.getLength() > 0) {
  87. for (int i = 0; i < nl.getLength(); i++) {
  88.  
  89. //get the directorfilms element
  90. Element el = (Element) nl.item(i);
  91.  
  92. //get the Employee object
  93. Movie e = getMovie(el);
  94.  
  95. //add it to list
  96. myMov.add(e);
  97. writer.write(e.toString());
  98. }
  99. }
  100.  
  101. System.out.println("No of Movies = " + myMov.size());
  102. System.out.println("writing to file is done");
  103.  
  104. //////////////////////////////////////////////////////////////////////////////////////////////////
  105. PrintWriter writerStar = new PrintWriter(new File("outputStar.txt"));
  106.  
  107. Element docEleStar = domStar.getDocumentElement();
  108. NodeList nlStar = docEleStar.getElementsByTagName("actor");
  109. if (nlStar != null && nlStar.getLength() > 0) {
  110. for (int i = 0; i < nlStar.getLength(); i++) {
  111.  
  112. //get the directorfilms element
  113. Element elStar = (Element) nlStar.item(i);
  114.  
  115. //get the Employee object
  116. Star e = getStar(elStar);
  117.  
  118. //add it to list
  119. myStar.add(e);
  120. writerStar.write(e.toString());
  121. }
  122. }
  123.  
  124. System.out.println("No of Star = " + myStar.size());
  125. System.out.println("writing to file is done");
  126. }
  127. private Star getStar(Element el) {
  128. String name = getTextValue(el, "stagename");
  129. int year = getIntValue(el, "dob");
  130.  
  131. Star s = new Star(null, name, year);
  132. return s;
  133. }
  134.  
  135. private Movie getMovie(Element el) {
  136.  
  137. String id = getTextValue(el, "fid");
  138. String title = getTextValue(el, "t");
  139. int year = getIntValue(el, "year");
  140. String directorName = getTextValue(el, "dirn");
  141. String genre = getTextValue(el, "cat");
  142.  
  143. // System.out.println("id = " + id);
  144. // System.out.println("title = " + title);
  145. // System.out.println("year = " + year);
  146. // System.out.println("directorName = " + directorName);
  147.  
  148. //Create a new Employee with the value read from the xml nodes
  149. Movie e = new Movie(id, title, year, directorName, genre);
  150.  
  151. return e;
  152. }
  153.  
  154. private String getTextValue(Element ele, String tagName) {
  155. String textVal = "";
  156. NodeList nl = ele.getElementsByTagName(tagName);
  157. if (nl != null && nl.getLength() > 0) {
  158. Element el = (Element) nl.item(0);
  159. textVal = el.getTextContent();
  160. }
  161. if (textVal == null)
  162. textVal = "";
  163. return textVal;
  164. }
  165.  
  166. private int getIntValue(Element ele, String tagName) {
  167. int intVal = 0;
  168. boolean numeric = true;
  169. try {
  170. if (getTextValue(ele, tagName) != null || getTextValue(ele, tagName).length() != 0)
  171. intVal = Integer.parseInt(getTextValue(ele, tagName));
  172. }
  173. catch (NumberFormatException e) {
  174. numeric = false;
  175. }
  176.  
  177. if (!numeric)
  178. return 0;
  179.  
  180. return Integer.parseInt(getTextValue(ele, tagName));
  181.  
  182. }
  183.  
  184.  
  185. public void run() throws IOException {
  186. //parse the xml file and get the dom object
  187. parseXmlFile();
  188.  
  189. //get each employee element and create a Employee object
  190. parseDocument();
  191.  
  192. }
  193.  
  194. public static void main(String[] args) throws IOException {
  195. long startTime = System.nanoTime();
  196. System.out.println("Timer started: " + startTime);
  197.  
  198. DomXmlParser dp = new DomXmlParser();
  199. dp.run();
  200. System.out.println("Parser Finished");
  201. try {
  202.  
  203. Class.forName("com.mysql.jdbc.Driver").newInstance();
  204.  
  205. // Connect to the test database
  206. Connection connection = DriverManager.getConnection("jdbc:mysql:///moviedb?autoReconnect=true&useSSL=false",
  207. "mytestuser", "mypassword");
  208. String queryStar = "{CALL insertingStar(?, ?, ?)}";
  209. CallableStatement StarStatement = connection.prepareCall(queryStar);
  210. System.out.println("Please wait for stars to be inserted to the database!");
  211.  
  212. int j = 0;
  213. while (j < dp.myStar.size()) {
  214. StarStatement.setString(1, null);
  215. StarStatement.setString(2, dp.myStar.get(j).getName());
  216. StarStatement.setInt(3, dp.myStar.get(j).getBirthYear());
  217. StarStatement.executeQuery();
  218. j++;
  219. }
  220. System.out.println("Inserting to stars database is done!");
  221.  
  222. String query = "{CALL add_movie(?, ?, ?, ?, ?, ?, ?)}";
  223. CallableStatement queryStatement = connection.prepareCall(query);
  224. System.out.println("Please wait for movies to be inserted!");
  225. int i = 0;
  226. while (i < dp.myMov.size()) {
  227. queryStatement.setString(1, dp.myMov.get(i).getId());
  228. queryStatement.setString(2, dp.myMov.get(i).getTitle());
  229. queryStatement.setInt(3, dp.myMov.get(i).getYear());
  230. queryStatement.setString(4, dp.myMov.get(i).getDirector());
  231. queryStatement.setString(5, "");
  232. queryStatement.setString(6, "");
  233. queryStatement.setString(7, "");
  234.  
  235. queryStatement.executeQuery();
  236.  
  237. i++;
  238. }
  239. connection.close();
  240.  
  241. }
  242. catch (Exception e) {
  243. System.out.println(e.getMessage());
  244. }
  245. System.out.println("Inserting to movies database is done!");
  246. long endTime = System.nanoTime();
  247. System.out.println("Took "+(endTime - startTime) + " ns");
  248. }
  249.  
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement