[XML-SIG] Getting Started
Martin v. Loewis
martin@v.loewis.de
Sat, 17 Nov 2001 07:37:32 +0100
> My main question(s) at the moment is (are): how to create new xml
> documents, new elements, new attributes (like in database terms
> "creating a new record")?
Hi Albert,
Asking n XML experts, you'll get probably n+1 or more answers. The
typical ones are
1. Just use print statements. This is what I normally recommend to
create an XML document "from scratch", given only the data
that go into it:
print "<mycontent>Hello, world</mycontent>"
produces a well-formed XML document.
2. Have something emit SAX events, and use a SAX XML writer. This is
suitable in particular if you want to stream-modify an existing
document, but also works when creating documents from scratch.
Essentially, you have to emit all events that a parser would emit,
which might not be easy. Likewise, to add new attributes, you
derive from SAXFilterBase, set the SAX filter as content, error,
etc handler in the parser, and watch for the elements where
you want to create new attributes. Then you modify the attributes,
and pass the modified attributes on to the "final" content handler,
which may be a writer.
3. Create a DOM tree, modify it, and write it back.
You can create a fresh DOM tree by calling createDocument on
the dom implementation.
HTH,
Martin