473,398 Members | 2,404 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,398 software developers and data experts.

extra xml header with ElementTree?

Hi, I'm using ElementTree which is wonderful. I have a need now to write out
an XML file with these two headers:
<?xml version="1.0" encoding="UTF-8" ?>
<?NLS TYPE="org.eclipse.help.toc"?>

My elements have the root named tocbody and I'm using:
newtree = ET.ElementTree(tocbody)
newtree.write(fname)

I assume if I add the encoding arg I'll get the xml header:
newtree = ET.ElementTree(tocbody)
newtree.write(fname,encoding='utf-8')

but how can I get the <?NLS TYPE="org.eclipse.help.toc"?into the tree?

python2.4.1,hpux10,ElementTree1.2.6

thanks,
--Tim
May 25 '07 #1
6 11603
On May 25, 3:55 pm, "Tim Arnold" <tia...@sas.comwrote:
Hi, I'm using ElementTree which is wonderful. I have a need now to write out
an XML file with these two headers:
<?xml version="1.0" encoding="UTF-8" ?>
<?NLS TYPE="org.eclipse.help.toc"?>

My elements have the root named tocbody and I'm using:
newtree = ET.ElementTree(tocbody)
newtree.write(fname)

I assume if I add the encoding arg I'll get the xml header:
newtree = ET.ElementTree(tocbody)
newtree.write(fname,encoding='utf-8')

but how can I get the <?NLS TYPE="org.eclipse.help.toc"?into the tree?

python2.4.1,hpux10,ElementTree1.2.6
#This import is for 2.5, change for 2.4
from xml.etree import cElementTree as ET

tocbody = '<toc><item>one</item><item>two</item></toc>'

doc = ET.ElementTree(ET.fromstring(tocbody))

outfile = open('\\working\\tmp\\toctest.xml', 'w')

outfile.write('<?xml version="1.0" encoding="UTF-8" ?>')

outfile.write('<?NLS TYPE="org.eclipse.help.toc"?>')

doc._write(outfile, doc._root, 'utf-8', {})

outfile.close()

-----------------

<?xml version="1.0" encoding="UTF-8" ?>
<?NLS TYPE="org.eclipse.help.toc"?>
<toc>
<item>one</item>
<item>two</item>
</toc>

May 25 '07 #2
"Gerard Flanagan" <gr********@yahoo.co.ukwrote in message
news:11*********************@u30g2000hsc.googlegro ups.com...
On May 25, 3:55 pm, "Tim Arnold" <tia...@sas.comwrote:
>Hi, I'm using ElementTree which is wonderful. I have a need now to write
out
an XML file with these two headers:
<?xml version="1.0" encoding="UTF-8" ?>
<?NLS TYPE="org.eclipse.help.toc"?>

My elements have the root named tocbody and I'm using:
newtree = ET.ElementTree(tocbody)
newtree.write(fname)

I assume if I add the encoding arg I'll get the xml header:
newtree = ET.ElementTree(tocbody)
newtree.write(fname,encoding='utf-8')

but how can I get the <?NLS TYPE="org.eclipse.help.toc"?into the tree?

python2.4.1,hpux10,ElementTree1.2.6

#This import is for 2.5, change for 2.4
from xml.etree import cElementTree as ET

tocbody = '<toc><item>one</item><item>two</item></toc>'

doc = ET.ElementTree(ET.fromstring(tocbody))

outfile = open('\\working\\tmp\\toctest.xml', 'w')

outfile.write('<?xml version="1.0" encoding="UTF-8" ?>')

outfile.write('<?NLS TYPE="org.eclipse.help.toc"?>')

doc._write(outfile, doc._root, 'utf-8', {})

outfile.close()

-----------------

<?xml version="1.0" encoding="UTF-8" ?>
<?NLS TYPE="org.eclipse.help.toc"?>
<toc>
<item>one</item>
<item>two</item>
</toc>
thanks, this works well. After looking at the ET code, I just used the
'write' method straight since it calls _write in turn.

thanks again,
--Tim
May 25 '07 #3
Hello

I've got a web application with the following structure:

1) module of 100 functions corresponding to user actions (e.g.
"update_profile()", "organisations_list()")
2) a wsgi callable which maps urls to functions eg
/organisations/list/?sort=date_created is mapped to
organisations_list("dateCreated")
3) mapping is performed using inspect.getargspec()
4) a bunch of html generating templates

In the templates I want to generate urls by referencing the function to
which they map, rather than the url, e.g.

<a href="${organisations_list('dateCreated'})'">Sort By Date Created</a>

In other words, I want to always refer to functions, rather than mixing
up function calls and urls

I would like a class that proxies all the 100 functions in the user
actions module. When a proxied function is called via this class it
should return the url to which it is mapped rather than executing the
user action function.

<a href="${proxyclass.organisations_list('dateCreated ')}">Sort By Date
Created</a>

should produce:

<a href="/organisations/list/?sort=date_created">Sort By Date Created</a>

Obviously, I don't want to write and maintain copies of these 100
functions in another class.

My question is therefore: what is the best way to proxy these 100 functions?

Thanks

