6 Replies Latest reply: Mar 2, 2012 11:45 AM by EJP RSS

    Appending the generated XML file through java

    814917
      Hi,I wrote a code that reads data from the excel file and i'm able to convert each record of the excel file to a node in XML.
      But the problem is that only one node is getting resulted in xml file that too the last record.
      Hi could anybody help me get over this issue.Any help is greatly appreciated.



      Her's my code

      import jxl.Cell;
      import jxl.Sheet;
      import jxl.Workbook;
      import jxl.WorkbookSettings;
      import jxl.read.biff.BiffException;
      import java.io.FileInputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.util.Locale;
      import java.io.File;
      import java.io.FileWriter;
      import javax.xml.bind.*;


      import javax.xml.bind.annotation.XmlElement;

      public class ReadExcel {
           
           WorkbookSettings ws = null;          
           Workbook workbook = null;          
           Sheet s = null;
           Cell rowData[] = null;          
           int totalSheets = 0;
           Record record = null;
           
           
           
           public static void main(String[] args) throws Exception
           {
                
                ReadExcel excel=new ReadExcel();
                excel.readAndConvertToXML("C:\\Users\\257367\\Desktop\\karthik1.xls");
                
           }
           public void readAndConvertToXML(String filepath)
           {

                /**JAXBContext serves as the Entry Point for making use of the JAXB API.
                * It is initiated by the list of Class objects whose objects want to be represented as XML Documents.
                * Then an instance of Marshaller is created by calling JAXBContext.createMarshaller() method.
                * The Java object of type Person is marshalled into the XML Document by calling the Marshaller.marshall() method.
                * Since we want the XML Document to look Well-indented, we have set the property Marshaller.JAXB_FORMATTED_OUTPUT to true.
                */
                

                FileInputStream fs = null;
                
                try
                {
                     JAXBContext context = JAXBContext.newInstance(Record.class);
                     Marshaller marshaller = context.createMarshaller();
                     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                     fs = new FileInputStream(new File(filepath));
                     InputStream is = (InputStream)fs;
                     ws = new WorkbookSettings();
                     ws.setLocale(new Locale("en", "EN"));
                     workbook = workbook.getWorkbook(is, ws);
                     s = workbook.getSheet(0);
                     
                     for(int i=0;i<s.getRows();i++)
                     {
                          rowData = s.getRow(i);
                          if(rowData.length>1)
                          {
                               record=new Record(rowData);
                               marshaller.marshal(record, new FileWriter(".\\src\\Person.xml"));
                               
                          }
                          
                     }
                     workbook.close();
                }
                catch(IOException exception)
                {
                     exception.printStackTrace();
                }
                catch(BiffException biffException)
                {
                     biffException.printStackTrace();
                }
                catch(Exception exception)
                {
                     exception.printStackTrace();
                }
                
           }
           

      }
        • 1. Re: Appending the generated XML file through java
          EJP
          What 'appendIng'? You're creating a new FileWriter for every marshall() call. No appending whatsoever. Your code is working as expected.
          • 2. Re: Appending the generated XML file through java
            814917
            Hi,
            Thanks for the reply.As suggested by you I've changed the code as



            try
                      {
                           JAXBContext context = JAXBContext.newInstance(Record.class);
                           Marshaller marshaller = context.createMarshaller();
                           marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

                           fs = new FileInputStream(new File(filepath));
                           InputStream is = (InputStream)fs;
                           ws = new WorkbookSettings();
                           ws.setLocale(new Locale("en", "EN"));
                           workbook = workbook.getWorkbook(is, ws);
                           s = workbook.getSheet(0);
                           FileWriter fileWriter = new FileWriter(".\\src\\Person.xml",true);
                           
                           for(int i=1;i<s.getRows();i++)
                           {
                                rowData = s.getRow(i);
                                record = new Record(rowData);
                                if(rowData.length>1)
                                {
                                     
                                     marshaller.marshal(record, fileWriter);
                                     
                                     
                                }
                                
                           }
                           workbook.close();
                      }

            So if the code works fine then
            i'm getting my xml resultled as shown


            <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
            <record>
            <SNo>1</SNo>
            <taskDescription>sdlhopsdih goi sdfghodfg dofgh dfgodsh o</taskDescription>
            <taskName>uploading data</taskName>
            <taskNo>1</taskNo>
            </record>
            <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
            <record>
            <SNo>2</SNo>
            <taskDescription>fghdfghfgjghjk</taskDescription>
            <taskName>development</taskName>
            <taskNo>4</taskNo>
            </record>
            <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
            <record>
            <SNo>3</SNo>
            <taskDescription>gfhkfgi hjf'nhji f'hjfi'</taskDescription>
            <taskName>testing</taskName>
            <taskNo>5</taskNo>
            </record>



            How can i avoid this "<?xml version="1.0" encoding="UTF-8" standalone="yes"?>" being repeated for every node rather than for the first time



            Thanks

            Edited by: 811914 on Apr 19, 2011 9:37 PM
            • 3. Re: Appending the generated XML file through java
              EJP
              I dunno, what's a Record? Seems like it gets marshalled as a Document. You need it to be marshalled as an Element, which would also mean you would need something else around it to be marshalled as the Document. Maybe you have to create the entire marshalled document object in memory first? I don't know how JAXB marshalling works, and you're in the wrong forum for that anyway.
              • 4. Re: Appending the generated XML file through java
                873819
                If you want to append some output to an existing XML file then first unmarshall your xml file to the JAXB java object content model
                make your changes to the java object and now marshall the changed java object back to the same file thats it
                    JAXBContext jaxbContext;
                    try
                    {
                      jaxbContext =
                          JAXBContext.newInstance("yourpackage");
                      Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
                
                      YourCustomObject yco = (YourCustomObject) unmarshaller.unmarshal(new File("custom.xml"));
                      yco.setSomething("something-changed");
                      Marshaller marshaller = jaxbContext.createMarshaller();
                      marshaller.marshal(yco, new File("custom.xml"));
                    }
                    catch (JAXBException e)
                    {
                      e.printStackTrace();
                    }
                let me know if it makes sense

                Thanks,
                Tej
                Tejeswar Arlagadda
                • 5. Re: Appending the generated XML file through java
                  880683
                  Hi Tej:

                  Let me raise a small concern in the given suggestion (I know it is bit pretty old bug - but I do have a similar issue facing .. )

                  As you suggested, to unmarshall the newly created xml to append latest update will have performance issue - I am afraid.

                  For eg : Let us say I got 50000 records to process - each record are processed one by one at a time .. (By which I mean, it is not the case of one single collection objects holding 50000 records and @ end create one single xml).

                  But per your suggestion, my xml will have 49999 record objects in memory footprint unmarshalled, to write the last record marshalled again to same xml file ..

                  BtB- am not sure issue got resovled for repeated entry of "<?xml version="1.0" encoding="UTF-8" standalone="yes"?>" being repeated for every node rather than for the first time.

                  Any different approaches - please share the same ..

                  Regards

                  Abhilash
                  • 6. Re: Appending the generated XML file through java
                    EJP
                    Performance is irrelevant when you don't have a choice. You can't append to XML files, you have to do what the poster above said. If you have to do this 49999 times you have a design problem. Rethink. For example, keep the file open.