简体   繁体   English

在Android版Java中创建和写入xml文件

[英]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. 我已经创建了一个不错的科学计算器,现在想将输入和结果保存到文件中,以便在PC上使用以及作为存档报告使用,并且在整个过程中需要一些帮助。

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. 我先在excel中创建了所需的输出标头,然后将其保存为.xml文件,我认为这确实简化了.xml文件的设置,因此所有格式,列和标头都已建立,而我需要我希望将文件或其内容放置在我的Java代码中。

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. 我现在想将数据放入此xml文件/布局是特定列,因此当我将此.xml文件放回PC时,excel将打开一个漂亮的报表。 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. 我正在寻找方法,但是也许有人可以帮助我入门。希望这可以成为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 到目前为止,我已经做到了:1.在PC上创建具有所需格式的Excel文件2.将该文件另存为XML,Excel将插入用于格式化的标签

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 现在我需要做:3.在Android上编写Java代码以生成类似的XML文件。在Android上,将该XML文件保存在SD卡上。5。将该XML传输到PC并在Excel中打开

Update - so it looks like java.io.file might be my solution but as always nothing simple is easy. 更新-看起来java.io.file可能是我的解决方案,但一如既往,没有简单的事情容易。 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 我的代码可能看起来像这样,但是我看到以字符串形式输入xml代码时会遇到问题,因为它们具有自己的引号和与Java代码冲突的其他字符。

                    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. 内置了用于编写XML的DOM方法,但是我通常更喜欢使用David Megginson的XMLWriter。 http://www.megginson.com/downloads/xml-writer-0.2.zip 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. 这是我如何使用JAR在Android应用程序中输出XML的示例 Look at the method writeCylinders. 查看方法writeCylinders。

java.io.file did exactly what I needed. java.io.file正是我所需要的。 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. 如前所述,我首先从excel创建了xml文件,然后使用out.write命令在手机上重新创建了该文件,仅从应用程序中替换了应答者。 I needed to include carriage return and line feeds so excel would understand and everything works well. 我需要包括回车和换行符,以便excel能够理解并且一切正常。 My excel.xml was 515 lines so it takes up some room in my app but it works beautifully. 我的excel.xml为515行,因此它占用了我的应用程序中的一些空间,但是效果很好。 My calculator creates some interesting information and creates a nice report which I send to a pc and have excel open it. 我的计算器会创建一些有趣的信息,并创建一个不错的报告,然后将其发送到PC并由excel打开。 This is a major improvement to what a programable calculator can perform. 这是对可编程计算器可以执行的功能的重大改进。 And its in my pocket! 而且它在我的口袋里!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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