Forum OpenACS Development: Re: Export Postgres tables to XML files

Collapse
Posted by Ernie Ghiglione on
Hi Nancy,

As Caroline points out, I've been working exporting data into XML but not necessarily table structure. However, I don't think that will be radically different from what I've been doing.

The data structure for the type of data I'm extracting are very specific and rather cumbersome, so I had a bit of problems to creating XML documents by hand, that is using strings to generate XML. At one time I got really lost in the process so I decided to use tDOM instead.

tDOM is very cool as it allows you to generate XML straight from a list, which is rather nice if you are dealing with complex data structures. There's an excellent posting on the tDOM website about creating XML documents that it was really helpful:

http://groups.yahoo.com/group/tdom/message/423

For what I've been doing, I have set up a set of functions that help me contruct a large XML document very easily. For instance:

ad_proc -public createManifest {
    {-identifier ""}
    {-version ""}
} {
    Creates an IMS manifest

    @param identifier Manifest identifier
    @param version manifest version

} {

    set doc [dom createDocument manifest]
    set manifest [$doc documentElement]

    if {![empty_string_p $identifier]} {
        $manifest setAttribute identifier $identifier
    }

    if {![empty_string_p $version]} {
        $manifest setAttribute version $version
    }

    return $manifest

}

ad_proc -public addOrganizations {
    {-parent:required}
    {-default ""}
} {
    Creates an organizations

    @param parent parent node
    @param default default identifier

} {

    set organizations [$parent appendChild [[$parent ownerDocument] createElement organizations]]

    if {![empty_string_p $default]} {
        $organizations setAttribute default $default
    }

    return $organizations

}

So when I have to create a manifest file (an XML file that contains information and structure about a course), I call the createManifest function passing an identifier and a version, then it returns the tDOM node that I use to insert childnodes to (example using addOrganizations -parent #theManifestnode# ...).

Once you are done creating your XML doc, you can save it to a file or return it to a browser by doing:

ns_return 200 text/xml [$manifest asXML -indent 2 -escapeNonASCII]

(In this case, it returns it as to a browser). $manifest is the manifestsnode.

I hope that helps,

Ernie