Tim Arnold wrote:
Hi, I'm using ElementTree which is wonderful. I have a need now to write out
an XML file with these two headers:
<?xml version="1.0" encoding="UTF-8" ?>
<?NLS TYPE="org.eclipse.help.toc"?>

My elements have the root named tocbody and I'm using:
newtree = ET.ElementTree(tocbody)
newtree.write(fname)

I assume if I add the encoding arg I'll get the xml header:
newtree = ET.ElementTree(tocbody)
newtree.write(fname,encoding='utf-8')

but how can I get the <?NLS TYPE="org.eclipse.help.toc"?into the tree?

python2.4.1,hpux10,ElementTree1.2.6

thanks,
--Tim

May 25 '07 #4
Kind and wise fellows,

I've got a web application with the following structure:

1) module of 100 functions corresponding to user actions (e.g.
"update_profile()", "organisations_list()")
2) a wsgi callable which maps urls to functions eg
/organisations/list/?sort=date_created is mapped to
organisations_list("dateCreated")
3) mapping is performed using inspect.getargspec()
4) a bunch of html generating templates

In the templates I want to generate urls by referencing the function to
which they map, rather than the url, e.g.

<a href="${organisations_list('dateCreated'})'">Sort By Date Created</a>

In other words, I want to always refer to functions, rather than mixing
up function calls and urls

I would like a class that proxies all the 100 functions in the user
actions module. When a proxied function is called via this class it
should return the url to which it is mapped rather than executing the
user action function.

<a href="${proxyclass.organisations_list('dateCreated ')}">Sort By Date
Created</a>

should produce:

<a href="/organisations/list/?sort=date_created">Sort By Date Created</a>

Obviously, I don't want to write and maintain copies of these 100
functions in another class.

My question is therefore: what is the best way to proxy these 100 functions?

Thanks

Tim Arnold wrote:
Hi, I'm using ElementTree which is wonderful. I have a need now to write out
an XML file with these two headers:
<?xml version="1.0" encoding="UTF-8" ?>
<?NLS TYPE="org.eclipse.help.toc"?>

My elements have the root named tocbody and I'm using:
newtree = ET.ElementTree(tocbody)
newtree.write(fname)

I assume if I add the encoding arg I'll get the xml header:
newtree = ET.ElementTree(tocbody)
newtree.write(fname,encoding='utf-8')

but how can I get the <?NLS TYPE="org.eclipse.help.toc"?into the tree?

python2.4.1,hpux10,ElementTree1.2.6

thanks,
--Tim
May 25 '07 #5
Tim Arnold wrote:
Hi, I'm using ElementTree which is wonderful. I have a need now to write out
an XML file with these two headers:
<?xml version="1.0" encoding="UTF-8" ?>
<?NLS TYPE="org.eclipse.help.toc"?>

My elements have the root named tocbody and I'm using:
newtree = ET.ElementTree(tocbody)
newtree.write(fname)

I assume if I add the encoding arg I'll get the xml header:
newtree = ET.ElementTree(tocbody)
newtree.write(fname,encoding='utf-8')

but how can I get the <?NLS TYPE="org.eclipse.help.toc"?into the tree?
Try

ET.ProcessingInstruction("NLS", 'TYPE="..."')

Or try lxml.etree instead, it's ET compatible but has very good support for
PIs starting with 1.3beta.

http://codespeak.net/lxml/dev/

Stefan
May 25 '07 #6
Josh West wrote:
Kind and wise fellows,
[...]
Please see my separate reply with the same subject line but in a new
thread. That reply explains *why* it's in a new thread.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

May 25 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Stewart Midwinter | last post by:
I want to parse a file with ElementTree. My file has the following format: <!-- file population.xml --> <?xml version='1.0' encoding='utf-8'?> <population> <person><name="joe" sex="male"...
1
by: Greg Wilson | last post by:
I'm trying to convert from minidom to ElementTree for handling XML, and am having trouble with entities in DTDs. My Python script looks like this: ...
1
by: mirandacascade | last post by:
O/S: Windows 2K Vsn of Python: 2.4 Currently: 1) Folder structure: \workarea\ <- ElementTree files reside here \xml\ \dom\
0
by: Greg Aumann | last post by:
I am trying to write some python code for a library that reads an XML-like language from a file into elementtree data structures. Then I want to be able to read and/or modify the structure and then...
2
by: mirandacascade | last post by:
Situation is this: 1) I have inherited some python code that accepts a string object, the contents of which is an XML document, and produces a data structure that represents some of the content of...
8
by: Craig | last post by:
Hi there, I'm only new to Python so please bear with me. I using ElementTree to generate an XML file that will reference a DTD and an XSL file. The header information I want at the start of...
5
by: saif.shakeel | last post by:
#!/usr/bin/env python from elementtree import ElementTree as Element tree = et.parse("testxml.xml") for t in tree.getiterator("SERVICEPARAMETER"): if t.get("Semantics") == "localId":...
1
by: Mike Slinn | last post by:
The following short Python program parses a KML file and displays the names of all Marks and Routes: from elementtree.ElementTree import ElementTree tree = ElementTree(file='test.kml') kml =...
3
by: gray.bowman | last post by:
I'm messing around with trying to write an xml file using xml.etree.ElementTree. All the examples on the internet show the use of ElementTree.write(), although when I try to use it it's not...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.