SAX Parser returning empty List in main class

58 views Asked by At

I am trying to extract data from GraphML file. file Link: https://www.dropbox.com/s/mp5x7ykqabmpzsg/EXEMPLE%202.graphml?dl=0

Here is my default handler class:

public class MLAnalyser extends DefaultHandler {

public List<List<String>> elementsAttributes = new ArrayList<List<String>>(); 
List<Integer> index_node= new ArrayList<Integer>();
List<Integer> target_edge= new ArrayList<Integer>();
List<Integer> source_edge= new ArrayList<Integer>();

public void startElement(String namespaceURI, String sName, String qName, Attributes attrs) throws SAXException { 
    String eName = sName; 
    if ("".equals(eName))
        eName = qName;
if (eName == "node") {

    if (attrs != null) { 
        for (int i = 0; i < attrs.getLength(); i++) { 
            String aName = attrs.getLocalName(i);
            if ("".equals(aName)) { 
            aName = attrs.getQName(i); 
            } 
            System.out.println(" " + aName + "=" + attrs.getValue(i) +"" ) ; 
            if (aName == "id") {
                int x = Integer.parseInt(attrs.getValue(i));
                index_node.add(x);
            }      
        } 
    } 
}
else if (eName == "edge") {

    if (attrs != null) { 
        //Attributes listing
        for (int i = 0; i < attrs.getLength(); i++) { 
            String aName = attrs.getLocalName(i);
            if ("".equals(aName)) { 
                aName = attrs.getQName(i); 
            } 
            System.out.println(attrs.getValue(i) ) ; 
            if (aName == "source")
            {
               int x = Integer.parseInt(attrs.getValue(i));
               source_edge.add(x);
            }
            else if (aName == "target")
            {
               int x = Integer.parseInt(attrs.getValue(i));
               target_edge.add(x);
            }          

        }
    } 
}
System.out.println(index_node.size() + "   " + source_edge.size() + "    " + target_edge.size() ) ;

}

from this class the size of the three lists was correct, but in the main class when I create an MLAnalyser object and print the size of my lists, they are empty.

public class GraphML_Parser {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    MLAnalyser a = new MLAnalyser() ;

        try{

            SAXParserFactory fabrique = SAXParserFactory.newInstance();
            SAXParser parseur = fabrique.newSAXParser();
            System.out.println(a.index_node.size());
            File myfile = new File("D:/EXEMPLE 2.graphml");

            DefaultHandler gestionnaire = new MLAnalyser();

            parseur.parse(myfile, gestionnaire);

            for(int i=0; i<a.index_node.size(); i++)
            {
                System.out.println("node "+ i + ":");
                System.out.println(a.index_node.get(i));
            }
            //GrapheMatrix mat = new GrapheMatrix(a.nodeNum,a.arcNum);
            //a.create_matrix();
        }

        catch(ParserConfigurationException pce){
            System.out.println("Erreur de configuration du parseur");
            System.out.println("Lors de l'appel à newSAXParser()");
        }catch(SAXException se){
            System.out.println("Erreur de parsing");
            System.out.println("Lors de l'appel à parse()");
        }catch(IOException ioe){
            System.out.println("Erreur d'entrée/sortie");
            System.out.println("Lors de l'appel à parse()");
        }
}

}

1

There are 1 answers

0
Khaled On BEST ANSWER

The solution was to call "public void endDocument()" and write your code in it.