user8143344
user8143344

Reputation:

Java XML parser executes each node twice

I try to parse an XML file with a class I found on the internet. Only problem is that it passes twice by each node. First time it return the real value and null on the second time. Which makes the final value null. Here is the code of the parser class:

public class MyXMLHandler extends DefaultHandler{
private String node = null;
int i = 0;
   int j = 0;
   int k = 0;
   boolean cont = true;

public void startDocument() throws SAXException {
    System.out.println("Début du parsing");
}

public void startElement(String namespaceURI, String lname, String qname, Attributes attrs) throws SAXException {
    System.out.println("---------------------------------------------");
    System.out.println("qname = " + qname);
    node = qname;

}

public void endElement(String uri, String localName, String qName)throws SAXException{
    System.out.println("Fin de l'élément " + qName);      
    }

public void endDocument() throws SAXException {
    System.out.println("Fin du parsing");
}


public void characters(char[] data, int start, int end){  
   System.out.println("***********************************************");
   String rep ="";
   String str = new String(data, start, end);


   System.out.println("Donnée du nœud " + node + " : " + str);


}

}

and this is the server Slave that executes the parsing:

public class ServerEsclave implements Runnable{


private final Socket socket;
private  ServerSocket serverSocket;
private ServerMaitre serverMaitre;
ServerEsclave(Socket socket, ServerSocket serveur){
    this.socket = socket;
    this.serverSocket = serveur;
}

ServerEsclave(Socket socket, ServerMaitre serverMaitre) {
    // TODO Auto-generated constructor stub
    this.socket = socket;
    this.serverMaitre = serverMaitre;
}

@Override
public void run() {
    try {
        BufferedReader input = new BufferedReader(
                new InputStreamReader(socket.getInputStream(), "8859_1"), 1024);
        StringBuffer sb = new StringBuffer();
        InputStream ch = socket.getInputStream();
        //sb.append(input.readLine());
        //System.out.println(sb);
        //String[] commande = sb.toString().split(" ");
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();

            parser.parse(ch, new MyXMLHandler());

            }catch (DOMException e) {
                e.printStackTrace();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (TransformerFactoryConfigurationError e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        catch(IOException e) {System.out.println(e);}
        finally {
            try { if(socket != null) socket.close();}
            catch(IOException e) {}
        }
    }
}

XML code that I want to parse:

<?xml version="1.0" encoding="iso-8859-1"?>
<Request>
   <ReqName> LIST </ReqName>
   <Dir>/home/abdou/Bureau</Dir>   
   <MailAddress>[email protected]</MailAddress>
</Request>

And finally this is what I get on the console

Début du parsing

qname = Request


Donnée du nœud Request :


qname = ReqName


Donnée du nœud ReqName : LIST Fin de l'élément ReqName


Donnée du nœud ReqName :


qname = Dir


Donnée du nœud Dir : /home/abdou/Bureau Fin de l'élément Dir


Donnée du nœud Dir :


qname = MailAddress


Donnée du nœud MailAddress : [email protected] Fin de l'élément MailAddress


Donnée du nœud MailAddress :

Fin de l'élément Request Fin du parsing


You can clearly see that he passes twice by each node and the secon value he prints in null.

Thank you for your help.

Upvotes: 1

Views: 272

Answers (1)

user8143344
user8143344

Reputation:

For futur viewers. This bug was, as potame said, due to the fact that the characters(...) event is not triggered once,it "splits" the char data.

Upvotes: 1

Related Questions