0

I have one doubt regarding xml reading using Stax Parser. Expected Result:

hq_seq_mast {SEQ_VAL=SEQ_VAL, 1=column,LAST_SEQ_VAL=LAST_SEQ_VAL, 2=column} db_sequence_info{TNAME=TNAME, 3=column, CNAME=CNAME, 4=column}

Actual Result:

{hq_seq_mast={}, db_sequence_info={}}

What Mistake I have Done.

import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
public class SaxDiffChecker {
public static LinkedHashMap<String,LinkedHashMap<String,String>>FirstxmlCollection=new LinkedHashMap<String,LinkedHashMap<String,String>>();
public static LinkedHashMap<String,String> firstXml=new LinkedHashMap<String,String>();
public static String tableName=null;
public static String tableendElement=null;
public static String columendElement=null;
public static String characterElement=null;
public static String tempendElement=null;
public static String tempName=null;
public static String ntempName=null;
public static int key=0;
public void print(String fileLocation) {
 try {
 FileInputStream fileInputStream = new FileInputStream(fileLocation);
 XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fileInputStream);
 while (xmlStreamReader.hasNext()) {
 printEventInfo(xmlStreamReader);
 }
 System.out.print("======="+FirstxmlCollection);
 xmlStreamReader.close();
 } catch (XMLStreamException e) {
 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
 } catch (FileNotFoundException e) {
 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
 }
}
 private static void printEventInfo(XMLStreamReader reader) throws XMLStreamException {
 int eventCode = reader.next();
 switch (eventCode) {
 case 1 :
 //System.out.println("event = START_ELEMENT");
 //System.out.println("Localname = "+reader.getLocalName()+" Attribute Name"+reader.getAttributeValue(0));
 if("table".equalsIgnoreCase(reader.getLocalName())){
 tableName=reader.getAttributeValue(null,"name");
 tableendElement=reader.getLocalName();
 }else if("column".equalsIgnoreCase(reader.getLocalName())){
 columendElement=reader.getLocalName();
 firstXml.put(reader.getAttributeValue(0),reader.getAttributeValue(0));
 }else{
 tempendElement=reader.getLocalName();
 }
 break;
 case 2 :
 //System.out.println("event = END_ELEMENT");
 //System.out.println(tableendElement+"Localname = "+reader.getLocalName());
 if(tableendElement.length()>0 && tableendElement!=null && tableendElement.equalsIgnoreCase(reader.getLocalName())){
 System.out.println(tableName+"Heeee"+firstXml);
 FirstxmlCollection.put(tableName,firstXml);
 firstXml.clear();
 }else if(columendElement.length()>0 && columendElement!=null && columendElement.equalsIgnoreCase(reader.getLocalName())){
 firstXml.put(""+key++,columendElement);
 }
 break;
 case 3 :
 //System.out.println("event = PROCESSING_INSTRUCTION");
 //System.out.println("PIData = " + reader.getPIData());
 break;
 case 4 :
 //System.out.println("event = CHARACTERS");characterElement
 //System.out.println("Characters = " + reader.getText());
 break;
 case 5 :
 //System.out.println("event = COMMENT");
 //System.out.println("Comment = " + reader.getText());
 break;
 case 6 :
 System.out.println("event = SPACE");
 System.out.println("Space = " + reader.getText());
 break;
 case 7 :
 System.out.println("event = START_DOCUMENT");
 System.out.println("Document Started.");
 break;
 case 8 :
 //System.out.println("event = END_DOCUMENT");
 System.out.println("Document Ended");
 break;
 case 9 :
 //System.out.println("event = ENTITY_REFERENCE");
 //System.out.println("Text = " + reader.getText());
 break;
 case 11 :
 //System.out.println("event = DTD");
 //System.out.println("DTD = " + reader.getText());
 break;
 case 12 :
 //System.out.println("event = CDATA");
 //System.out.println("CDATA = " + reader.getText());
 break;
 }
}
public static void main(String[] args) {
 SaxDiffChecker eventsPrinter = new SaxDiffChecker();
 eventsPrinter.print("C:\\Users\\vellikutti\\Desktop\\bala.xml");
 }

}

xml:
<data-dictionary name="hq">
 <table engine-type="InnoDB" name="hq_seq_mast" scope="HQ">
 <columns>
 <column name="SEQ_VAL">
 <data-type>int(10)</data-type> 
 <nullable>false</nullable> 
 </column>
 <column name="LAST_SEQ_VAL">
 <data-type>int(10)</data-type> 
 <nullable>false</nullable> 
 </column>
 </columns>
 </table>
 <table engine-type="InnoDB" name="db_sequence_info" scope="HQ">
 <columns>
 <column name="TNAME">
 <data-type>varchar(30)</data-type> 
 <nullable>false</nullable> 
 </column>
 <column name="CNAME">
 <data-type>varchar(30)</data-type> 
 <nullable>false</nullable> 
 </column>
 </columns>
 </table>
fivedigit
18.7k6 gold badges59 silver badges61 bronze badges
asked May 17, 2012 at 19:08

1 Answer 1

2

Here is your problem firstXml.clear(). The data is being cleared. You may want to make firstXml a local variable or set it to firstXml = new LinkedHashMap<String,String>();, instead of clearing it.

Code Changes:

//create new map instead of clearing saved map (remember - obj passed by ref)
firstXml=new LinkedHashMap<String,String>();//firstXml.Clear();

Result:

hq_seq_mastHeeee [SEQ_VAL:SEQ_VAL, 0:column, LAST_SEQ_VAL:LAST_SEQ_VAL, 1:column]
db_sequence_infoHeeee [TNAME:TNAME, 2:column, CNAME:CNAME, 3:column]
Document Ended
=======[hq_seq_mast:[SEQ_VAL:SEQ_VAL, 0:column, LAST_SEQ_VAL:LAST_SEQ_VAL, 1:column], db_sequence_info:[TNAME:TNAME, 2:column, CNAME:CNAME, 3:column]]
answered May 17, 2012 at 19:33
5
  • Dear user845279 now result like {hq_seq_mast={}, db_sequence_info={}} Commented May 17, 2012 at 19:41
  • @BALASCJP - I just tested it, replace firstXml.clear() with firstXml = new LinkedHashMap<String,String>() and it'll work. Commented May 17, 2012 at 19:47
  • What is exact difference between firstXml.clear() & firstxml=new LinkedHashMap<String,String>();. In above I first assigned FirstxmlCollection.put(tableName,firstXml); and then only cleared then how the problem arises can you please explain it firstXml.clear() Commented May 18, 2012 at 5:29
  • I'm not sure if I can explain the idea in a comment, you should read about pass by reference. The line FirstxmlCollection.put(tableName,firstXml); doesn't make a copy of the firstXml object, it just points to it. So when you execute firstXml.clear(), you are clearing the only existing copy of the list. In order for this to work, you have to create a new list and leave the other one as it is. Try and look up Pass-by-Reference vs Pass-by-Value, it's a big part of OOP. Commented May 18, 2012 at 6:25
  • Thank u very much for your explanation.One doubt I declared firstXml as static then only this problem arises right??? Commented May 18, 2012 at 6:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.