The below code calls the ReadXMLFile constructor
ReadXMLFile r=new ReadXMLFile("small.xml");
HashMap r=r.getrcset();
I am updating the sathiya hashmap using update fn which will be called by the ReadXMLFile constructor. Its working fine ie.( elements are getting added to sathiya hashmap) inside update because i can print it . but the sathiya hashmap is empty in the getrcset file. while i try to print it i get 0: {} 1:{} . cant figure y its empty
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();
}
}
}
Though I am completely confused by the title of question and this line of yours:
If your question is why is
sathiyaempty ? The reason is that you are calling clear on rc after updatingsathiyaYour program behavior is similar to this piece of code:
In above code, mainCont is holding a reference to content, the moment content is cleared mainCont will show the updated value of content, since its referring to that object.
But instead of
content.clear();if you make itcontent = new HashMap<String, String>();You will see that mainCont has all the values since now content points to a different object.
Coming back to your code:
Instead of :
do this:
off topic:
Consider using generics instead of using plain non safe Collections.