KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Adding XML Comments in 4D Without Character Escaping
PRODUCT: 4D | VERSION: 20 R | PLATFORM: Mac & Win
Published On: October 20, 2025
When setting XML element values directly, 4D automatically escapes special characters like < and > to ensure validity, which can unintendedly convert comment syntax into plain text entities.
To insert a true XML comment node (e.g., <!--attachmentContent-->) without escaping, use DOM Append XML child node with the XML comment constant, as it creates a dedicated node type that preserves the content.

Project Method: Util_AddXMLComment

#DECLARE($elementRef : Text; $commentText : Text)->$commentRef : Text

$commentRef:=DOM Append XML child node($elementRef; XML comment; $commentText)



Usage and Verification

Build the XML structure, append the comment, and export to view the result:
$docRef:=DOM Create XML Ref("root")
$elemRef:=DOM Create XML element($docRef; "com:attachmentContent")
$commentRef:=Util_AddXMLComment($elemRef; "attachmentContent")
DOM EXPORT TO VAR($docRef; $xmlText)
ALERT($xmlText)
DOM CLOSE XML($docRef)