Я пытаюсь сгенерировать вложенный XML-файл, получая данные из схемы HR в Oracle. XML должен выглядеть следующим образом. В этом случае данные хранятся в объектах Oracle PLSQL, а не в таблице сотрудников и отделов.
<Departments>
<department id = "department_id" name = "department_name" >
<employees>
<employee id = "employee_id">
<employee_name> </employee_name>
<salary> </salary>
</employee>
<employee id = "employee_id">
<employee_name> </employee_name>
<salary> </salary>
</employee>
</employees>
</department>
</Departments>
Объекты могут быть созданы с использованием следующего кода:
--Employee Object
CREATE TYPE emp_o AS OBJECT (
employee_id NUMBER,
employee_name VARCHAR2(100),
salary NUMBER
);
--Employee table
CREATE TYPE emp_ot IS
TABLE OF emp_o;
--Department object
CREATE TYPE deps_o AS OBJECT (
department_id NUMBER,
department_name VARCHAR2(100),
emp_tab emp_ot
);
--department table
CREATE TYPE deps_t IS
TABLE OF deps_o;
Вот чтоДо сих пор я пытался, приведенный ниже код делает следующее: 1. Заполнить объекты PLSQL 2. Сгенерировать XML Clob, используя данные в этих объектах. -> Здесь я сталкиваюсь с проблемой. Пожалуйста, рассмотрите только без комментариев код
DECLARE
l_message DEPS_T;
l_employee_t emp_ot;
l_xml_clob CLOB;
BEGIN
SELECT deps_o(
d.department_id,
d.department_name,
emp_ot(emp_o(
e.employee_id,
e.first_name || ' ' || e.last_name,
e.salary
)
)
)
BULK COLLECT INTO l_message
FROM departments d , employees e
where e.department_id = d.department_id and d.department_id =20;
--Print the content
-- for i in 1..l_message.COUNT
-- loop
-- DBMS_OUTPUT.PUT_LINE(l_message(i).department_id);
-- DBMS_OUTPUT.PUT_LINE(l_message(i).department_name);
-- l_employee_t := l_message(i).emp_tab;
-- for j in 1..l_employee_t.COUNT
-- loop
-- DBMS_OUTPUT.PUT_LINE('Employee Name:'||l_employee_t(j).employee_name);
-- end loop;
-- end loop;
--Generate XML
-- <Departments>
-- <department id = "department_id" name = "department_name" >
-- <employees>
-- <employee id = "employee_id">
-- <employee_name> </employee_name>
-- <salary> </salary>
-- </employee>
-- </employees>
-- </department>
-- </Departments>
-- select XMlelement("Departments",XMLElement("department",
-- XMLAttributes(t.department_name as "id", t.department_name as "name"),
-- XMLElement("employees",
-- XMLElement("employee",
-- XMLAttributes(g.employee_id as "employee_id"),
-- XMLForest(g.employee_name,g.salary)
-- )
-- )
--
-- )).getClobval() into l_xml_clob
--
-- from TABLE(CAST(l_message as deps_t)) t,
-- TABLE(CAST(t.emp_tab as emp_ot)) g;
select XMLELEMENT("Departments",
(SELECT XMLAGG(XMLELEMENT("department",
XMLATTRIBUTES(t.department_name as "id", t.department_name as "name"),
(select XMLelement("employees",XMLAGG(XMLelement("employee",
XMLAttributes(g.employee_id as "id"),
XMlForest(g.employee_name as "employee_name")
))) ---"employees" end
from TABLE(CAST(t.emp_tab as emp_ot)) g
) ---end of inner select
)
)
from TABLE(CAST(l_message as deps_t)) t
group by t.department_id)
).getClobval() into l_xml_clob
from dual;
-- group by t.department_id;
--for rec1 in (
--select t.department_id||','
-- ||t.department_name||','
-- ||g.employee_id||','
-- ||g.employee_name||','
-- ||g.salary as r
--from TABLE(CAST(l_message as deps_t)) t
-- ,TABLE(CAST(t.emp_tab as emp_ot)) g )
-- loop
---- DBMS_OUTPUT.PUT_LINE(rec1.department_id);
---- DBMS_OUTPUT.PUT_LINE(rec1.department_name);
---- DBMS_OUTPUT.PUT_LINE(rec1.employee_id);
---- DBMS_OUTPUT.PUT_LINE(rec1.employee_name);
---- DBMS_OUTPUT.PUT_LINE(rec1.salary);
-- DBMS_OUTPUT.PUT_LINE(rec1.r);
-- end loop;
DBMS_OUTPUT.PUT_LINE(l_xml_clob);
END;
/