writing xml tree

Paul Boddie paul at boddie.net
Mon Nov 24 06:50:49 EST 2003


david farning <dfarning at localhost.localdomain> wrote in message news:<PzQvb.4886$aw2.1345162 at newssrv26.news.prodigy.com>...
> 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




More information about the Python-list mailing list