下面的代码调用 ReadXMLFile 构造函数
ReadXMLFile r=new ReadXMLFile("small.xml");
HashMap r=r.getrcset();
我正在使用 update fn 更新 sathiya HashMap ,它将由 ReadXMLFile 构造函数调用。它工作正常,即(元素被添加到sathiya hashmap)内部更新,因为我可以打印它。但 getrcset 文件中的 sathiya hashmap 为空。当我尝试打印它时,我得到 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();
}
}
}
最佳答案
虽然我对问题的标题和你的这句话完全感到困惑:
but the rcset is not empty in the getrcset file ??
如果您的问题是为什么是 sathiya
空的 ?原因是您在更新sathiya
后在rc上调用clear
if (qName.equalsIgnoreCase("CLUSTER")) {
update(rc);
rc.clear();// This line is clearing the content of rc
}
您的程序行为与这段代码类似:
Map<Integer, Map<String, String>> mainCont = new HashMap<Integer, Map<String,String>>();
Map<String, String> content = new HashMap<String, String>();
content.put("1", "1");
mainCont.put(1, content);
System.out.println(mainCont);
content.clear();
System.out.println(mainCont);
在上面的代码中,mainCont 持有对内容的引用,当内容被清除时,mainCont 将显示内容的更新值,因为它引用了该对象。
但不是 content.clear();
如果你成功了content = new HashMap<String, String>();
您将看到 mainCont 具有所有值,因为现在内容指向不同的对象。
回到你的代码:
而不是:
if (qName.equalsIgnoreCase("CLUSTER")) {
update(rc);
rc.clear();
}
这样做:
if (qName.equalsIgnoreCase("CLUSTER")) {
update(rc);
rc = new HashMap();
}
题外话:
考虑使用泛型而不是使用普通的非安全集合。
关于java - 由一个函数更新的 Hashmap 元素无法被同一 java 文件中的其他函数访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10400714/