[Twisted-Python] regarding xml elements

Phil Christensen phil at bubblehouse.org
Fri Mar 28 10:32:44 EDT 2008


On Mar 28, 2008, at 7:44 AM, Raj kumar wrote:
> Hi,
> I have an xml file in my application,
> I have created an element using
> Example goes like this.........
>
> document.createElement("abc")
> and i appeneded it by using append() method.
> But how i can reflect this change to my xml file?
>  and one more thing is i want to create element with some other  
> parameters....
> like..............
>
>   <abc   m=" "  n=" ">
>     </abc>
> and i have m and n values as strings with me..
> can anybody help me to create this element and write it to the  
> existing xml file
> as a child of an existing element?

It sounds like you're working with t.w.microdom. I believe you'll want  
to use Document.writexml(). It's not documented in the code, and I  
haven't looked anywhere else, but you should be able to use it like  
this:

     def writexml(self, stream, indent='', addindent='', newl='',  
strip=0,
                  nsprefixes={}, namespace=''):

You'll probably want to ignore most of the parameters. I'm fairly sure  
indent, addindent,newl, and strip are related to pretty-printing. I've  
never used the namespace-related vars either, so I can't really tell  
you what they do ;-)...

Also, in case it's not obvious, 'stream' should be a file object of  
some kind.

To add attributes to your element, you can invoke createElement like  
this:

     document.createElement('abc', attributes={'m':'mvalue',  
'n':'nvalue'})

Then just append() it normally as you've already done.

-phil




More information about the Twisted-Python mailing list