extra xml header with ElementTree?

T

Tim Arnold

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
 
G

Gerard Flanagan

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>
 
J

Josh West

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
 
T

Tim Arnold

Gerard Flanagan said:
#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
 
J

Josh West

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
 
S

Stefan Behnel

Tim said:
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
 
S

Steve Holden

Josh said:
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 ----------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top