Re: Pyxml: DOM questions, problems with namespaces

Mike Brown <[email protected]>
Newsgroups gmane.comp.python.xml
Message-ID <[email protected]>
Matthias Dorfner wrote:
> 1. some problems creating my xml file with the
> correct double quoted string in the namespace, here one example:
> 
> <?xml version='1.0' encoding='UTF-8'?>
> <request xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
> xmlns:noNameSpaceSchemaLocation='Handle.xsd'>
> 
> I need exactly this output but not single quoted(' -> "). Here's the code

The type of quotes used on attribute values during serialization
depends on the serializer.

If you use the .toxml() method to serialize the document, you'll get
double quotes, I believe. It won't pretty-print though.

print doc.toxml()

<?xml version="1.0"?><request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNameSpaceSchemaLocation="Handle.xsd"/>

xml.dom.ext.Print and PrettyPrint haven't been updated in ages.
Well, they've been updated, but only in 4Suite (where they manifest as
Ft.Xml.Domlette.Print and PrettyPrint), not PyXML. In PyXML they're
hard-coded to use single quotes as attribute delimiters.

I asked on the list a while back if there was any interest in bringing
4Suite's Print/PrettyPrint implementation into PyXML and didn't get much
response. It's the kind of thing where if I want to see it done, I have to
submit patches.

> I use to create this one:
> 
> dom = xml.dom.minidom.getDOMImplementation()
> doc = dom.createDocument(None, "request", None)
> 
> #Get the document element
> msg_elem = doc.documentElement
> 
> #Create an xmlns attributes on the root element
> msg_elem.setAttributeNS(EMPTY_NAMESPACE, "xmlns:xsi",
> "http://www.w3.org/2001/XMLSchema-instance")
> msg_elem.setAttributeNS(EMPTY_NAMESPACE, "xsi:noNameSpaceSchemaLocation",
> "Handle.xsd")

This isn't related to your quoting problem, but you are using the
wrong namespaces. Instead of EMPTY_NAMESPACE you need to do it like this:

msg_elem.setAttributeNS(XMLNS_NAMESPACE, 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance')

msg_elem.setAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xsi:noNameSpaceSchemaLocation','Handle.xsd')

> 2. I want so post the above created xml structure to a webserver, but I
> need to convert this first to a string, how can I do this? PrettyPrint
> allows only writing this DOM structure to a file. Or is it possible to
> correctly read out this xml file?

A "file" in Python is any file-like object, basically anything with a .read()
and maybe also .write() method for byte strings. This includes the sys.stdin,
stdout, stderr streams.

So to pretty-print to the screen, you could just do

PrettyPrint(doc, sys.stdout)

And to print to a buffer rather than an external file:

f = cStringIO.StringIO()
PrettyPrint(doc, f)

To read from that buffer into a string, s:

s = f.getvalue()

Alternatively:

f.reset()
s = f.read()

To read from an external file:

f = open('yourfile', 'rb')
s = f.read()
f.close()

Always be sure to call close() on your file-like objects when you're done
reading or writing to them. (though not really necessary on sys.stdin, stdout,
stderr)

I don't know what web API you're using, but if you have access to an
object representing the HTTP request, it might have a method that
reads from a stream, in which case you could do something like

PrettyPrint(doc, request.stream)

to print directly to into the request object.

Mike
_______________________________________________
XML-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/xml-sig
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.