Manipulate a string as XML

I was recently asked to provide code which satisfied the following specification:

In flowdata for a PRD I am trying to do the following in a mapping activity.

I have a string, which looks like this (example):

“<data><startdate>2012-06-20T12:26:32.000+02:00</startdate><enddate>2012-07-17T12:26:32.000+02:00</enddate><udlaaner>24222</udlaaner><modtager>24300</modtager><initiator>cn=ADMKEVRAS,ou=sa,o=data</initiator><desc><value>Test 11</value></desc><A1>[DN on a group]</A1></data>”

This string is just a stringified XML stored on a String attribute in edirectory.

In clean XML it looks like this:

<value>Test 11</value>
<A1>[DN on a group]</A1>

I need to convert this string to an XML document in order to add one more child node to the <desc> node and also to change the value of the <enddate> node. After this I need to stringify the XML document again in order to save the updated information to the String attribute in edirectory.

In order to do what the request asks, the following steps are required:

  • If using a version of Novell IDM prior to 4, you’ll need to add this ECMAScript as a function to whichever mapping activity requires it. If using Novell IDM version 4 and above, you can add this script as a global script and attach it to the Workflow directly; the code can then be called from all mapping activities within that Workflow
  • Read the string out of the directory
  • Ensure the string can be converted to an XML document (replace the < and > tags)
  • Convert the string into an XML document
  • Add the new node and replace the text in the <startdate> node
  • Convert the XML document back into a String, and store it in the directory

The code for the solution is shown below

The string started off looking like this:

<data><startdate>2012-06-20T12:26:32.000+02:00</startdate><enddate>2012-07-17T12:26:32.000+02:00</enddate><udlaaner>24222</udlaaner><modtager>24300</modtager><initiator>cn=ADMKEVRAS,ou=sa,o=data</initiator><desc><value>Test 11</value></desc><A1>[DN on a group]</A1></data>

The string, once manipulated, looked like this:

<?xml version=”1.0″ encoding=”UTF-8″?><data><startdate>BELKAST CONSULTING</startdate><enddate>Keith Armstrong</enddate><udlaaner>24222</udlaaner><modtager>24300</modtager><initiator>cn=ADMKEVRAS,ou=sa,o=data</initiator><desc><value>Test 11</value><new-desc-node>This is a value</new-desc-node></desc><A1>[DN on a group]</A1></data>