下面的代码调用 ReadXMLFile 构造函数
ReadXMLFile r=new ReadXMLFile("small.xml");
HashMap r=r.getrcset();
我正在使用将由 ReadXMLFile 构造函数调用的 update fn 更新 sathiya 哈希图。它工作正常,即(元素被添加到 sathiya hashmap)内部更新,因为我可以打印它。但是 getrcset 文件中的 sathiya 哈希图是空的。当我尝试打印它时,我得到 0: {} 1:{} 。想不出它是空的
package iws.falcon.matcher.pbm;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.HashMap;
import java.util.Iterator;
import java.util.*;
public class ReadXMLFile {
public HashMap sathiya = new HashMap();
String filepath=null;
int cid=0;
int f=0;
HashMap rc=null;
public HashMap getrcset()
{
System.out.println("inside getsathiya"+sathiya.size());
Set set = sathiya.entrySet();
// Get an iterator
Iterator i = set.iterator();
// Display elements
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println("god"+sathiya);
return sathiya;
}
public void update(HashMap god)
{
sathiya.put(cid,god);
for (int i=0,n=sathiya.size();i<n;i++)
System.out.println("inside update fn"+sathiya.get(i));
cid++;
}
public ReadXMLFile (String fp)
{
filepath=fp;
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean name = false;
boolean cluster=false;
public void startElement(String uri, String localName,String qName,
Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("CLUSTER"))
{rc=new HashMap();
}`enter code here`
if (qName.equalsIgnoreCase("URI")) {
name = true;
}
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
if (qName.equalsIgnoreCase("CLUSTER"))
{
update(rc);
rc.clear();
}
}
public void characters(char ch[], int start, int length) throws SAXException {
if (name)
{
String e= new String(ch, start, length);
rc.put(e,e);
name = false;
}
}
};
saxParser.parse("small.xml", handler);
} catch (Exception e) {
e.printStackTrace();
}
}
}