[XML-SIG] Howto create this XML string?
Michael McLay
mmclay at comcast.net
Thu Jan 19 18:50:27 CET 2006
On Thursday 19 January 2006 11:19, Sbaush wrote:
> 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...
> I can't do it... Is there anyone that can explain me what is the way?
> Thank you!
This is pretty easy to do using ElementTree and cEleementTree. (These packages
will be included in the standard Python distribution starting with 1.5.)
Here's a script that shows three ways of doing what you requested.
import sys
import cElementTree as ET
root = ET.Element('manager')
request = ET.SubElement(root, "request")
append = ET.SubElement(request, "append")
append.set('mode','INPUT')
method = ET.SubElement(append, "method")
method.set('type','GOOD')
source = ET.SubElement(method, "source")
source.set('address','127.0.0.1')
action = ET.SubElement(method, "action")
action.set('option','OK')
tree = ET.ElementTree(root)
tree.write(sys.stdout)
print '\n\n\n next add some white space for pretty printing\n'
root = ET.Element('manager')
root.text='\n '
request = ET.SubElement(root, "request")
request.text='\n '
request.tail='\n'
append = ET.SubElement(request, "append")
append.set('mode','INPUT')
append.text= '\n '
append.tail= '\n '
method = ET.SubElement(append, "method")
method.set('type','GOOD')
method.tail='\n '
source = ET.SubElement(append, "source")
source.set('address','127.0.0.1')
source.tail= '\n '
action = ET.SubElement(append, "action")
action.set('option','OK')
action.tail='\n '
tree = ET.ElementTree(root)
tree.write(sys.stdout)
# the trailing tags can also be
print "\n\n\nHere's an example from http://effbot.org/zone/xml-writer.htm\n"
from elementtree.SimpleXMLWriter import XMLWriter
import sys
w = XMLWriter(sys.stdout)
html = w.start("html")
w.start("head")
w.element("title", "my document")
w.element("meta", name="generator", value="my application 1.0")
w.end()
w.start("body")
w.element("h1", "this is a heading")
w.element("p", "this is a paragraph")
w.start("p")
w.data("this is ")
w.element("b", "bold")
w.data(" and ")
w.element("i", "italic")
w.data(".")
w.end("p")
w.close(html)
This script produces the following output:
<manager><request><append mode="INPUT"><method type="GOOD"><source
address="127.0.0.1" /><action
option="OK" /></method></append></request></manager>
next add some white space for pretty printing
<manager>
<request>
<append mode="INPUT">
<method type="GOOD" />
<source address="127.0.0.1" />
<action option="OK" />
</append>
</request>
</manager>
Here's a different example from http://effbot.org/zone/xml-writer.htm
<html><head><title>my document</title><meta name="generator" value="my
application 1.0" /></head><body><h1>this is a heading</h1><p>this is a
paragraph</p><p>this is <b>bold</b> and <i>italic</i>.</p></body></html>
More information about the XML-SIG
mailing list