5 Replies Latest reply: Feb 22, 2008 2:27 AM by 807603 RSS

    duplicate values while printing into a file

    807603
      Hi All,
      I am retriving values from database and printing it in a xml file.The querry that i run doesnt have any duplicate values but file printing duplicate values are generated....
      int DomcntLocal = Domcnt;
                int i =0;
                int constant =0;
                Iterator<DefineTags> it = null ;
           it = myData.iterator();

                while(it.hasNext())
                {
                     
                     Domcnt++;
                     DomcntLocal = 0;
                     DivideRows=0;
                     //int count;
                     while(DomcntLocal< 5)
                     {
                     dom= initiatexml.createDocument(dom);
                     InitiateXML initiatexml = new InitiateXML();
                     ProjectCls projectcls = new ProjectCls();
                     rootEle =initiatexml.createDOMTree(dom,i);
                     projectcls.InitiateProject(rootEle,DivideRows,dom,it,myData);
                     i++;
                     if(it.hasNext())
                          initiatexml.printToFile(dom,i);
                     DomcntLocal++;
                     numOfRows--;
                }
                                    
                     System.out.println("Generated file successfully.");
                     Domcnt--;
                     
                }
           .....................
      ...............................
      public void InitiateProject(Element rootEle,int numOfRows,Document dom,Iterator<DefineTags> it,List<DefineTags> myData) throws.....
      ......
      .....
      while(it.hasNext()) {
                     
                     DefineTags tag = (DefineTags)it.next();
                     System.out.println("Project Name is::"+tag.getProject_name());
                     Element rootProject = createProject(tag,dom);
                     System.out.println("The rootProject prj is::" +rootProject);
                     cnt++;
                     it.next();
                     if(cnt==numOfRows+5)
                          break;
                     rootEle.appendChild(rootProject);
      ............
      ........
      public Element createProject(DefineTags tag,Document dom)throws WCMExceptions
           {
           int Domcnt = 0;     
                     Element rootProject = dom.createElement("importProject");
                
                     System.out.println("u r in project craetion tag");
                     
                     Element project = dom.createElement("project");
                     //Element name = dom.createElement("name");
                     Element g_name = dom.createElement("name");
                     Text nameText = dom.createTextNode(tag.getProject_name());
                     Element pr
      .......................
      ...............
      Can any one help me out
        • 1. Re: duplicate values while printing into a file
          807603
          Your code in the topicstart is hard to read/interpret and isn't compileable nor runnable after just copypasting.

          Please use the [code] and [/code] tags to preformat your code. Please show a small self contained compilable example of your code instead of a cut-down code with lot of dirty stuff. Write a Test class with a simple main method which reproduces your problem.

          Google using the keywords "How To Ask Questions The Smart Way" and "SSCCE".
          • 2. Re: duplicate values while printing into a file
            807603
            I don't know if this is the issue, but it is highly suspect that you would call i.next() twice in the same while(hasNext()) loop.
            • 3. Re: duplicate values while printing into a file
              807603
              There are lot of duplicate values that are displayed...
              So just a check over duplicate values i used it.next
              • 4. Re: duplicate values while printing into a file
                807603
                Just to simplfy my code.....
                Here some classes cume into picture...
                1>connection to db and retriving values.....from here i take valus for iterator it.
                2>Source class from which i call 2 classes
                it = myData.iterator();
                          while(it.hasNext())
                          {
                               
                               Domcnt++;
                               DomcntLocal = 0;
                               DivideRows=0;
                               //int count;
                               while(DomcntLocal< 5)
                               {
                               dom= initiatexml.createDocument(dom);
                               InitiateXML initiatexml = new InitiateXML();
                               ProjectCls projectcls = new ProjectCls();
                               rootEle =initiatexml.createDOMTree(dom,i);
                               projectcls.InitiateProject(rootEle,DivideRows,dom,it,myData);
                               i++;
                               if(it.hasNext())
                               initiatexml.printToFile(dom,i);
                               DomcntLocal++;
                               numOfRows--;
                               }
                          
                               Domcnt--;
                }

                Initiate Project Class snippet
                public void InitiateProject(Element rootEle,int numOfRows,Document dom,Iterator<DefineTags> it,List<DefineTags> myData) throws WCMExceptions{
                int cnt=0;     
                          int i=0;
                          while(it.hasNext()) {
                               
                               DefineTags tag = (DefineTags)it.next();
                               Element rootProject = createProject(tag,dom);
                               cnt++;
                               it.next();
                               if(cnt==numOfRows+5)
                                    break;
                               rootEle.appendChild(rootProject);
                          System.out.println               
                                         
                          }
                          }
                ******************createProject contains element creation and adding values to it**************
                ***********************I am using 5 as i want that each of the xml file should contain 5 record********

                public void printToFile(Document dom,int i)throws WCMExceptions{
                int Domcnt =0 ;
                try
                {
                /**
                * print the output .
                */
                String packageBody = "D:\\packageBody";
                String x=packageBody+i+".xml";
                System.out.println("U r in printfile"+i);
                OutputFormat format = new OutputFormat(dom);
                format.setIndenting(true);
                /**
                to generate a file output use fileoutputstream instead of system.out
                **/
                XMLSerializer serializer = new XMLSerializer(
                new FileOutputStream(new File(x)), format);
                serializer.serialize(dom);
                } catch(IOException ie) {
                throw new WCMExceptions("Error occured while writing data in a file");
                }
                }
                }

                I hope that my code may be clear
                i have given code snippet becoz i think error lies somewhere in loop or some initialization.
                • 5. Re: duplicate values while printing into a file
                  807603
                  How hard is it really to press the code button when posting code. It makes a great difference in reading.

                  Regards