gpt4 book ai didi

java - 从 java 和 sql 查询生成 xml

转载 作者:数据小太阳 更新时间:2023-10-29 02:19:35 26 4
gpt4 key购买 nike

我正在使用以下查询:

SELECT inst.*
FROM
(SELECT ROWNUM AS r,
b.name INSTNAME,
ICC.INSTITUTE_ID,
ICC.COURSE_ID,
c.name INSTCOURSE
FROM INSTITUTE_COURSE_CATEGORY ICC,
INSTITUTE a,
MEMBER b,
INSTITUTE_COURSE c
WHERE ICC.CATEGORY_ID = 47
AND ICC.INSTITUTE_ID = a.id
AND ICC.INSTITUTE_ID = c.institute_id
AND ICC.COURSE_ID = c.id
AND a.member_id = b.id
)inst
WHERE inst.r BETWEEN 1 AND 10

产生以下结果:

  R     INSTNAME                             INSTITUTE_ID   COURSE_ID   INSTCOURSE                                                                 
1 Asia Pacific Institute Of Mangement 167 1134 Post Graduate Diploma in Management
2 Asia Pacific Institute Of Mangement 167 1135 Post Graduate Diploma in Management (Marketing)
3 Asia Pacific Institute Of Mangement 167 1136 Post Graduate Diploma in Management (International Business)
4 Asia Pacific Institute Of Mangement 167 1138 Post Graduate Diploma in Management (Banking & Financial Services)
5 University of Delhi 182 4283 Bachelor of Business Studies
6 University of Delhi 182 4285 Bachelor of Commerce (Pass)
7 University of Delhi 182 4291 Bachelor of Commerce (Honours)
8 University of Delhi 182 4366 Master of Commerce
9 Acharya Narendra Dev College 183 812 B.Com. (Hons)
10 Aditi Mahavidyalaya 184 829 B.Com (Pass)

根据上面的查询结果,我不得不在java中制作以下xml:

<Root>
<INSTITUTE>
<ID>167</ID>
<NAME>Asia Pacific Instiute Of Mangement</Name>
<COURSES>
<COURSE>
<ID>1134</ID>
<NAME>Post Graduate Diploma in Management</NAME>
</COURSE>
..more
</COURSES>
</INSTITUTE>
..more
</Root>

例如:

There are 4 Courses that belongs to InstituteID(167). 

The problem I am facing is how to make 4 child nodes (Courses Nodes(1134,1135,1136,1138)) under a single `<INSTITUTE>` node

随后将其他类(class)节点置于各自的节点下

<INSITUTE>

节点。

请提供建议...

最佳答案

您可以使用 Oracle 的 XMLQuery 功能一次性生成 XML。为了说明这一点,我首先通过创建四个表来模拟您的设置:

SQL> create table member (id,name)
2 as
3 select 1, 'Asia Pacific Institute Of Management' from dual union all
4 select 2, 'University of Delhi' from dual union all
5 select 3, 'Acharya Narendra Dev College' from dual union all
6 select 4, 'Aditi Mahavidyalaya' from dual
7 /

Table created.

SQL> create table institute (id, member_id)
2 as
3 select 167, 1 from dual union all
4 select 182, 2 from dual union all
5 select 183, 3 from dual union all
6 select 184, 4 from dual
7 /

Table created.

SQL> create table institute_course (id,institute_id,name)
2 as
3 select 1134, 167, 'Post Graduate Diploma in Management' from dual union all
4 select 1135, 167, 'Post Graduate Diploma in Management (Marketing)' from dual union all
5 select 1136, 167, 'Post Graduate Diploma in Management (International Business)' from dual union all
6 select 1137, 167, 'Post Graduate Diploma in Management (Banking & Financial Services)' from dual union all
7 select 4283, 182, 'Bachelor of Business Studies' from dual union all
8 select 4285, 182, 'Bachelor of Commerce (Pass)' from dual union all
9 select 4291, 182, 'Bachelor of Commerce (Honours)' from dual union all
10 select 4366, 182, 'Master of Commerce' from dual union all
11 select 812, 183, 'B.Com. (Hons)' from dual union all
12 select 829, 184, 'B.Com (Pass)' from dual
13 /

Table created.

SQL> create table institute_course_category (category_id,institute_id,course_id)
2 as
3 select 47, 167, 1134 from dual union all
4 select 47, 167, 1135 from dual union all
5 select 47, 167, 1136 from dual union all
6 select 47, 167, 1137 from dual union all
7 select 47, 182, 4283 from dual union all
8 select 47, 182, 4285 from dual union all
9 select 47, 182, 4291 from dual union all
10 select 47, 182, 4366 from dual union all
11 select 47, 183, 812 from dual union all
12 select 47, 184, 829 from dual
13 /

Table created.

我创建了这四个表,以便您的查询给出与您的问题相同的结果集:

SQL> SELECT inst.*
2 FROM ( SELECT ROWNUM AS r
3 , b.name INSTNAME
4 , ICC.INSTITUTE_ID
5 , ICC.COURSE_ID
6 , c.name INSTCOURSE
7 FROM INSTITUTE_COURSE_CATEGORY ICC
8 , INSTITUTE a
9 , MEMBER b
10 , INSTITUTE_COURSE c
11 WHERE ICC.CATEGORY_ID = 47
12 AND ICC.INSTITUTE_ID = a.id
13 AND ICC.INSTITUTE_ID = c.institute_id
14 AND ICC.COURSE_ID = c.id
15 AND a.member_id = b.id
16 ) inst
17 WHERE inst.r BETWEEN 1 AND 10
18 /

