[XML-SIG] Serializing DOM tree to file
Rich Salz
rsalz@datapower.com
Fri, 16 May 2003 19:44:54 -0400 (EDT)
On Fri, 16 May 2003, Nickolay Kolev wrote:
> Basically, what I want to do is to have what PrettyPrint from
> xml.dom.ext writes to the console. But I want it in a file.
I was going to say that PrettyPrint is pretty ugly, because it moves
all the namespace declarations up to the outermost element.
Well it's worse than ugly, it's seriously broken. It moves the default
namespace declaration to the top. Here's my prettyprint script:
; cat $h/bin/xml/pp
#! /usr/bin/env python2
import sys
from xml.dom.ext.reader import PyExpat
from xml.dom.ext import PrettyPrint
r = PyExpat.Reader()
dom = r.fromStream(sys.stdin)
PrettyPrint(dom)
Here's the input:
; cat x.xml
<S:foo xmlns:S="http://example.com/1">
<bar>test</bar>
<bar xmlns='http://example.com/2'>test2</bar>
<S:bar xmlns:S='http://example.com/2'>test2</S:bar>
</S:foo>
And the output:
<?xml version='1.0' encoding='UTF-8'?>
<S:foo xmlns:S='http://example.com/1' xmlns='http://example.com/2'>
<bar>test</bar>
<bar>test2</bar>
<S:bar xmlns:S='http://example.com/2'>test2</S:bar>
</S:foo>
Seriously broken!
from xml.com.ext.c14n import Canonicalize
s = Canonicalize(node) # returns output as a string
Canonicalize(node, sys.stdout) # anything with a write() method
--
Rich Salz Chief Security Architect
DataPower Technology http://www.datapower.com
XS40 XML Security Gateway http://www.datapower.com/products/xs40.html