XMLSERIALIZE

2024-03-05 01:54:27  Updated

Purpose

XMLSERIALIZE() serializes XMLType data into data of a string type such as VARCHAR, VARCHAR2, CLOB, or BLOB. The data format can be configured by specifying parameters.

Syntax

XMLSERIALIZE
  ( { DOCUMENT | CONTENT } value_expr [ AS datatype ]
    [ ENCODING xml_encoding_spec ]
    [ VERSION string_literal ]
    [ NO INDENT | { INDENT [SIZE = number] } ]
    [ { HIDE | SHOW } DEFAULTS ]
  )

Parameters

Parameter Description
DOCUMENT Specifies that the data to be serialized is a valid XML document.
CONTENT Specifies that the data to be serialized is a valid XML fragment.
AS datatype The return type, which can be VARCHAR, VARCHAR2, BLOB, or CLOB. The default value is CLOB. If the return type is BLOB, you can specify ENCODING.
ENCODING The encoding standard.
VERSION The version.
NO INDENT ELEMENT Specifies to strip line breaks or indentation between different levels of elements.
INDENT SIZE The number of indent spaces. If this parameter is set to 0, each element is placed on a line by itself but without indentation. The default value is 2.
HIDE | SHOW DEFAULTS Specifies whether to display the default values defined by the XML schema.

Return type

The return type is VARCHAR, VARCHAR2, CLOB, or BLOB.

Examples

obclient> SELECT XMLSERIALIZE(CONTENT XMLPARSE(CONTENT 'aaa' WELLFORMED) AS varchar2(100) VERSION '1.0') AS RES FROM DUAL;
+---------------------------+
| RES                       |
+---------------------------+
| <?xml version="1.0"?>
aaa |
+---------------------------+
1 row in set

obclient> SELECT XMLSERIALIZE(DOCUMENT XMLPARSE(DOCUMENT '<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<FROM>Jani</FROM>
<heading>Reminder</heading>
<body><p>Do not forget me this weekend!</p></body>
</note>') AS varchar2(200) NO INDENT) AS RES FROM DUAL;
+----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| RES                                                                                                                                                            |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| <?xml version="1.0" encoding="UTF-8"?><note><to>Tove</to><FROM>Jani</FROM><heading>Reminder</heading><body><p>Do not forget me this weekend!</p></body></note> |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set

obclient> SELECT XMLSERIALIZE(DOCUMENT XMLPARSE(DOCUMENT '<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<FROM>Jani</FROM>
<heading>Reminder</heading>
<body><p>Do not forget me this weekend!</p></body>
</note>') AS varchar2(200)) AS RES FROM DUAL;
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| RES                                                                                                                                                                                   |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| <?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Tove</to>
  <FROM>Jani</FROM>
  <heading>Reminder</heading>
  <body>
    <p>Do not forget me this weekend!</p>
  </body>
</note>
 |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set

Contact Us