Clarity

 View Only

  • 1.  XML Parse Error

    Posted 15 days ago
    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>



    -------------------------------------------


  • 2.  RE: XML Parse Error

    Broadcom Employee
    Posted 15 days ago

    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&amp;test.com"/>
                    </Resource>
                </Resources>
            </NikuDataBus>
        ]]></core:set>
    
        <core:set var="cleanXOG" value="${rawXOG.replaceAll('&amp;(?!(amp|lt|gt|quot|apos|#[0-9]+|#x[a-fA-F0-9]+);)', '&amp;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
    ------------------------------



  • 3.  RE: XML Parse Error

    Posted 8 days ago

    Hi Suman.

    It worked. 

    Thanks

    Senthil

    -------------------------------------------



  • 4.  RE: XML Parse Error

    Broadcom Employee
    Posted 8 days ago

    Thanks for the confirmation Senthil



    ------------------------------
    Thanks & Regards
    Suman Pramanik
    ------------------------------



  • 5.  RE: XML Parse Error

    Posted 14 days ago

    On the core:set, try using: escapeText="false", this will properly escape the XML, using encode=0 does not work the same way

    -------------------------------------------