Reputation: 353
I have the following XML file. As you can, there are 2 separate documents:
<doc>
<str name="id">1</str>
<str name="full_message_t">"""“God loves you more daddy....God loves them more</str>
<str name="source_t">” tweets teenage daughter of #MH370 chief steward http://t.co/Abw96As7xv"""</str>
<str name="news_t">TODAY</str>
<str name="link_t">"""“God loves you more daddy....God loves them more</str>
<long name="_version_">1464787951004155905</long>
</doc>
<doc>
<str name="id">2</str>
<str name="full_message_t">"""Flight #MH370 ended flight in southern Indian Ocean</str>
<str name="source_t"> says @NajibRazak http://t.co/NfesnDrcsP"""</str>
<str name="news_t">TOMORROW</str>
<str name="link_t">"""Flight #MH370 ended flight in southern Indian Ocean</str>
<long name="_version_">1464787951005204483</long>
</doc>
And the following SAXParser code:
public class ReadXMLFile {
public static void main(String[] args) {
//Declaration
final String row[] = new String[5]; //id, full_message_t, source_t, news_t, link_t
final ArrayList<String[]> tableResult = new ArrayList<String[]>(); //Store ArrayList of row[]
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler()
{
//When these are TRUE, extract the ocntent
boolean b_id = false;
boolean b_full_message_t = false;
boolean b_source_t = false;
boolean b_news_t = false;
boolean b_link_t = false;
//############################################################################################
//Look for <start> tags
public void startElement(String uri, String localName,String qName,
Attributes attributes) throws SAXException {
System.out.println("Start Element :" + qName);
//Only interested in <str>
if (qName.equalsIgnoreCase("str")) {
String name = attributes.getValue("name");
//Check for name="id"
if(name.equalsIgnoreCase("id")){
b_id = true;
}
//Check for name="full_message_t"
else if(name.equalsIgnoreCase("full_message_t")){
b_full_message_t = true;
}
else if(name.equalsIgnoreCase("source_t")){
b_source_t = true;
}
else if(name.equalsIgnoreCase("news_t")){
b_news_t= true;
}
else if(name.equalsIgnoreCase("link_t")){
b_link_t = true;
}
}//end if
}//end startElement
//############################################################################################
//Look for <end> tags
public void endElement(String uri, String localName,
String qName) throws SAXException {
System.out.println("End Element :" + qName);
//When reach </doc>, row Array is complete. Add into ArrayList
if (qName.equalsIgnoreCase("doc")) {
System.out.println("Push row into Arraylist : " + row[0]);
tableResult.add(row);
}
}//end endElement
//############################################################################################
//Get the content
public void characters(char ch[], int start, int length) throws SAXException {
if (b_id) {
//System.out.println("id : " + new String(ch, start, length));
row[0] = new String(ch, start, length);
System.out.println("The id is " + row[0]);
b_id = false;
}
else if (b_full_message_t) {
//System.out.println("fullmsg : " + new String(ch, start, length));
row[1] = new String(ch, start, length);
System.out.println("The fullmsg is " + row[1]);
b_full_message_t = false;
}
else if (b_source_t) {
//System.out.println("fullmsg : " + new String(ch, start, length));
row[2] = new String(ch, start, length);
System.out.println("The source is " + row[2]);
b_source_t = false;
}
else if (b_news_t) {
//System.out.println("fullmsg : " + new String(ch, start, length));
row[3] = new String(ch, start, length);
System.out.println("The news is " + row[3]);
b_news_t = false;
}
else if (b_link_t) {
//System.out.println("fullmsg : " + new String(ch, start, length));
row[4] = new String(ch, start, length);
System.out.println("The link is " + row[4]);
b_link_t = false;
}
}//end characters
};//end DefaultHandler
//############################################################################################
//Read the String
//saxParser.parse("solrTest.xml", handler);
File file = new File("solrTest.xml");
InputStream inputStream= new FileInputStream(file);
Reader reader = new InputStreamReader(inputStream,"UTF-8");
InputSource is = new InputSource(reader);
is.setEncoding("UTF-8");
saxParser.parse(is, handler);
} catch (Exception e) {
e.printStackTrace();
}
//Test output Arraylist
System.out.println("Test Result");
System.out.println(tableResult.get(0));
System.out.println(tableResult.get(1));
}
}
By the end of the execution, there should be 2 String Arrays, which will be added into the ArrayList. However at the last line when I print out the address, the addresses are the same. In another words, the last doc is stored into the ArrayList twice. Why is this so? What happened to the first doc?
Upvotes: 1
Views: 87
Reputation: 1511
You have to reinitialize your row array after the call
tableResult.add(row);
row = new String[5];
Otherwise it has always the same reference. Which means your list has two entries, with a reference to the same array.
Upvotes: 2