[XML-SIG] Howto create this XML string?

Sbaush sbaush at gmail.com
Fri Jan 20 11:30:27 CET 2006


hi, i've tested the matthew way, and it work perfectly.
Is possible to have the XML in a string (for example xmlstring) and for
printing do a
print xmlstring ?

The Matthew way is

import sys
import elementtree.ElementTree as ET

root = ET.Element("manager")
req=ET.SubElement(root,"request")
app=ET.SubElement(req,"append")
app.set("mode","INPUT")
met=ET.SubElement(app,"method")
met.set("type","GOOD")
src=ET.SubElement(app,"source")
src.set("address","127.0.0.1 ")
act=ET.SubElement(app,"action")
act.set("option","OK")

tree = ET.ElementTree(root)
tree.write(sys.stdout)
print

$ python et.py
<manager><request><append mode="INPUT"><method type="GOOD" /><source
address=" 127.0.0.1" /><action option="OK"
/></append></request></manager>


2006/1/19, Matthew Dixon Cowles < matt at mondoinfo.com>:
>
> Dear Sbaush,
>
> > Hi all.
> > I've this XML:
> >
> > <manager>
> >     <request>
> >         <append mode="INPUT">
> >             <method type="GOOD"/>
> >             <source address=" 127.0.0.1"/>
> >             <action option="OK"/>
> >         </append>
> >     </request>
> > </manager>
> >
> > How can i write this in a Python String? I would like use a
> > xml.domimplementation and not a banal print...print...
>
> Using Fredrik Lundh's ElementTree from:
>
> http://effbot.org/zone/element-index.htm
>
> you could do something like:
>
> import sys
> import elementtree.ElementTree as ET
>
> root = ET.Element("manager")
> req=ET.SubElement(root,"request")
> app=ET.SubElement(req,"append")
> app.set("mode","INPUT")
> met=ET.SubElement(app,"method")
> met.set("type","GOOD")
> src=ET.SubElement(app,"source")
> src.set("address"," 127.0.0.1")
> act=ET.SubElement(app,"action")
> act.set("option","OK")
>
> tree = ET.ElementTree(root)
> tree.write(sys.stdout)
> print
>
> $ python et.py
> <manager><request><append mode="INPUT"><method type="GOOD" /><source
> address="127.0.0.1" /><action option="OK"
> /></append></request></manager>
>
> Regards,
> Matt
>
>


--
Sbaush

--
Sbaush
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/xml-sig/attachments/20060120/8437be0d/attachment-0001.html 


More information about the XML-SIG mailing list