XMLELEMENT function in PL/SQL

The XMLELEMENT function allows developers to read values from Oracle tables and present them in XML format along with wrapping the values in descriptive XML tags.

The general format for XMLELEMENT is:

SELECT XMLELEMENT("tag_name", TABLE.field_name1),
XMLELEMENT("tag_name", TABLE.field_name2)
FROM TableName;

The XMLELEMENT function creates an XML element in XMLType by taking the tag_name and the information from the specified field of a table as arguments. The tag_name is used to create an XML tag.

For example, in the following query we will read the same two attributes, stno and sname, from the Student table and present the query in XML format, wrapped in descriptive XML tags:

SELECT XMLELEMENT("Student_Number", stno),
XMLELEMENT("Name", sname)
FROM Student;

output:

XMLELEMENT("STUDENT_NUMBER",STNO)
--------------------------------------------------------------------
XMLELEMENT("NAME",SNAME)
--------------------------------------------------------------------
<Student_Number>2</Student_Number>
<Name>ab</Name>
 
<Student_Number>3</Student_Number>
<Name>Mani</Name>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.