开发者

XML Question relating to Java and Python

开发者 https://www.devze.com 2023-01-09 01:28 出处:网络
I am trying to write an application using Python 2.7 that will allow the user to open a dialog box, pick a file, have it read into a structure of some sort (ArrayList, List etc...) open to suggestions

I am trying to write an application using Python 2.7 that will allow the user to open a dialog box, pick a file, have it read into a structure of some sort (ArrayList, List etc...) open to suggestions here and then to find basic statistical measures from the data (things like mean, standard deviation etc...) and to output the original file with a summary of the statistical measurements in XML format. Just wondering how is the best way to accomplish this. I have code for opening a window to allow a user to pick a file (from another website) but not sure how to use it to pass the selected file into the function that will read the xml file selected.

Here is the code for the window:

from Tkinter import *
from tkMessageBox import *
from tkColorChooser import askcolor              
from tkFileDialog   import askopenfilename      

def callback():
    askopenfilename() 
    Button(text='Please Select File', command=callback).pack(fill=X)
    mainloop()
    pass
def quit(event):
    if tkMessageBox.askokcancel('Quit','Do you really want to quit?'):
    root.destroy()

I think there is more to the pass function.

I have a version of the XMLReader in Java (That is run in BlueJ so don't know if it will run out side of it) code:

import java.util.*;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLReader
   {
       public static void main(String argv[]) {
       ArrayList XTimeStamp = new ArrayList();
       ArrayList XY = new ArrayList();
       ArrayList Price = new ArrayList();

       try {
           File file = new File("<PATH>\shares.xml");
           DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
           DocumentBuilder db = dbf.newDocumentBuilder();
           Document doc = db.parse(file);
           doc.getDocumentElement().normalize();
           System.out.println("Root element " + doc.getDocumentElement().getNodeName());
           NodeList nodeLst = doc.getElementsByTagName("shareprice");
           System.out.println("Share Price");

   for (int s = 0; s < nodeLst.getLength(); s++) {

       Node fstNode = nodeLst.item(s);
       if (fstNode.getNodeType() == Node.ELEMENT_N开发者_JAVA技巧ODE) {
       Element fstElmnt = (Element) fstNode;
       NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("timeStamp");
       Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
       NodeList fstNm = fstNmElmnt.getChildNodes();
       String timeStamp = fstNm.item(0).getNodeValue(); 
       XTimeStamp.add(timeStamp); 
       System.out.println("timeStamp : "  + ((Node) fstNm.item(0)).getNodeValue());
       NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("Price");
       Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
       NodeList lstNm = lstNmElmnt.getChildNodes();
       String YValue = lstNm.item(0).getNodeValue(); 
       Price.add(YValue);
       System.out.println("Price : " + ((Node) lstNm.item(0)).getNodeValue());
               }
             }
            } catch (Exception e) {
          e.printStackTrace();
      }
  System.out.println(XTimeStamp);
  System.out.println(Price);
  XY.add (XTimeStamp);
  XY.add (Price);
 System.out.println(XY);
  }
}

The one thing I don't like about the Java code is that I have to include the path of the file. I would like to allow the user to pick the file in the Java version as well. The reason I started with java is I have a little bit more experience (but not much). To create the application what is the best way forward, Python or Java and depending on which one any help on getting started sorting the issues would be greatly appreciated.


Well, you could do this in either Python or Java without too much difficulty, but I would make up your mind as to which one you want! I'll talk about Python because I much prefer it to Java, but I'm sure you can find people who will help you with Java.

So first, you want a file selection box with Tkinter. Well, that's pretty standard and there's lots of documentation and code boilerplate on Google. Try:

import Tkinter,tkFileDialog

root = Tkinter.Tk()
file = tkFileDialog.askopenfile(parent=root,mode='rb',title='Choose a file')

for starters; you can make it much more complicated if you'd like. Then to parse the file you could use Python's xml.parsers.expat module:

import xml.parsers.expat
xmlparser.ParseFile( file )

You'll have to write functions for what to do when the parser meets each type of node, but that's in the documentation. Then, you want to do some statistics on what you parse; scipy has all the functions you'll ever need to do so. This is the bit where you'll need to roll your own code the most.

Finally, you want to write the statistics you've generated to a new file (I think?).

statsfile = open( "stats.txt", w )
try:
    statsfile.write( stats )
finally:    
    statsfile.close()

Does that help?

0

精彩评论

暂无评论...
验证码 换一张
取 消