1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| public class MainActivity extends AppCompatActivity {
String TableEPG = "epg";
long TSactuel = System.currentTimeMillis() / 1000L;
Hashtable ht = new Hashtable();
public String getListe(int x) {
return String.valueOf(ht.get(x));
}
public void setListe(int x, String val) {
ht.put(x, val);
}
public static String toTimestamp( String date ) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmm" );
java.util.Date TSdate = sdf.parse(date);
String NewDate = String.valueOf(TSdate.getTime() / 1000L);
return NewDate;
}
TextView texte;
Button traiterXML;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
texte = (TextView) findViewById(R.id.jsonDisplay);
traiterXML = (Button) findViewById(R.id.startParsing);
ReadAllSQL();
}
public void traiterXML(View v) {
CreateSQL();
main();
}
public void main() {
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("storage/sdcard0/test.xml");
try {
Document document = (Document) builder.build(xmlFile);
Element rootNode = document.getRootElement();
System.out.println("element Root : " + rootNode);
List listID = rootNode.getChildren("channel");
List listProgramme = rootNode.getChildren("programme");
Iterator i = listID.iterator();
while (i.hasNext()) {
Element courant = (Element) i.next();
String Chaine = courant.getChild("display-name").getText();
String id = courant.getAttributeValue("id");
setListe(Integer.parseInt(id), Chaine);
}
Iterator j = listProgramme.iterator();
while (j.hasNext()) {
Element courant = (Element) j.next();
String chaine = courant.getAttributeValue("channel");
chaine = getListe(Integer.parseInt(chaine));
String debut_ts = toTimestamp(courant.getAttributeValue("start"));
int debut = Integer.parseInt(debut_ts);
String fin_ts = toTimestamp(courant.getAttributeValue("stop"));
int fin = Integer.parseInt(fin_ts);
int duree = Integer.parseInt(courant.getChildText("length"));
String description = courant.getChildText("desc");
String programme = courant.getChildText("title");
String soustitre = courant.getChildText("sub-title");
String image = courant.getAttributeValue("src");
WriteSQL(chaine, debut, fin, duree, programme, soustitre, description, image);
}
} catch (IOException io) {
System.out.println(io.getMessage());
} catch (JDOMException jdomex) {
System.out.println(jdomex.getMessage());
} catch (ParseException e) {
e.printStackTrace();
}
}
public void CreateSQL () {
SQLiteDatabase myDB= null;
try {
myDB = this.openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);
myDB.execSQL("DROP TABLE IF EXISTS " + TableEPG);
myDB.execSQL("CREATE TABLE IF NOT EXISTS " + TableEPG + " (chaine VARCHAR, debut INTEGER, fin INTEGER, duree INTEGER, programme VARCHAR, soustitre VARCHAR, description TEXT, image VARCHAR);");
}
catch(Exception e) {
Log.e("Error", "Error", e);
} finally {
if (myDB != null)
myDB.close();
}
}
public void WriteSQL (String chaine, int debut, int fin, int duree, String programme, String soustitre, String description, String image) {
SQLiteDatabase myDB = null;
try {
myDB = this.openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);
if (chaine != null) { chaine = chaine.replaceAll("'", "''"); }
if (programme != null) { programme = programme.replaceAll("'", "''"); }
if (soustitre != null) { soustitre = soustitre.replaceAll("'", "''"); }
if (description != null) { description = description.replaceAll("'", "''"); }
myDB.execSQL("INSERT INTO " + TableEPG + " (chaine, debut, fin, duree, programme, soustitre, description, image)" + " VALUES ('" + chaine + "', " + debut + ", " + fin + ", " + duree + ", '" + programme + "', '" + soustitre + "', '" + description + "', '" + image + "');");
} catch (Exception e) {
Log.e("Error", "Error", e);
} finally {
if (myDB != null)
myDB.close();
}
}
public void ReadAllSQL () {
SQLiteDatabase myDB = null;
try {
myDB = this.openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);
Cursor c = myDB.rawQuery("SELECT * FROM " + TableEPG , null);
int Column1 = c.getColumnIndex("chaine");
int Column2 = c.getColumnIndex("debut");
int Column3 = c.getColumnIndex("fin");
int Column4 = c.getColumnIndex("duree");
int Column5 = c.getColumnIndex("programme");
int Column6 = c.getColumnIndex("soustitre");
int Column7 = c.getColumnIndex("description");
int Column8 = c.getColumnIndex("image");
String Data="";
c.moveToFirst();
if (c != null) {
do {
String xml_chaine = c.getString(Column1);
int xml_debut = c.getInt(Column2);
int xml_fin = c.getInt(Column3);
int xml_duree = c.getInt(Column4);
String xml_programme = c.getString(Column5);
String xml_soustitre = c.getString(Column6);
String xml_description = c.getString(Column7);
String xml_image = c.getString(Column8);
Data =Data +xml_chaine+" - "+xml_programme+"/"+xml_debut+" - "+xml_fin+"\n";
}while(c.moveToNext());
}
texte.setText(Data);
}
catch(Exception e) {
Log.e("Error", "Error", e);
} finally {
if (myDB != null)
myDB.close();
}
}
} |