Parser un XML pour produire un XHTML
Bonjour,
j'ai un souci je dois parser un fichier xml et recuperer des elements, cependant il faut que en sortie je dois faire un fichier xhtml, le souci j'arrive pas a inserer
Code:
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
|
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
<title>L'archipel des Comores</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Language" content="fr" />
<meta name="Description" content="Description de votre page" />
<link rel="stylesheet" media="screen" type="text/css" title="Design" href="design.css" />
</head> |
voici mon code java:
Code:
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
package exo1;
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import java.util.List;
import java.util.Iterator;
public class JDomErpine
{
static org.jdom.Document document;
static Element racine;
static void afficheALL()
{
PrintWriter file = null;
try {
file = new PrintWriter(new BufferedWriter(new FileWriter("erpin.html")));
} catch (IOException e) {
System.out.println("Error: Opening failed!!!\n");
// TODO Auto-generated catch block
e.printStackTrace();
}
List<?> listReference = racine.getChildren("reference");
List<?> listAuthor = racine.getChild("reference").getChildren("author");
List<?> listMolecule = racine.getChild("molecule-class").getChildren("molecule");
List<?> listAlignement = racine.getChild("molecule-class").getChildren("alignment");
List<?> listConsensus = racine.getChild("molecule-class").getChildren("consensus-molecule");
List<?> listHelix = racine.getChild("molecule-class").getChild("consensus-molecule").getChild("molecule").getChildren("structure");
Iterator<?> i = listReference.iterator();
Iterator<?> a = listAuthor.iterator();
Iterator<?> m = listMolecule.iterator();
Iterator<?> al = listAlignement.iterator();
Iterator<?> co = listConsensus.iterator();
Iterator<?> test = listHelix.iterator();
while(i.hasNext())
{
Element courant = (Element)i.next();
Reference ref = new Reference();
ref.title=courant.getChildText("title").toString().replace("\n "," ").trim();
ref.journal=courant.getChildText("journal");
ref.volume=courant.getChildText("volume");
ref.pages=courant.getChildText("pages");
ref.date=courant.getChild("date").getChildText("year");
file.write(ref.toString());
}
while(a.hasNext())
{
Element courant = (Element)a.next();
Personne pers = new Personne();
pers.name1=courant.getChild("person").getChildText("first-name");
pers.name2=courant.getChild("person").getChildText("last-name");
file.write(pers.toString());
}
/*
while(test.hasNext())
{
Element courant = (Element)test.next();
//mol.test=courant.getChild("str-annotation").getChild("helix");
}
*/
while(m.hasNext())
{
Molecule mol = new Molecule();
Element courant = (Element)m.next();
mol.id= courant.getAttributeValue("id");
mol.name=courant.getChild("identity").getChildText("name");
mol.seq=courant.getChild("sequence").getChildText("seq-data").replace("\n", "").trim();
mol.length=courant.getChild("sequence").getChildText("seq-data").replace("\n", "").trim().length();
file.write(mol.toString());
}
while(al.hasNext())
{
Alignement algn = new Alignement();
Element courant = (Element)al.next();
algn.seqali=courant.getChild("ali-sequence").getChildText("seq-data");
file.write(algn.toString());
}
while(co.hasNext())
{
Consensus cons=new Consensus();
Element courant = (Element)co.next();
cons.consensus=courant.getChild("molecule").getChild("sequence").getChildText("seq-data").replace("\n", "").trim();
file.write(cons.toString());
}
file.close();
}
public static void main(String[] args)
{
SAXBuilder sxb = new SAXBuilder();
try
{
document = sxb.build(new File("erpin.xml"));
}
catch(Exception e){
System.out.println("Error: Opening failed!!!\n");
}
racine = document.getRootElement();
afficheALL();
}
}
class Reference {
public String title;
public String journal;
public String volume;
public String pages;
public String date;
public String toString() {
return "<h1>"+title+"</h1><h2>Journal:"+journal+"</h2><h3>Volume:"+volume+"</h3><h4>Pages:"+pages+"</h4><h5>Date de parution:"+date+"</h5>";
//"</hr><center><table><br><u><b>Composition de chaque molecule:</b></u></br><tr><th>ID</th><th>Nom</th><th>Sequence</th></tr><tr><td>"+id+"</td><td>"+name+"</td><td>"+seq+"</td></tr></table></center><p><tr><td>Taille </td><td>"+length+"</td></tr></table></center><p><strong>Sequence alignee:</strong><ul><li>"+seqali+"</li></ul></p>";
}
}
class Personne{
public String name1;
public String name2;
public String toString() {
return "<h6>"+name1+" "+name2+"</h6>";
//"</hr><center><table><br><u><b>Composition de chaque molecule:</b></u></br><tr><th>ID</th><th>Nom</th><th>Sequence</th></tr><tr><td>"+id+"</td><td>"+name+"</td><td>"+seq+"</td></tr></table></center><p><tr><td>Taille </td><td>"+length+"</td></tr></table></center><p><strong>Sequence alignee:</strong><ul><li>"+seqali+"</li></ul></p>";
}
}
class Molecule {
public String id;
public String name;
public String seq;
public int length;
public String toString() {
return "<head><title>XML</title></hr><center><table><br><u><b>Composition de la molecule:</b></u></br><tr><th>ID</th><th>Nom</th><th>Sequence</th></tr><tr><td>"+id+"</td><td>"+name+"</td><td>"+seq+"</td></tr></table></center><p><tr><td>Taille </td><td>"+length+"</td></tr></table></center>";
}
}
class Alignement{
public String seqali;
public String toString() {
return "<p><strong>Sequence alignee:</strong><ul><li>"+seqali+"</li></ul></p>";
}
}
class Consensus{
public String consensus;
public String toString() {
return "<p><strong>Sequence consensus:</strong><ul><li>"+consensus+"</li></ul></p></body></html>";
}
} |