In this blog, I have given an example which will generate a single XML object from a set of employee records. The query is written using XMLELEMENT and XMLAGG functions below.
- SELECT xmlelement("employeeDetails"
- , xmlagg( xmlelement("employeeDetail"
- , xmlelement("employeeID",employee_id)
- , xmlelement("firstName",first_name)
- , xmlelement("lastName",last_name)
- , xmlelement("email",email)
- , xmlelement("phoneNumber",phone_number)
- , xmlelement("hireDate",hire_date)
- , xmlelement("salary",salary)
- , xmlelement("departmentID",e.department_id)
- , xmlelement("departmentName",department_name)
- )
- )
- ) AS employee_xml FROM employees e, departments d
- WHERE e.department_id=d.department_id
- ;
- Output
- -------
- <?xml version="1.0" encoding="UTF-8"?>
- <employeeDetails>
- <employeeDetail>
- <employeeID>200</employeeID>
- <firstName>Jennifer</firstName>
- <lastName>Whalen</lastName>
- <email>JWHALEN</email>
- <phoneNumber>515.123.4444</phoneNumber>
- <hireDate>2003-09-17</hireDate>
- <salary>4400</salary>
- <departmentID>10</departmentID>
- <departmentName>Administration</departmentName>
- </employeeDetail>
- <employeeDetail>
- <employeeID>201</employeeID>
- <firstName>Michael</firstName>
- <lastName>Hartstein</lastName>
- <email>MHARTSTE</email>
- <phoneNumber>515.123.5555</phoneNumber>
- <hireDate>2004-02-17</hireDate>
- <salary>13000</salary>
- <departmentID>20</departmentID>
- <departmentName>Marketing</departmentName>
- </employeeDetail>
- <employeeDetail>
- <employeeID>202</employeeID>
- <firstName>Pat</firstName>
- <lastName>Fay</lastName>
- <email>PFAY</email>
- <phoneNumber>603.123.6666</phoneNumber>
- <hireDate>2005-08-17</hireDate>
- <salary>6000</salary>
- <departmentID>20</departmentID>
- <departmentName>Marketing</departmentName>
- </employeeDetail>
- </employeeDetails>
The same code can be replaced with XMLELEMNT, XMLFOREST AND XMLAGG and the code is much reduced.
- SELECT xmlelement("employeeDetails"
- , xmlagg( xmlelement("employeeDetail"
- , xmlforest(employee_id AS "employeeID"
- , first_name AS "firstName"
- , last_name AS "lastName"
- , email AS"email"
- , phone_number AS "phoneNumber"
- , hire_date AS "hireDate"
- , salary AS "salary"
- , e.department_id AS "departmentID"
- , department_name AS "departmentName"
- ))
- )
- ) AS employee_xml FROM employees e, departments d
- WHERE e.department_id=d.department_id
- ;
- Output
- -------
- <?xml version="1.0" encoding="UTF-8"?>
- <employeeDetails>
- <employeeDetail>
- <employeeID>200</employeeID>
- <firstName>Jennifer</firstName>
- <lastName>Whalen</lastName>
- <email>JWHALEN</email>
- <phoneNumber>515.123.4444</phoneNumber>
- <hireDate>2003-09-17</hireDate>
- <salary>4400</salary>
- <departmentID>10</departmentID>
- <departmentName>Administration</departmentName>
- </employeeDetail>
- <employeeDetail>
- <employeeID>201</employeeID>
- <firstName>Michael</firstName>
- <lastName>Hartstein</lastName>
- <email>MHARTSTE</email>
- <phoneNumber>515.123.5555</phoneNumber>
- <hireDate>2004-02-17</hireDate>
- <salary>13000</salary>
- <departmentID>20</departmentID>
- <departmentName>Marketing</departmentName>
- </employeeDetail>
- <employeeDetail>
- <employeeID>202</employeeID>
- <firstName>Pat</firstName>
- <lastName>Fay</lastName>
- <email>PFAY</email>
- <phoneNumber>603.123.6666</phoneNumber>
- <hireDate>2005-08-17</hireDate>
- <salary>6000</salary>
- <departmentID>20</departmentID>
- <departmentName>Marketing</departmentName>
- </employeeDetail>
- </employeeDetails>
No comments:
Post a Comment