writing xml tree

D

david farning

I am doing my first work in python/xml

Is is possiable to write a xmlTree in a single command?
for example

doc = libxml2.parseFile (filename)
# add node to doc

doc.write(filename) ??????????????

I have seen several examles writing one element or line at a time, but never
the whole tree.

Thanks
Dave Farning
 
P

Paul Boddie

david farning said:
I am doing my first work in python/xml

Is is possiable to write a xmlTree in a single command?
for example

doc = libxml2.parseFile (filename)
# add node to doc

doc.write(filename) ??????????????

I have seen several examles writing one element or line at a time, but never
the whole tree.

You can use the serialize method on the document node to get the
document as text (or on any other node to get a document fragment as
text):

s = doc.serialize()
# Use normal file writing mechanisms.

Or you could use the saveFile method to write to a file:

doc.saveFile(filename)

I'd recommend using Python interactively to see which methods are
available on the document node:

import libxml2
doc = libxml2.parseFile(filename)
dir(doc)

Indeed, until I started responding to your question, I hadn't noticed
saveFile, so there's a lot to discover. ;-)

Paul
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top