import org.jdom.*;
import org.jdom.output.*;
public class testParser5
{
static String[][] matrice=new String[3][5];
static int i=0,j=0,ligne,td;
static Element racine = new Element("Table");
static org.jdom.Document document = new Document(racine);
public static void displayTree(Node n)
{
if (n instanceof Tag)
{
Tag t = (Tag)n;
if (t.getTagName().equals("TR"))
{
System.out.println("\n-------------------------------------------------------");
ligne++;
}
else if (t.getTagName().equals("TD"))
{
if(j>4){j=0;i++;}
matrice[i][j++]=t.toPlainTextString();
System.out.print(t.toPlainTextString() + "|");
td++;
}
}
if (n.getChildren() != null)
for (int i = 0; i < n.getChildren().size(); i++)
displayTree(n.getChildren().elementAt(i));
}
static void affiche()
{
try
{
//On utilise ici un affichage classique avec getPrettyFormat()
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
sortie.output(document, System.out);
}
catch (java.io.IOException e){}
}
static void enregistre(String fichier)
{
try
{
//On utilise ici un affichage classique avec getPrettyFormat()
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
//Remarquez qu'il suffit simplement de créer une instance de FileOutputStream
//avec en argument le nom du fichier pour effectuer la sérialisation.
sortie.output(document, new FileOutputStream(fichier));
}
catch (java.io.IOException e){}
}
public static void main(String[] args)
{
try
{
Parser parser = new Parser ("file:C:\\fichier.html");
NodeList list = new NodeList ();
NodeFilter filter = new TagNameFilter ("TABLE");
for (NodeIterator e = parser.elements (); e.hasMoreNodes ();)
{
e.nextNode().collectInto(list, filter);
}
for (int i = 0; i < list.size(); i++)
{
//System.out.println("=============================");
Node table = list.elementAt(i);
displayTree(table);
}
int colonne=td/ligne;
for (int i = 0; i < ligne; i++)
{
for (int z = 0; z < colonne; z++){
System.out.print(" "+matrice[i][z]);
}
System.out.println();
}
for(int j=0;j<ligne-1;j++){
//Element racine = new Element("Racine");
Element etudiant = new Element("etudiant");
racine.addContent(etudiant);
for (int z = 0; z < colonne; z++){
Element nom = new Element(matrice[0][z]);
nom.setText(matrice[j+1][z]);
etudiant.addContent(nom);
}
}
affiche();
enregistre("out.xml");
}
catch(ParserException e)
{
e.printStackTrace();
}
}
}