Problème de lecture xml à la volée
Bonjour, et avant tous BONNE ANNEE !!!
je suis novice en Sax, et j'ai essayé d'adapter un exemple de code que j'ai trouvé sur ce forum. Cependant j'ai une erreur qui se produit et je voulais savoir si par hasard cela n'aurait pas un lien avec le fait qu'il faut préciser l'encodage xml..
En gros voici le principe de mon appli :
- J'ai un serveur qui envoie en continu des coordonnées de points de la forme suivante :
Code:
<SCENE><Ent><Pt x="701" y="485" /><Pt x="761" y="485" /><Pt x="761" y="545" /><Pt x="701" y="545" /></Ent></SCENE>...
- A partir d'un client je me connecte à ce serveur et je lis en continu le flux.
Ce que je voudrais faire c'est récupérer simplement les coordonnées x et y transmise en continue...
Voici les bouts de codes que j'utilise, cependant dés la premiere lecture et le premier parsage, il y a un problème :
XmlParser
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
| import javax.xml.parsers.*;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import java.io.*;
public class XmlParser {
AnalyseSax handler;
public XmlParser(InputSource is) {
handler = new AnalyseSax();
try{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
XMLReader reader = saxParser.getXMLReader();
reader.setContentHandler(handler);
reader.setErrorHandler(handler);
reader.parse(is);
}
catch (Exception e){
System.err.println("Pb de SAX");
e.printStackTrace();
System.exit(2);
}
}
} |
AnalyseSax
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
| import javax.xml.parsers.*;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import java.io.*;
public class AnalyseSax extends DefaultHandler {
public void startElement (String namespaceURI, String localName,
String qName, Attributes atts) {
//System.out.println("element : "+namespaceURI+" - "+localName+" - "+qName);
for (int i=0; i<atts.getLength(); i++)
System.out.println(" attributs : "+atts.getURI(i)+" - "+
atts.getLocalName(i)+" - "+
atts.getQName(i)+" = "+
atts.getValue(i));
}
public void characters (char[] ch, int debut, int l) {
System.out.print(" texte : ");
for (int i=0; i<l; i++)
System.out.print(ch[debut+i]);
System.out.println();
}
public void warning (SAXParseException e) {
System.out.println("WARNING : " + e);
}
public void error (SAXParseException e) {
System.out.println("ERROR : " + e);
}
} |
ClientXml
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
| import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;
import org.xml.sax.InputSource;
/** Le processus client se connecte au site fourni dans la commande
* d'appel en premier argument et utilise le port distant 1200.
*/
public class ClientXML {
static final int port = 1200;
static final int sizeBuf = 150;
public static void main(String[] args) throws Exception {
Socket socket = new Socket(/*args[0]*/"localhost", port);
System.out.println("SOCKET = " + socket);
BufferedReader plec = new BufferedReader(
new InputStreamReader(socket.getInputStream())
);
PrintWriter pred = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
String str = "<COMMAND>\n<ENTITY_MAP_SIZE>\n<Width=1024 />\n<Height=768 />\n</ENTITY_MAP_SIZE>\n</COMMAND>\n";
char [] buf = new char [sizeBuf];
boolean getData = true;
XmlParser xp;
InputSource is = new InputSource("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
while (getData) {
try {
// pred.println(str); // envoi d'un message
is = new InputSource(new InputStreamReader(socket.getInputStream()));
xp = new XmlParser(is);
}
catch (SocketException e) {
System.out.println(e);
getData = false;
}
}
plec.close();
pred.close();
socket.close();
}
} |
Et voici l'erreur qui se produit.... :
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
| SOCKET = Socket[addr=localhost/127.0.0.1,port=1200,localport=2152]
attributs : - - x = 478
attributs : - - y = 99
attributs : - - x = 538
attributs : - - y = 99
attributs : - - x = 538
attributs : - - y = 159
attributs : - - x = 478
attributs : - - y = 159
Pb de SAX
org.xml.sax.SAXParseException: Content is not allowed in trailing section.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:236)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:215)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:386)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:316)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1438)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$TrailingMiscDispatcher.dispatch(XMLDocumentScannerImpl.java:1310)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:368)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:834)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1242)
at XmlParser.<init>(XmlParser.java:17)
at ClientXML.main(ClientXML.java:45) |
En fait je me demandais si ce n'était pas un problème d'encodage, car les premiers caractères de la ligne lu sur le serveur sont normaux et ensuite il y a pleins de petits carrés...???
Si quelqu'un a une idée... je vous remercie...