R INSTNAME INSTITUTE_ID COURSE_ID INSTCOURSE
--- ------------------------------------ ------------ ---------- ------------------------------------------------------------------
1 Asia Pacific Institute Of Management 167 1137 Post Graduate Diploma in Management (Banking & Financial Services)
2 Asia Pacific Institute Of Management 167 1136 Post Graduate Diploma in Management (International Business)
3 Asia Pacific Institute Of Management 167 1135 Post Graduate Diploma in Management (Marketing)
4 Asia Pacific Institute Of Management 167 1134 Post Graduate Diploma in Management
5 University of Delhi 182 4366 Master of Commerce
6 University of Delhi 182 4291 Bachelor of Commerce (Honours)
7 University of Delhi 182 4285 Bachelor of Commerce (Pass)
8 University of Delhi 182 4283 Bachelor of Business Studies
9 Acharya Narendra Dev College 183 812 B.Com. (Hons)
10 Aditi Mahavidyalaya 184 829 B.Com (Pass)

10 rows selected.

现在 XML 查询是:

SQL> select xmlelement
2 ( "Root"
3 , xmlelement
4 ( "INSTITUTE"
5 , xmlagg(xmlforest(id,name,courses))
6 )
7 ).extract('/') your_xml
8 from ( select i.id
9 , m.name
10 , xmlagg
11 ( xmlelement
12 ( "COURSE"
13 , xmlforest(ic.id,ic.name)
14 )
15 ) courses
16 from institute i
17 inner join member m on (i.member_id = m.id)
18 inner join institute_course ic on (ic.institute_id = i.id)
19 inner join institute_course_category icc on (icc.institute_id = ic.institute_id and icc.course_id = ic.id)
20 where icc.category_id = 47
21 group by i.id
22 , m.name
23 )
24 /

YOUR_XML
--------------------------------------------------------------------------------------------------------------------------------------
<Root>
<INSTITUTE>
<ID>167</ID>
<NAME>Asia Pacific Institute Of Management</NAME>
<COURSES>
<COURSE>
<ID>1137</ID>
<NAME>Post Graduate Diploma in Management (Banking &amp; Financial Services)</NAME>
</COURSE>
<COURSE>
<ID>1136</ID>
<NAME>Post Graduate Diploma in Management (International Business)</NAME>
</COURSE>
<COURSE>
<ID>1135</ID>
<NAME>Post Graduate Diploma in Management (Marketing)</NAME>
</COURSE>
<COURSE>
<ID>1134</ID>
<NAME>Post Graduate Diploma in Management</NAME>
</COURSE>
</COURSES>
<ID>182</ID>
<NAME>University of Delhi</NAME>
<COURSES>
<COURSE>
<ID>4366</ID>
<NAME>Master of Commerce</NAME>
</COURSE>
<COURSE>
<ID>4291</ID>
<NAME>Bachelor of Commerce (Honours)</NAME>
</COURSE>
<COURSE>
<ID>4285</ID>
<NAME>Bachelor of Commerce (Pass)</NAME>
</COURSE>
<COURSE>
<ID>4283</ID>
<NAME>Bachelor of Business Studies</NAME>
</COURSE>
</COURSES>
<ID>183</ID>
<NAME>Acharya Narendra Dev College</NAME>
<COURSES>
<COURSE>
<ID>812</ID>
<NAME>B.Com. (Hons)</NAME>
</COURSE>
</COURSES>
<ID>184</ID>
<NAME>Aditi Mahavidyalaya</NAME>
<COURSES>
<COURSE>
<ID>829</ID>
<NAME>B.Com (Pass)</NAME>
</COURSE>
</COURSES>
</INSTITUTE>
</Root>


1 row selected.

这里是所用函数的文档:

XMLElement
XMLForest
XMLAgg

请注意,我使用 .extract('/') 只是为了美观。您可以将其关闭。

而且,由于您当前的查询只有两层深度,您可以使用下面的查询稍微缩短查询。但是,当您有两个以上的级别时,您将不得不使用可以轻松扩展的内联变体。

SQL> select xmlelement
2 ( "Root"
3 , xmlelement
4 ( "INSTITUTE"
5 , xmlagg
6 ( xmlforest
7 ( i.id
8 , m.name
9 , xmlagg
10 ( xmlelement
11 ( "COURSE"
12 , xmlforest(ic.id,ic.name)
13 )
14 ) as "COURSES"
15 )
16 )
17 )
18 ).extract('/') your_xml
19 from institute i
20 inner join member m on (i.member_id = m.id)
21 inner join institute_course ic on (ic.institute_id = i.id)
22 inner join institute_course_category icc on (icc.institute_id = ic.institute_id and icc.course_id = ic.id)
23 where icc.category_id = 47
24 group by i.id
25 , m.name
26 /

希望这对您有所帮助。

问候,
罗布。

关于java - 从 java 和 sql 查询生成 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11077676/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com