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.
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.
<?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><p><b>I have characters that should be escaped</b></p></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;
Chris Malek is a PeopleTools® Technical Consultant with over two decades of experience working on PeopleSoft enterprise software projects. He is available for consulting engagements.
Work with ChrisSWS turns SQL into production REST APIs — ready for AI, modern apps, and partner integrations. One install, unlimited potential.
A powerful PeopleSoft bolt-on that makes REST web services easy. You bring the SQL, SWS handles the rest.
Traditional PeopleSoft web services cost $3,600–$13,000 each to develop. SWS deploys production REST APIs in under 5 minutes through configuration alone.
Turn PeopleSoft data into clean REST APIs for AI integrations, modern applications, and vendor data feeds. Configuration-driven — no PeopleCode required.