Author Info
Chris Malek

Chris Malek is a PeopleTools® Technical Consultant with two decades of experience working on PeopleSoft enterprise software projects. He is available for consulting engagements.

About Chris Work with Chris
Do you need a quick way to create web services?
PeopleSoft Simple Web Services (SWS)

Introducing a small but powerful PeopleSoft bolt-on that makes web services very easy. If you have a SQL statement, you can turn that into a web service in PeopleSoft in a few minutes.

Read More & Purchase

XML Encoding Examples

In this section, we will look at how to create XML with PeopleCode. We do not use the message class to generate any XML. The PeopleCode is used to entirely generate the content.

Simple XML Output

This example shows how to produce a simple XML output in an handler. Below is the XML produced with the handler PeopleCode that is below. This is meant to demonstrate a few key concepts.

  • Producing an nested XML structure
  • Adding XML attributes to node
  • Show the different with character escaping differences between CDATA and Nodes
<?xml version="1.0"?>
  <hello DEBUG_NODE="True">world</hello>
    <![CDATA[<p><b>I have characters that should be escaped</b></p>]]>
  <STRING_TEST>&lt;p&gt;&lt;b&gt;I have characters that should be escaped&lt;/b&gt;&lt;/p&gt;</STRING_TEST>
  <META isMETA="True">

The full handler PeopleCode to produce this XML is the following.

import PS_PT:Integration:IRequestHandler;

class xmlOutputExample1 implements PS_PT:Integration:IRequestHandler
   method onRequest(&msRequest As Message) Returns Message;

method onRequest
   /+ &msRequest as Message +/
   /+ Returns Message +/
   /+ Extends/implements PS_PT:Integration:IRequestHandler.OnRequest +/
   Local Message &response;
   &response = CreateMessage(@("Operation." | &msRequest.OperationName), %IntBroker_Response);
   /* Setup response xml body */
   Local XmlDoc &xmlDocResponse;
   &xmlDocResponse = CreateXmlDoc("");
   Local XmlNode &responseRootNode;
   &responseRootNode = &xmlDocResponse.CreateDocumentElement("RESPONSE");
   Local XmlNode &xnTemp;
   Local XmlNode &xnChildNode;
   &xnChildNode = &xmlDocResponse.DocumentElement.AddElement("hello");
   &xnTemp = &xnChildNode.AddText("world");
   &xnChildNode.AddAttribute("DEBUG_NODE", "True");
   &xnChildNode = &xmlDocResponse.DocumentElement.AddElement("STRING_TEST_CDATA");
   Local string &s = "<p><b>I have characters that should be escaped</b></p>";
   &xnTemp = &xnChildNode.AddCDataSection(&s);
   &xnChildNode = &xmlDocResponse.DocumentElement.AddElement("STRING_TEST");
   &xnTemp = &xnChildNode.AddText(&s);
   Local XmlNode &nxMetaNode;
   &nxMetaNode = &xmlDocResponse.DocumentElement.AddElement("META");
   &nxMetaNode.AddAttribute("isMETA", "True");
   &xnTemp = &nxMetaNode.AddElement("OPRID").AddText(%OperatorId);
   &xnTemp = &nxMetaNode.AddElement("CURRENT_TIME").AddText(String(%Datetime));
   &xnTemp = &nxMetaNode.AddElement("TRANSACTION_ID").AddText(&msRequest.TransactionId);
   &xnTemp = &nxMetaNode.AddElement("DBNAME").AddText(%DbName);
   /* Push the generated XML to the response document */
   Return &response;