I tried replacing using regex functions or you can use CDATA, Since you are likely building the XOGXML string dynamically (perhaps from a database query or a loop), you can wrap the variables directly.
Instead of:
<PersonalInformation displayName="${row.display_name}" ... />
Use:
<PersonalInformation displayName="<![CDATA[${row.display_name}]]>" ... />
<gel:script xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
xmlns:core="jelly:core"
xmlns:re="jelly:regexp">
<core:set var="rawXOG" encode="0"><![CDATA[
<NikuDataBus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Resources>
<Resource>
<PersonalInformation displayName="User (R&D), Test" emailAddress="test&test.com"/>
</Resource>
</Resources>
</NikuDataBus>
]]></core:set>
<core:set var="cleanXOG" value="${rawXOG.replaceAll('&(?!(amp|lt|gt|quot|apos|#[0-9]+|#x[a-fA-F0-9]+);)', '&amp;')}"/>
<gel:log>Cleaned XML: ${cleanXOG}</gel:log>
<core:invokeStatic className="javax.xml.parsers.DocumentBuilderFactory" method="newInstance" var="factory"/>
<core:invoke method="newDocumentBuilder" on="${factory}" var="builder"/>
<core:new className="java.io.StringReader" var="StrReader">
<core:arg value="${cleanXOG}"/>
</core:new>
<core:new className="org.xml.sax.InputSource" var="ipSrc">
<core:arg value="${StrReader}"/>
</core:new>
<core:invoke method="parse" on="${builder}" var="RootXML">
<core:arg value="${ipSrc}"/>
</core:invoke>
<gel:log>Parsing Successful!</gel:log>
</gel:script>
------------------------------
Thanks & Regards
Suman Pramanik
------------------------------
Original Message:
Sent: Feb 18, 2026 03:40 PM
From: Senthil Kumar Lakshmanan
Subject: XML Parse Error
I'm getting an error while parsing an XML string because it contains an unescaped ampersand (&) (for example: R&D). The parser throws an error like:
The reference to entity "D" must end with the ';' delimiter.
If anyone has a sample GEL script / Java SAX-based example that safely escapes only invalid & characters (without corrupting existing entities like &, <, etc.), please share.
<gel:script xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
xmlns:core="jelly:core">
<core:invokeStatic className="javax.xml.parsers.DocumentBuilderFactory" method="newInstance" var="factory"/>
<core:invoke method="newDocumentBuilder" on="${factory}" var="builder"/>
<core:set var="XOGXML" encode="0"><![CDATA[
<NikuDataBus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../xsd/nikuxog_resource.xsd">
<Header action="write" externalSource="NIKU" objectType="resource" version="16"/>
<Resources>
<Resource bookingManagerUserName="abc" employmentType="abc" externalId="abc" hireDate="2013-10-28" isActive="true" managerUserName="abc" resourceId="abc" resourceType="LABOR" username="abc">
<PersonalInformation displayName="User (R&D), Test" emailAddress="abc" firstName="Test" lastName="User (R&D)"/>
<ManagementInformation availability="8" openForTimeEntry="false"/>
<CustomInformation>
<ColumnValue name="partition_code">NIKU.ROOT</ColumnValue>
</CustomInformation>
</Resource>
</Resources>
</NikuDataBus>
]]>
</core:set>
<gel:log>${XOGXML}</gel:log>
<core:new className="java.io.StringReader" var="StrReader">
<core:arg value="${XOGXML}"/>
</core:new>
<core:new className="org.xml.sax.InputSource" var="ipSrc">
<core:arg value="${StrReader}"/>
</core:new>
<core:invoke method="parse" on="${builder}" var="RootXML">
<core:arg value="${ipSrc}"/>
</core:invoke>
</gel:script>
-------------------------------------------