Write returned XML to a .xml file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jld730
    New Member
    • Apr 2007
    • 34

    Write returned XML to a .xml file

    In the example below, how would you write the in-memory xmldoc to a text file? In my real situation, I send a string to a web service (code 2 below) which returns XML/KML that I want to save to a local file.

    from: [HTML]http://www.faqs.org/docs/diveintopython/kgp_parse.html[/HTML]

    Code:
    >>> from xml.dom import minidom                                          
    >>> xmldoc = minidom.parse('~/diveintopython/common/py/kgp/binary.xml')  
    >>> xmldoc                                                               
    <xml.dom.minidom.Document instance at 010BE87C>
    >>> print xmldoc.toxml()                                                 
    <?xml version="1.0" ?>
    <grammar>
    <ref id="bit">
      <p>0</p>
      <p>1</p>
    </ref>
    <ref id="byte">
      <p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/>\
    <xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p>
    </ref>
    </grammar>


    2
    Code:
    # from http://svn.python.org/projects/python/branches/release25-maint/Doc/howto/urllib2.rst
    
    import urllib, urllib2, arcgisscripting
    from xml.dom import minidom
    
    user_address = '8615 Westwood Center Drive, Vienna, VA, 22182'
    url = 'http://xxx.yyy.zzz' #geocoding servlet url - the one I use is internal
    values = {'address': user_address} 
    
    data = urllib.urlencode(values)
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req)
    xmlResults = response.read()
    
    xmldoc = minidom.parseString(xmlResults)
    ##places = xmldoc.getElementsByTagName('Placemark')
    ##
    ##nameTag = places[0].getElementsByTagName('name')
    ##print nameTag[0].firstChild.wholeText
    ##
    ##descriptionTag = places[0].getElementsByTagName('description')
    ##print descriptionTag[0].firstChild.wholeText
    ##
    ##coordinatesTag = places[0].getElementsByTagName('coordinates')
    ##print coordinatesTag[0].firstChild.wholeText
    ##
    ##gp.SetParameterAsText(1,xmlResults)
    
    print xmldoc.toxml()
    Thanks!
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    [CODE=python]
    f = open('filename' , 'wb') #Can be 'w'
    f.write(xmldoc. toxml())
    [/CODE]

    Comment

    • jld730
      New Member
      • Apr 2007
      • 34

      #3
      Originally posted by Laharl
      [CODE=python]
      #Assumes dom is an XML Document object
      f = open('filename' , 'wb') #Can be 'w'
      f.write(dom.tox ml())
      [/CODE]


      I'm not sure I follow this. So with my code2, i tack this
      Code:
      f = open('filename', 'wb')
      f.write(xmldoc.toxml())
      onto the end. It executes, but where is my file saved, what is its name? I'm new to this still, but 'w', 'wb' is not familiar to me at all.

      Thanks for your response.

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        Filename there is a string representation of the file's name where you want the XML to be saved. w vs wb is the mode in which the file is opened. w means write-mode, wb means binary-write mode. If it's written in wb, it should be read with rb.

        Doing it the way you did would create a file in the same directory as the Python script called filename. That was partially my fault for putting quotes around that.

        Comment

        • jld730
          New Member
          • Apr 2007
          • 34

          #5
          Originally posted by jld730
          I'm not sure I follow this. So with my code2, i tack this
          Code:
          f = open('filename', 'wb')
          f.write(xmldoc.toxml())
          onto the end. It executes, but where is my file saved, what is its name? I'm new to this still, but 'w', 'wb' is not familiar to me at all.

          Thanks for your response.

          Sorry, I guess that was fairly stupid of me (forgive me -- boss has had me way over my head these days -- brain is malfunctioning) . I did this code and it works. Still not sure what 'wb' means.

          Code:
          kmlTextFile = "C:\\Temp\\scriptTEST2.kml"
          f = open(kmlTextFile, "w")
          f.write(xmldoc.toxml())

          Comment

          • jld730
            New Member
            • Apr 2007
            • 34

            #6
            Originally posted by Laharl
            Filename there is a string representation of the file's name where you want the XML to be saved. w vs wb is the mode in which the file is opened. w means write-mode, wb means binary-write mode. If it's written in wb, it should be read with wb.

            Doing it the way you did would create a file in the same directory as the Python script called filename. That was partially my fault for putting quotes around that.

            Ahh, OK, thanks for the explanation!!

            Comment

            • jlm699
              Contributor
              • Jul 2007
              • 314

              #7
              Just to fix a minor typo by laharl... when writing with 'wb' (write-binary) you will READ with 'rb' (read-binary).

              Comment

              • Laharl
                Recognized Expert Contributor
                • Sep 2007
                • 849

                #8
                Typo...what typo? I don't see any typo...=P

                Comment

                Working...