Using Caché XML Tools
Serving XML from Caché
[Back] [Next]
   
Server:docs1
Instance:LATEST
User:UnknownUser
 
-
Go to:
Search:    

Caché can serve XML data in a variety of ways by making use of the tools given in the previous chapter, as well as using Caché server pages. This chapter covers the following topics:

You can also use Caché to create web services. For more information, see Creating Web Services and Web Clients in Caché.
Overview of Serving XML
To serve XML from Caché server pages (CSP), the requirements are as follows:
Creating a Simple Caché XML Server
This section demonstrates a simple HTTP-based XML server that serves static XML content in response to incoming HTTP requests. The XML server is a subclass of the %CSP.Page class that has been extended to serve XML instead of HTML. Such a server class looks like this:
Class GXML.Server Extends %CSP.Page
{

Parameter CONTENTTYPE = "text/xml";

ClassMethod OnPage() As %Status
{
   write "<?xml version=""1.0""?>"
   write "<Person><Name>Jaynes,Janice Q.</Name>"
   write "<DOB>1959-09-03</DOB></Person>"
   quit $$$OK
}

}
This class extends the %CSP.Page class, overrides the CONTENTTYPE parameter to "text/xml" and overrides the OnPage() method to write out XML content (in this case, static content).
How the Example Behaves
If you have written a sample like the preceding, you can view it in two ways:
Depending on your browser, you see either an XML tree or just text. In either case, you can use the View Source command in the browser to display the actual XML sent.
Creating the Example with the New Class Wizard
The New Class Wizard in Studio makes it easy to create code like the preceding, if you forget the details. The procedure is as follows:
  1. Select File > New.
  2. On the first page of the wizard, enter a package name and a class name.
  3. On the second page of the wizard, specify that the new class definition is a CSP page.
  4. On the third page of the wizard, specify that this CSP page serves XML.
  5. Select Finish.
  6. At this point, Studio displays a new class definition similar to this:
    Class GXML.Server Extends %CSP.Page
    {
    Parameter CONTENTTYPE = "text/xml";
    
    ClassMethod OnPage() As %Status
    {
        Write "<?xml version=""1.0"" ?>",!
        Quit $$$OK
    }
    }
  7. Then edit the OnPage() method as needed.
Serving Objects as XML from CSP
Rather than writing static XML as in the previous example, it would be more interesting to write an XML document that contains the contents of a Caché object. In order to do this, the object class must be XML-enabled; that is, it must extend %XML.Adaptor and specify any needed settings such as element names (if the defaults are not wanted). See Projecting Objects to XML.
You would also modify the OnPage() method to generate XML output, as described in Writing XML Output from Caché Objects.” The OnPage() method should create an instance of %XML.Writer and invoke its methods to generate the output.
Modifying the XML Server Class
This section shows how a XML server class can serve XML generated from persistent objects. Here the OnPage() method writes one instance of an XML-enabled class (GXML.Person1) as an XML document. By default, %XML.Writer generates output to the current device; if called from a browser, that means the output goes to the browser
Class GXML.Server2 Extends %CSP.Page
{

Parameter CONTENTTYPE = "text/xml";

ClassMethod OnPage() As %Status
{
    set writer=##class(%XML.Writer).%New()
    set writer.Indent=1
    set obj=##class(GXML.Person1).%OpenId(1)
    set status=writer.RootObject(obj)
    if $$$ISERR(status) { quit $$$ERROR($$$GeneralError,"Error writing root object") }
    quit $$$OK
}

}
View the results by requesting the following URL from a browser:
http://127.0.0.1/csp/samples/GXML.Server2.cls
As in the previous example, this serves an XML document to the browser. In this case, the document you see corresponds to the instance of GXML.Person1 with the ID equal to 1. A more robust server could let the user choose among the existing stored objects and then would display the selected one.