Using an agent to generate XML

One of the biggest advantages of using an agent to generate XML is flexibility. Agents can be set to run on a schedule, based on an event, or in response to a URL command. This kind of flexibility is necessary to create automated XML applications. For example, the Web site for a bookstore contains a database where customers contribute articles to the monthly newsletter. There is an editing and approval workflow process that moves the articles through the system until they have been approved. An agent runs each hour to collect the articles that are ready for publication and converts them into XML. The agent then places the articles into another database as static XML documents where they are collected by subscribers.

The ROI Books application contains an agent called createXML which generates XML for each document in a view and sends it out based on a request from a browser or a server. To see the output of this agent, open the ROI Books application in Microsoft Internet Explorer 5 and click the XML Agent link, or run the agent using the OpenAgent URL command:

   http://host/roiBooks.nsf/createXML?OpenAgent

Image of XML agent

The agent could be written to not only print the output, but also to store XML output in a string variable and write it to a static XML document or to another database system using LS:DO or the DECS connector APIs.

Example: XML Agent

This example is a LotusScript® agent that retrieves each document in a view called XML, creates XML from the content, and prints the output.

   Dim s As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim view As NotesView

Set db = s.currentDatabase
Set view = db.GetView( "XML" )
Set doc = view.GetFirstDocument

Print "Content-type: text/xml"
   'Prevents Domino from sending default headers.
Print "<BOOKCATALOG>"
   'BOOKCATALOG is the root element of the XML document.
   

While Not ( doc Is Nothing )
   'Loop as long as there are document objects available.
      Print  "<BOOK>"
      'Send the parent element for each book document.
   Print "<bookTitle>"+doc.bookTitle(0)+"</bookTitle>"
   Print "<bookAuthor>"+doc.bookAuthor(0)+"</bookAuthor>"
   Print "<bookPrice>"+doc.bookDiscountPrice(0)+"</bookPrice>"
   Print "<bookCategory>"+doc.bookCategory(0)+"</bookCategory>"
   Print "</BOOK>"
      'Close the book element tag.
   Set doc = view.GetNextDocument( doc )
      'Get the next document in the view.
Wend
Print "</BOOKCATALOG>"
   'Closes the root element.