Forum OpenACS Development: Generating an XML file from a Query

Collapse
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

Collapse
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

Collapse
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:

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