By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,091 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,091 IT Pros & Developers. It's quick & easy.

extra xml header with ElementTree?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.