Bonjour,
On mar, 2005-04-05 at 12:09 +0200, Jean Luc COSSI wrote:
> Bonjour,
>
> Je suis à la recherche d'outils permettant la conversion de csv vers
> xml, avec xslt.
Ci joint la classe "CSVDocument" qui était publiée sur le site 4xt.org
(actuellement indisponible).
Il s'agit d'un parseur SAX 1.0 destiné à être utilisé par le processeur
XSLT "XT" de James Clark.
Son adaptation en parseur SAX 2.0 autonome ou à utiliser avec d'autres
processeurs XSLT est, à priori, triviale...
Cordialement,
Eric van der Vlist
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import com.jclark.xsl.sax.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.xml.sax.*;
import java.io.*;
public class CSVDocument implements ResultTreeFragment{
private String filename;
static public byte NEW_LINE = 0;
static public byte NEW_FIELD = 1;
static public byte NON_QUOTED = 2;
static public byte QUOTED = 3;
static public byte PERHAPS_NON_QUOTED = 4;
static public byte DOUBLE_QUOTED = 5;
public CSVDocument (String filename){
this.filename= filename;
}
public static ResultTreeFragment document(String filename){
ResultTreeFragment rtf = new CSVDocument(filename);
return rtf;
}
public void emit(DocumentHandler h) throws SAXException {
try {
AttributeListImpl attributes = new AttributeListImpl();
BufferedReader br = new BufferedReader(new FileReader(filename));
h.startElement("table", attributes);
int row = 0, col = 0, state = NEW_LINE ;
StringBuffer sb = new StringBuffer();
String buf;
char ch[];
int n;
while ((buf = br.readLine()) != null) {
for (int i = 0; i < buf.length(); i++) {
if (state == NEW_LINE ) {
attributes.clear();
attributes.addAttribute("pos", "CDATA", Integer.toString(row));
h.startElement("tr", attributes);
row ++;
col = 0;
state = NEW_FIELD;
}
if (state == NEW_FIELD ) {
attributes.clear();
attributes.addAttribute("pos", "CDATA", Integer.toString(col));
h.startElement("td", attributes);
col ++;
state = NON_QUOTED;
}
if (state == DOUBLE_QUOTED )
state=QUOTED;
if (state == PERHAPS_NON_QUOTED ) {
if (buf.charAt(i) == '"') {
sb.append('"');
state = DOUBLE_QUOTED;
} else {
state = NON_QUOTED;
}
}
if (state == NON_QUOTED ) {
if (buf.charAt(i) == ';') {
state = NEW_FIELD;
} else if (buf.charAt(i) == '"') {
state = QUOTED;
} else {
sb.append(buf.charAt(i));
}
if (state == NEW_FIELD ) {
if (sb.length()>0){
ch = new char[sb.length()];
sb.getChars(0, sb.length(), ch, 0);
h.characters(ch, 0, sb.length());
sb = new StringBuffer();
}
h.endElement("td");
}
} else if (state == QUOTED ) {
if (buf.charAt(i) == '"') {
state = PERHAPS_NON_QUOTED;
} else {
sb.append(buf.charAt(i));
}
}
}
if (state == NON_QUOTED || state == NEW_FIELD) {
if (sb.length()>0){
ch = new char[sb.length()];
sb.getChars(0, sb.length(), ch, 0);
h.characters(ch, 0, sb.length());
sb = new StringBuffer();
}
if (state == NON_QUOTED)
h.endElement("td");
h.endElement("tr");
state=NEW_LINE;
} else if (state == QUOTED) {
sb.append("\n");
}
}
br.close();
h.endElement("table");
} catch (Exception e) {
e.printStackTrace();
throw new SAXException(e.toString());
}
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> Cordialement,
--
Le premier annuaire des apiculteurs 100% XML!
http://apiculteurs.info/
------------------------------------------------------------------------
Eric van der Vlist http://xmlfr.org http://dyomedea.com
(ISO) RELAX NG ISBN:0-596-00421-4 http://oreilly.com/catalog/relax
(W3C) XML Schema ISBN:0-596-00252-1 http://oreilly.com/catalog/xmlschema
------------------------------------------------------------------------
--
Devenez redacteur <XML>fr et contribuez au developpement du
xml francophone (http://xmlfr.org/infos/redacteurs/) !
Liste de diffusion "xml-tech@xmlfr.org" (http://xmlfr.org).
Cette liste est a votre disposition pour discuter en francais de
tout sujet technique lie a XML.
Pour resilier votre abonnement, envoyez un message contenant
la commande "unsubscribe" a xml-tech-request@xmlfr.org
(mailto:xml-tech-request@xmlfr.org?Subject=unsubscribe)
Received on Tue Apr 5 12:36:32 2005