SAX 解析器在主 class 中返回空列表
SAX Parser returning empty List in main class
我正在尝试从 GraphML 文件中提取数据。文件 Link: https://www.dropbox.com/s/mp5x7ykqabmpzsg/EXEMPLE%202.graphml?dl=0
这是我的默认处理程序 class:
public class MLAnalyser 扩展了 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() ) ;
}
由此 class 三个列表的大小是正确的,但是在主要 class 中,当我创建一个 MLAnalyser 对象并打印我的列表的大小时,它们是空的。
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()");
}
}
}
解决方案是调用 "public void endDocument()" 并在其中编写代码。
我正在尝试从 GraphML 文件中提取数据。文件 Link: https://www.dropbox.com/s/mp5x7ykqabmpzsg/EXEMPLE%202.graphml?dl=0
这是我的默认处理程序 class:
public class MLAnalyser 扩展了 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() ) ;
}
由此 class 三个列表的大小是正确的,但是在主要 class 中,当我创建一个 MLAnalyser 对象并打印我的列表的大小时,它们是空的。
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()");
}
}
}
解决方案是调用 "public void endDocument()" 并在其中编写代码。