简体   繁体   中英

creating and writing to an xml file in java for android

I have created a nice scientific calculator and now want to save inputs and results to a file for use on a PC and as an archieval report and need some help with the overall process.

I have first created the required output headers and such in excel and then saved it as an .xml file I am thinking that this really eased the setup of the .xml file so all the formatting and columns and headers are established and all I need to do is place this file or its cotents in my java code, I hope.

Is this a good approach so far?

I would like to now put my data into this xml file/layout is specif columns so when I put this .xml file back on a PC that excel will open a nice looking report. no formulas just the data.

Am I on the right track? any good links to get going? I am off to search for how to but maybe somebody can get me started The hopes are this can be a good example for oneonline.

So far I have done this: 1.Create an Excel file on PC with formatting that you need 2.Save that file as XML, Excel will insert Tags for formatting

now I need to do: 3.Write java code to generate similar XML file on Android 4.On Android, save this XML file on SD Card 5.Transfer the XML to PC and open that in Excel

Update - so it looks like java.io.file might be my solution but as always nothing simple is easy. My code might look like this but I see I am going to have problems with entering the xml code as strings because they have quotes of their own and other characters that conflict with java code.. this is what I am trying

                    astring1 = "<?xml version="1.0"?>";
                astring2 = <?mso-application progid="Excel.Sheet"?>;
                astring3 = Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet";
                astring4 =  xmlns:o="urn:schemas-microsoft-com:office:office"

                if (((m1_ss_spinner.getSelectedItem().toString().equals("Square"))))
                {
                    try 
                    {
                        BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
                        out.write astring1;
                        out.write astring2;
                        out.write astring3;
                        out.write astring4;
                        out.close();
                    } catch (IOException e) {
                    } //end try

                } //end if

DOM methods for writing XML are built in, but I usually prefer using David Megginson's XMLWriter. http://www.megginson.com/downloads/xml-writer-0.2.zip

Here's an example of how I've used the JAR to output XML in an Android app. Look at the method writeCylinders.

java.io.file did exactly what I needed. As I said before, I first ccreated the xml file from excel and used the out.write command to recreate the file on my phone, substituting just the answere from my app. I needed to include carriage return and line feeds so excel would understand and everything works well. My excel.xml was 515 lines so it takes up some room in my app but it works beautifully. My calculator creates some interesting information and creates a nice report which I send to a pc and have excel open it. This is a major improvement to what a programable calculator can perform. And its in my pocket!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM