niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Databases » Oracle
Return Nested XML from Heirarchical Query
Post new topic   Reply to topic Page 1 of 1 [2 Posts] View previous topic :: View next topic
Author Message
Mark Petereit
*nix forums beginner


Joined: 17 Jul 2006
Posts: 1

PostPosted: Mon Jul 17, 2006 5:19 pm    Post subject: Return Nested XML from Heirarchical Query Reply with quote

I have the following hierarchical query that returns our company's
organizational structure:


SELECT TRIM(E.LAST_NAME)||', '||TRIM(E.NICK_NAME) AS Name,
J.DESCRIPTION AS Title
FROM LAWPRODHR.EMPLOYEE E
JOIN LAWPRODHR.JOBCODE J ON
J.COMPANY = E.COMPANY AND
J.JOB_CODE = E.JOB_CODE
LEFT JOIN LAWPRODHR.HRSUPER S ON
S.COMPANY = E.COMPANY AND
S.EMPLOYEE = E.EMPLOYEE
CONNECT BY PRIOR S.CODE = E.SUPERVISOR


How would I utilize XMLAgg, XMLElement, XMLForrest, etc. to generate
nested XML elements that properly reflect the organizational structure?


For example, the executive branch of the U.S. Government would look
like this:


<Position>
<Name>George Bush</Name>
<Title>President</Title>
<Position>
<Name>Donald Rumsfeld</Name>
<Title>Secretary of Defense</Title>
<Position>
<Name>Gordon England</Name>
<Title>Deputy Secretary of Defense</Title>
</Position>
</Position>
<Position>
<Name>Condoleezza Rice</Name>
<Title>Secretary of Status</Title>
<Position>
<Name>Karen Hughes</Name>
<Title>Under Secretary for Public Diplomacy and Public
Affairs</Title>
</Position>
</Position>
</Position>
Back to top
Maxim Demenko
*nix forums Guru


Joined: 24 Apr 2005
Posts: 428

PostPosted: Mon Jul 17, 2006 6:10 pm    Post subject: Re: Return Nested XML from Heirarchical Query Reply with quote

Mark Petereit schrieb:
Quote:
I have the following hierarchical query that returns our company's
organizational structure:


SELECT TRIM(E.LAST_NAME)||', '||TRIM(E.NICK_NAME) AS Name,
J.DESCRIPTION AS Title
FROM LAWPRODHR.EMPLOYEE E
JOIN LAWPRODHR.JOBCODE J ON
J.COMPANY = E.COMPANY AND
J.JOB_CODE = E.JOB_CODE
LEFT JOIN LAWPRODHR.HRSUPER S ON
S.COMPANY = E.COMPANY AND
S.EMPLOYEE = E.EMPLOYEE
CONNECT BY PRIOR S.CODE = E.SUPERVISOR


How would I utilize XMLAgg, XMLElement, XMLForrest, etc. to generate
nested XML elements that properly reflect the organizational structure?


For example, the executive branch of the U.S. Government would look
like this:


Position
Name>George Bush</Name
Title>President</Title
Position
Name>Donald Rumsfeld</Name
Title>Secretary of Defense</Title
Position
Name>Gordon England</Name
Title>Deputy Secretary of Defense</Title
/Position
/Position
Position
Name>Condoleezza Rice</Name
Title>Secretary of Status</Title
Position
Name>Karen Hughes</Name
Title>Under Secretary for Public Diplomacy and Public
Affairs</Title
/Position
/Position
/Position


If you are on 10g, there is an out the box solution , you can try
something like this ( i took the standard emp table only to test this
block, you can replace the relevant part with your query).

DECLARE
qryctx DBMS_XMLGEN.ctxhandle;
result XMLType;
PROCEDURE lob_output
(
p_clob CLOB
) IS
l_clob CLOB;
l_clob_Length NUMBER;
l_Iterations NUMBER;
l_Chunk VARCHAR2(32767);
l_Chunk_Length NUMBER := 32767;
BEGIN
l_clob := p_clob;
l_clob_Length := Dbms_Lob.Getlength(l_clob);
l_Iterations := Ceil(l_clob_Length / l_Chunk_Length);
FOR i IN 0 .. l_Iterations - 1 LOOP
l_Chunk := Dbms_Lob.Substr(l_clob, l_Chunk_Length, i *
l_Chunk_Length + 1);
dbms_output.put_line(l_Chunk);
END LOOP;
END;
BEGIN
qryctx :=
DBMS_XMLGEN.newcontextFromHierarchy(
'SELECT level,
XMLElement("Position",
XMLElement("Name", ename),
XMLElement("Title", job))
FROM emp
CONNECT BY PRIOR empno = mgr
START WITH mgr is NULL');
result := DBMS_XMLGEN.getxmltype(qryctx);
DBMS_XMLGEN.closecontext(qryctx);
lob_output(RESULT.getClobVal());
END;
/

Btw, example is almost copy/pasted from
http://download-east.oracle.com/docs/cd/B19306_01/appdev.102/b14259/xdb13gen.htm#sthref1610

Best regards

Maxim
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [2 Posts] View previous topic :: View next topic
The time now is Thu Nov 20, 2008 11:00 pm | All times are GMT
navigation Forum index » Databases » Oracle
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Very slow query Michael Sutter MySQL 0 Fri Jul 21, 2006 1:10 pm
No new posts nested switches, with common labels G Patel C 3 Fri Jul 21, 2006 2:45 am
No new posts Column info without executing query Dan Strömberg PostgreSQL 10 Thu Jul 20, 2006 6:21 pm
No new posts recursive query Jürg Schaufelberger Server 1 Thu Jul 20, 2006 6:06 pm
No new posts How to query on part of a date column? Barry Newton MySQL 4 Thu Jul 20, 2006 5:02 pm

Mortgage Calculator | Car salvage | Unsecured Loans | Mortgage Calculator | MPAA
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2332s ][ Queries: 16 (0.1535s) ][ GZIP on - Debug on ]