473,395 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Write returned XML to a .xml file

34
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]

Expand|Select|Wrap|Line Numbers
  1. >>> from xml.dom import minidom                                          
  2. >>> xmldoc = minidom.parse('~/diveintopython/common/py/kgp/binary.xml')  
  3. >>> xmldoc                                                               
  4. <xml.dom.minidom.Document instance at 010BE87C>
  5. >>> print xmldoc.toxml()                                                 
  6. <?xml version="1.0" ?>
  7. <grammar>
  8. <ref id="bit">
  9.   <p>0</p>
  10.   <p>1</p>
  11. </ref>
  12. <ref id="byte">
  13.   <p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/>\
  14. <xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p>
  15. </ref>
  16. </grammar>


2
Expand|Select|Wrap|Line Numbers
  1. # from http://svn.python.org/projects/python/branches/release25-maint/Doc/howto/urllib2.rst
  2.  
  3. import urllib, urllib2, arcgisscripting
  4. from xml.dom import minidom
  5.  
  6. user_address = '8615 Westwood Center Drive, Vienna, VA, 22182'
  7. url = 'http://xxx.yyy.zzz' #geocoding servlet url - the one I use is internal
  8. values = {'address': user_address} 
  9.  
  10. data = urllib.urlencode(values)
  11. req = urllib2.Request(url, data)
  12. response = urllib2.urlopen(req)
  13. xmlResults = response.read()
  14.  
  15. xmldoc = minidom.parseString(xmlResults)
  16. ##places = xmldoc.getElementsByTagName('Placemark')
  17. ##
  18. ##nameTag = places[0].getElementsByTagName('name')
  19. ##print nameTag[0].firstChild.wholeText
  20. ##
  21. ##descriptionTag = places[0].getElementsByTagName('description')
  22. ##print descriptionTag[0].firstChild.wholeText
  23. ##
  24. ##coordinatesTag = places[0].getElementsByTagName('coordinates')
  25. ##print coordinatesTag[0].firstChild.wholeText
  26. ##
  27. ##gp.SetParameterAsText(1,xmlResults)
  28.  
  29. print xmldoc.toxml()
Thanks!
May 21 '08 #1
7 2189
Laharl
849 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. f = open('filename', 'wb') #Can be 'w'
  2. f.write(xmldoc.toxml())
  3.  
May 21 '08 #2
jld730
34
Expand|Select|Wrap|Line Numbers
  1. #Assumes dom is an XML Document object
  2. f = open('filename', 'wb') #Can be 'w'
  3. f.write(dom.toxml())
  4.  


I'm not sure I follow this. So with my code2, i tack this
Expand|Select|Wrap|Line Numbers
  1. f = open('filename', 'wb')
  2. 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.
May 21 '08 #3
Laharl
849 Expert 512MB
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.
May 21 '08 #4
jld730
34
I'm not sure I follow this. So with my code2, i tack this
Expand|Select|Wrap|Line Numbers
  1. f = open('filename', 'wb')
  2. 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.

Expand|Select|Wrap|Line Numbers
  1. kmlTextFile = "C:\\Temp\\scriptTEST2.kml"
  2. f = open(kmlTextFile, "w")
  3. f.write(xmldoc.toxml())
May 21 '08 #5
jld730
34
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!!
May 21 '08 #6
jlm699
314 100+
Just to fix a minor typo by laharl... when writing with 'wb' (write-binary) you will READ with 'rb' (read-binary).
May 21 '08 #7
Laharl
849 Expert 512MB
Typo...what typo? I don't see any typo...=P
May 21 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
6
by: haynesc | last post by:
Hi, I'm having a problem where when trying to open a file in write mode, I get an IOError stating no such file or directory. I'm calling an external program which takes an input file and...
0
by: Zed | last post by:
I have a problem with .net writing to a remote server that I need to deploy on. The app works fine on my local machine. But when got access to the server I went to VS.NET and tried to create a...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
0
by: psy000 | last post by:
Hi, I have a C# web service client that talks to a JAVA application sever. I use AXIS to generate the WSDL file, use wsdl.exe to generate proxy stub c# code. When I try to use c# client connect...
3
by: collinm | last post by:
hi i send a command to a led display, the led display is suppose to return me some character i write a string on a serial port void ledDisplayExist() { char msg={'\0', '\0', '\0', '\0',...
9
by: msuk | last post by:
All, I have a well form block of XML that is stored in a C# string type and I just simply want to display it in the browser using Response.Write but when I try this I get the following error: ...
7
by: nass | last post by:
hi all, i am running slackware linux and need to use some function that will will enable me to write and read from a shared mem segment.. i am using open() , to open a file, and then use mmap to...
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.