Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.70 KB | None | 0 0
  1. System.out.println("xmlString = n" + xmlString);
  2. ===========================================================
  3. INFO: xmlString =
  4. <?xml version="1.0" encoding="UTF-8"?>
  5. <response>
  6. <lst name="responseHeader">
  7. <int name="status">0</int>
  8. <int name="QTime">2</int>
  9. <lst name="params">
  10. <str name="indent">true</str>
  11. <str name="q">source_t:ST</str>
  12. <str name="wt">xml</str>
  13. </lst>
  14. </lst>
  15. ...
  16.  
  17. try {
  18. System.out.println("Writing xmlString to xml file");
  19.  
  20. File file = new File("xmlString.xml");
  21.  
  22. // if file doesnt exists, then create it
  23. if (!file.exists()) {
  24. file.createNewFile();
  25. }
  26.  
  27. FileWriter fw = new FileWriter(file.getAbsoluteFile());
  28. BufferedWriter bw = new BufferedWriter(fw);
  29. bw.write(xmlString);
  30. bw.close();
  31.  
  32. System.out.println("Done writing file");
  33.  
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37.  
  38. INFO: Writing xmlString to xml file
  39. INFO: Done writing file
  40. SEVERE: java.net.MalformedURLException: no protocol: <?xml version="1.0" encoding="UTF-8"?>
  41.  
  42. public class SAXParserClass {
  43.  
  44. //Declare as public since it needs to be redefined for appending into ArrayList
  45. public static String row[] = new String[5]; //id, full_message_t, source_t, news_t, link_t
  46.  
  47. public ArrayList<String[]> XMLReader(String xmlString)
  48. {
  49. //Output: GlassFish Server. Test if String is passed in correctly
  50. System.out.println("xmlString = n" + xmlString);
  51. try {
  52. System.out.println("Writing xmlString to xml file");
  53.  
  54. File file = new File("xmlString.xml");
  55.  
  56. // if file doesnt exists, then create it
  57. if (!file.exists()) {
  58. file.createNewFile();
  59. }
  60.  
  61. FileWriter fw = new FileWriter(file.getAbsoluteFile());
  62. BufferedWriter bw = new BufferedWriter(fw);
  63. bw.write(xmlString);
  64. bw.close();
  65.  
  66. System.out.println("Done writing file");
  67.  
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. }
  71.  
  72. //Declaration
  73. final ArrayList<String[]> tableResult = new ArrayList<String[]>(); //Store ArrayList of row[]
  74.  
  75. try {
  76.  
  77. SAXParserFactory factory = SAXParserFactory.newInstance();
  78. SAXParser saxParser = factory.newSAXParser();
  79.  
  80. DefaultHandler handler = new DefaultHandler()
  81. {
  82. //When these are TRUE, extract the content
  83. boolean b_id = false;
  84. boolean b_full_message_t = false;
  85. boolean b_source_t = false;
  86. boolean b_news_t = false;
  87. boolean b_link_t = false;
  88.  
  89. //############################################################################################
  90. //Look for <start> tags
  91. public void startElement(String uri, String localName,String qName,
  92. Attributes attributes) throws SAXException {
  93.  
  94. System.out.println("Start Element :" + qName);
  95.  
  96. //Only interested in <str>
  97. if (qName.equalsIgnoreCase("str")) {
  98. String name = attributes.getValue("name");
  99.  
  100. //Check for name="id"
  101. if(name.equalsIgnoreCase("id")){
  102. b_id = true;
  103. }
  104. //Check for name="full_message_t"
  105. else if(name.equalsIgnoreCase("full_message_t")){
  106. b_full_message_t = true;
  107. }
  108. else if(name.equalsIgnoreCase("source_t")){
  109. b_source_t = true;
  110. }
  111. else if(name.equalsIgnoreCase("news_t")){
  112. b_news_t= true;
  113. }
  114. else if(name.equalsIgnoreCase("link_t")){
  115. b_link_t = true;
  116. }
  117. }//end if
  118.  
  119. }//end startElement
  120.  
  121. //############################################################################################
  122. //Look for <end> tags
  123. public void endElement(String uri, String localName,
  124. String qName) throws SAXException {
  125.  
  126. System.out.println("End Element :" + qName);
  127.  
  128. //When reach </doc>, row Array is complete. Add into ArrayList
  129. if (qName.equalsIgnoreCase("doc")) {
  130. System.out.println("Push row into Arraylist : " + row[0]);
  131. tableResult.add(row);
  132. row = new String[5]; //reinitialize String[]
  133. }
  134. }//end endElement
  135.  
  136. //############################################################################################
  137. //Get the content
  138. public void characters(char ch[], int start, int length) throws SAXException {
  139.  
  140. if (b_id) {
  141. //System.out.println("id : " + new String(ch, start, length));
  142. row[0] = new String(ch, start, length);
  143. System.out.println("The id is " + row[0]);
  144. b_id = false;
  145. }
  146. else if (b_full_message_t) {
  147. //System.out.println("fullmsg : " + new String(ch, start, length));
  148. row[1] = new String(ch, start, length);
  149. System.out.println("The fullmsg is " + row[1]);
  150. b_full_message_t = false;
  151. }
  152. else if (b_source_t) {
  153. //System.out.println("fullmsg : " + new String(ch, start, length));
  154. row[2] = new String(ch, start, length);
  155. System.out.println("The source is " + row[2]);
  156. b_source_t = false;
  157. }
  158. else if (b_news_t) {
  159. //System.out.println("fullmsg : " + new String(ch, start, length));
  160. row[3] = new String(ch, start, length);
  161. System.out.println("The news is " + row[3]);
  162. b_news_t = false;
  163. }
  164. else if (b_link_t) {
  165. //System.out.println("fullmsg : " + new String(ch, start, length));
  166. row[4] = new String(ch, start, length);
  167. System.out.println("The link is " + row[4]);
  168. b_link_t = false;
  169. }
  170.  
  171. }//end characters
  172.  
  173. };//end DefaultHandler
  174.  
  175. //############################################################################################
  176. //Read the String
  177. //saxParser.parse("solrTest.xml", handler);
  178. //File file = new File("solrTest.xml");
  179. //InputStream inputStream= new FileInputStream(file);
  180. // Reader reader = new InputStreamReader(xmlString,"UTF-8");
  181. //
  182. // InputSource is = new InputSource(reader);
  183. // is.setEncoding("UTF-8");
  184.  
  185. saxParser.parse(xmlString, handler);
  186.  
  187. } catch (Exception e) {
  188. e.printStackTrace();
  189. }
  190.  
  191. //Test output Arraylist
  192. System.out.println("Test Result!!!");
  193.  
  194. for(String[] columns : tableResult){
  195. for(int i=0; i<columns.length; i++){
  196. System.out.println("Number = " + columns[i]);
  197. }
  198. }
  199.  
  200. //Return the ArrayList of String[]
  201. return tableResult;
  202. }//end XMLReader
  203.  
  204. }//end class
  205.  
  206. StringBuilder sb=new StringBuilder(xmlstring);
  207. String str=new String(sb);
  208.  
  209. String xmlString="<?xml version='1.0' encoding='UTF-8'?>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement