public void startElement(String nameSpaceURI, String localName, String rawName, Attributes attributs) throws SAXException { if(localName.equals("document")) { oitem=true; } if(oitem==true) { if(localName.equals("titre")) { otitre=true; } if(localName.equals("htmlpage")) { odescription=true; } } } public void endElement(String nameSpaceURI, String localName, String rawName) throws SAXException { if(localName.equals("document")) { try { String query = "select * from jos_content"; rs = s.executeQuery(query); while (rs.next()) { if(rs.getString("title").equals(titre) && rs.getString("introtext").equals(description)) { bool=false; System.out.println("pas d'insertion pour " + titre); break; } else { bool=true; } } } catch (SQLException e) { e.printStackTrace(); } if(bool) { try { //Récupère tous les flux PreparedStatement ps = c.prepareStatement("insert into jos_content (`title`,`introtext`) values(?,?)"); //Insert les différents champs dans la requête ps.setObject(1,titre ); ps.setObject(2,description ); ps.executeUpdate(); } catch (SQLException ex) { System.out.println("probleme SQL pour inserer la news"+ex); } catch (Exception e) { System.out.println("proble pour inserer la news"+e); } System.out.println("insertion ok pour " + titre); } //Remet les variables à null pour l'article suivant titre=""; description=""; } if(localName.equals("document")) { oitem=false; } if(localName.equals("titre")) { otitre=false; } if(localName.equals("htmlpage")) { odescription=false; } }
/** * Evenement recu a chaque fois que l'analyseur rencontre des caracteres (entre * deux balises). * @param ch les caracteres proprement dits. * @param start le rang du premier caractere a traiter effectivement. * @param end le rang du dernier caractere a traiter effectivement * @see org.xml.sax.ContentHandler#characters(char[], int, int) */ public void characters(char[] ch, int start, int end) throws SAXException { //Si on est dans un article if(oitem==true ) { if(otitre==true) { String data1=new String(ch, start, end); titre+=data1; } if(odescription==true) { String data2=new String(ch, start, end); description+=data2; } } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question