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"?>
<RESPONSE>
  <hello DEBUG_NODE="True">world</hello>
  <STRING_TEST_CDATA>
    <![CDATA[<p><b>I have characters that should be escaped</b></p>]]>
  </STRING_TEST_CDATA>
  <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">
    <OPRID>PS</OPRID>
    <CURRENT_TIME>2019-10-21-19.20.31.000000</CURRENT_TIME>
    <TRANSACTION_ID>d95ffa9d-f437-11e9-8a51-fef2b534ad6a</TRANSACTION_ID>
    <DBNAME>CS92U009</DBNAME>
  </META>
</RESPONSE>

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;
end-class;

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 */
   &response.SetXmlDoc(&xmlDocResponse);
   Return &response;
   
end-method;

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