Pb de SAX

d12ahm Messages postés 14 Date d'inscription dimanche 23 décembre 2007 Statut Membre Dernière intervention 26 septembre 2009 - 20 juil. 2009 à 21:48
cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 - 20 juil. 2009 à 22:24
bonjour a t
je suis debutant sur java
j'crée un parser saxpour lire un fichier xml sous jbuilder9
voila le code:

//**************************************************************
package com.borland.samples.xml.saxparser;
import java.io.IOException;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
public class MySaxParser extends DefaultHandler {
private static int INDENT = 2;
private static String attList = "" ;
public static void main(String[] argv) {
if (argv.length != 1) {
System.out.println("Usage: java MySaxParser [URI]");
System.exit(0);
}
System.setProperty("javax.xml.parsers.SAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl");
String uri = argv[0];
try {
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
parserFactory.setValidating(false);
parserFactory.setNamespaceAware(false);
MySaxParser MySaxParserInstance = new MySaxParser();
SAXParser parser = parserFactory.newSAXParser();
parser.parse(uri, MySaxParserInstance);
}
catch(IOException ex) {
ex.printStackTrace();
}
catch(SAXException ex) {
ex.printStackTrace();
}
catch(ParserConfigurationException ex) {
ex.printStackTrace();
}
catch(FactoryConfigurationError ex) {
ex.printStackTrace();
}
}
private int idx = 0;
public void characters(char[] ch, int start, int length) throws SAXException {
String s = new String(ch, start, length);
if (!s.startsWith("\n"))
System.out.println(getIndent() + " Value: " + s);
}
public void endDocument() throws SAXException {
idx -= INDENT;
System.out.println(getIndent() + "end document");
System.out.println("...PARSING ends");
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (!attList.equals(""))
System.out.println(getIndent() + " Attributes: " + attList);
attList = "";
System.out.println(getIndent() + "end element");
idx -= INDENT;
}
public void startDocument() throws SAXException {
idx += INDENT;
System.out.println("PARSING begins...");
System.out.println(getIndent() + "start document: ");
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
idx += INDENT;
System.out.println('\n' + getIndent() + "start element: " + qName);
if (attributes.getLength() > 0) {
idx += INDENT;
for (int i = 0; i < attributes.getLength(); i++){
attList attList + attributes.getQName(i) + " " +
attributes.getValue(i);
if (i < (attributes.getLength() - 1))
attList = attList + ", ";
}
idx -= INDENT;
}
}
private String getIndent() {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < idx; i++)
sb.append(" ");
return sb.toString();
}
}
//************************************************************
voila le fichier xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE XmlEmployees SYSTEM "Employees.dtd">
<XmlEmployees>
<XmlEmployee>
2
<FirstName>Robert</FirstName>
<LastName>Nelson</LastName>
250


<HireDate>1988-12-28</HireDate>
<DeptNo>600</DeptNo>
<JobCode>VP</JobCode>
<JobGrade>2</JobGrade>
<JobCountry>USA</JobCountry>
<Salary>105900.000000</Salary>
<FullName>Nelson, Robert</FullName>
</XmlEmployee>
<XmlEmployee>
4
<FirstName>Bruce</FirstName>
<LastName>Young</LastName>
233


<HireDate>1988-12-28</HireDate>
<DeptNo>621</DeptNo>
<JobCode>CEO</JobCode>
<JobGrade>2</JobGrade>
<JobCountry>Eng</JobCountry>
<Salary>97500.000000</Salary>
<FullName>Young, Bruce</FullName>
</XmlEmployee>
</XmlEmployees>
[size=200]mais j'ai les erreurs suivant
/size
PARSING begins...
start document:
org.xml.sax.SAXParseException: Content is not allowed in prolog.
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:345)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:223)
at com.borland.samples.xml.saxparser.MySaxParser.main(MySaxParser.java:22)

1 réponse

cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
20 juil. 2009 à 22:24
Salut,

Si tu débuttes, je te conseille vivement d'utiliser un parseur DOM au lieu d'un parseur SAX : ca te sera bien plus simple à manipuler. SAX est à utiliser si tu as de très gros fichiers xml et que tu n'as pas besoin de lire tout le fichier, dans les autres cas, il vaut mieux un parseur DOM qui se charge tout l'arbre en mémoire : c'est bien plus simple d'accèder à un noeud de l'arbre plutôt que d'utiliser les événements SAX et devoir tout reconstruire ton fichier xml à la main !
______________________________________
DarK Sidious
0
Rejoignez-nous