Forum OpenACS Development: Generating an XML file from a Query

Posted by Juan Carlos on
In need to create an XML file from the data I got from a query to a Database.

Is there any procedure in OpenACS that I can use to generate the XML file

Posted by Antonio Pisano on
The most common library tool to manipulate XML in Tcl is tDOM, but you might not need all that.

Something I have found quite convenient sometimes is to get my data from the db by a db_multirow, then format it in the adp as xml taking advantage of the "multiple", "if" and other special tags.

Hope this helps

Posted by Neophytos Demetriou on
Not sure there is a procedure to do it for you but you can come up with one using tdom by following the example below:

set records {{{Ayn Rand} {Atlas Shrugged}} {{Leo Tolstoy} {War and Peace}}}
set doc [dom createDocument "library"]
set root [$doc documentElement]
foreach rec $records {
  lassign $rec author title
  set el [$doc createElement "book"]
  set field1 [$doc createElement "author"]
  set text1 [$doc createTextNode $author]
  $field1 appendChild $text1
  set field2 [$doc createElement "title"]
  set text2 [$doc createTextNode $title]
  $field2 appendChild $text2
  $el appendChild $field1
  $el appendChild $field2
  $root appendChild $el
puts [$doc asXML]
Here's what the output looks like:

        <author>Ayn Rand</author>
        <title>Atlas Shrugged</title>
        <author>Leo Tolstoy</author>
        <title>War and Peace</title>
Of course, you can just append to string. You could also use appendFromScript (which I prefer) but it is somewhat more cumbersome to setup.
Posted by Juan Carlos on
Thanks that will do.