Parsing XML File selected by user

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Anthony Keane
    New Member
    • Jul 2010
    • 4

    Parsing XML File selected by user

    I am trying to allow a user pick the XML file that they would like to parse and use a generic parser to accomplish the task. The code is taken from different websites, just wondering if the code I have so far can be integrated and if so how?

    Code:
    import Tkinter,tkFileDialog
    import os
    from xml.etree import ElementTree as ET
    
    root = Tkinter.Tk()
    file = tkFileDialog.askopenfile(parent=root,mode='rb',title='Choose a file')
    root.mainloop()
    
    def XMLReader( file ):
        xml_file = file
        tree = ET.parse(xml_file)
        try:
           tree = ET.parse("sar")
        except Exception, inst:
           print "Unexpected error opening %s: %s" % (xml_file, inst)
        return
    Any help and advice would be greatly appreciated as I am a beginner.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I am not familiar with ElementTree, but it looks like you have a good start. What do you want to integrate this code into?

    Comment

    • Anthony Keane
      New Member
      • Jul 2010
      • 4

      #3
      Originally posted by bvdet
      I am not familiar with ElementTree, but it looks like you have a good start. What do you want to integrate this code into?
      At the moment when I run the code I get a dialog box to pick the file, but it does not pass it onto the XMLReader. It is a multi step project. Step 1 is to get the file read into the system then get statistics things like mean etc. and to plot the data on a graph (someone suggested using scipy and numpy for this but I am not at that stage), also produce a new XML File with the original data and the statistics.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        In the code you posted, you did not call function XMLReader(). tkFileDialog.as kopenfile() returns and file name with the complete path if a file is selected or an empty string if "Cancel" is selected. You need to open the file and pass the open file object to ElementTree. Example:
        Code:
        fn = tkFileDialog.askopenfile(parent=root,mode='rb',title='Choose a file')
        if fn:
            tree = ET.parse(open(fn, "rb"))

        Comment

        • Anthony Keane
          New Member
          • Jul 2010
          • 4

          #5
          Code at Present

          Code:
          import Tkinter,tkFileDialog
          import os
          from xml.etree import ElementTree as ET
          from xml.dom import minidom
          
          
          root = Tkinter.Tk()
          fn = tkFileDialog.askopenfile(parent=root,mode='rb',title='Choose a file')
          root.mainloop()
          
          def XMLReader(fn):
              xNode = []
              yNode = []
              tree = ET.parse(open(fn, "rb"))
              xmldoc = minidom.parse ( fn )
              xmldoc
              print xmldoc.toxml()
              xmldoc.childNodes
              ECGNode = xmldoc.firstChild
              print ECGNode.toxml()
              ECGNode.childNodes
              readingNode = ECGNode.childNodes[1]
              readingNode
              timeStamp = xNode.childNodes[2]
              xnode.append ( timeStamp )
              YVoltage = yNode.childNodes[3]
              ynode.append ( YVoltage )
          Here is the code I have at present a bit chunky and I would get rid of the print statements if I knew the code work, but at present there is problems with it that I can't figure out.

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            You should not need to use both minidom and ElementTree. What data are you trying to read from the XML file? Generally you should use the getElementsByTa gName method (if using minidom) to get a list of the elements you want, then read attributes and/or text from child elements. If you could provide a sample of the XML data and what you want to read, I can give you an example using minidom.

            Comment

            • Anthony Keane
              New Member
              • Jul 2010
              • 4

              #7
              The XML looks like this :
              Code:
              <?xml version="1.0" ?><SharePrice>
              	<ShareInfo>
              		<Time> 12:00:00.01</Time>
              		<Price>  12.6</Price>
              	</ShareInfo>
              I am trying to get time in the form of a timeStamp and Price in the form of integer so that I can perform Statistical analysis on the data (and plot the Price V. Time)

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                This code will create a dictionary of the tags as keys and the text in between the tags as the values:
                Code:
                from xml.dom.minidom import parseString
                xml_str = """<?xml version="1.0" ?>
                <SharePrice>
                    <ShareInfo>
                        <Time> 12:00:00.01</Time>
                        <Price>  12.8</Price>
                    </ShareInfo>
                    <ShareInfo>
                        <Time> 12:00:10.01</Time>
                        <Price>  12.6</Price>
                    </ShareInfo>
                    <ShareInfo>
                        <Time> 12:00:20.01</Time>
                        <Price>  13.0</Price>
                    </ShareInfo>
                </SharePrice>"""
                
                xmlDoc = parseString(xml_str)
                
                tagNames = ["Time", "Price"]
                dd = {}
                for name in tagNames:
                    elemList = xmlDoc.getElementsByTagName(name)
                    for elem in elemList:
                        dd.setdefault(name, []).append(elem.firstChild.nodeValue)

                Comment

                Working...