PostgreSQL reports different XML parsing failures using SQLSTATE codes 2200S, 2200M, and 2200N when XML content fails W3C validation during functions like XMLPARSE(DOCUMENT ...), xmlcomment(), or when inserting into XML-typed columns. Error 2200S (invalid_xml_comment) occurs when XML comments violate the XML specification, most often due to the forbidden double-hyphen (“--”) sequence inside comment text, malformed comment closing sequences (comments must close with “-->”), or dynamically generated comment strings that embed unsanitized input containing “--”.

Error 2200M (invalid_xml_document) is raised when the parsed value is not a well-formed XML document at the structural/document level—commonly caused by missing or multiple root elements, unescaped special characters, unclosed tags, or mismatches between the XML declaration’s encoding and the actual database encoding. Error 2200N (invalid_xml_content) covers malformed XML structure or content (for example mismatched or improperly nested tags and unescaped special characters), including cases where XML fragments are concatenated without a single wrapper root.

All sources recommend validating with XMLPARSE beforehand, using PostgreSQL’s native XML builder functions (XMLELEMENT/XMLFOREST/XMLAGG/xmlcomment where applicable) instead of string concatenation, and optionally adding staging-layer checks or database constraints/triggers to block invalid XML.