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] - >>> 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 - # 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!
7 2189 -
f = open('filename', 'wb') #Can be 'w'
-
f.write(xmldoc.toxml())
-
-
#Assumes dom is an XML Document object
-
f = open('filename', 'wb') #Can be 'w'
-
f.write(dom.toxml())
-
I'm not sure I follow this. So with my code2, i tack this - 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.
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.
I'm not sure I follow this. So with my code2, i tack this - 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. - kmlTextFile = "C:\\Temp\\scriptTEST2.kml"
-
f = open(kmlTextFile, "w")
-
f.write(xmldoc.toxml())
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!!
Just to fix a minor typo by laharl... when writing with 'wb' (write-binary) you will READ with 'rb' (read-binary).
Typo...what typo? I don't see any typo...=P
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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)...
|
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...
|
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',...
|
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:
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
| |