我使用以下查询:
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
然后在其各自的
下制作其他课程节点 <INSITUTE>
节点。
请提供建议......
答案 0 :(得分:4)
您可以使用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 & 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.
以下是所用功能的文档:
请注意,我使用.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 /
希望这有帮助。
的问候,
罗布。
答案 1 :(得分:3)
为了获得或多或少的结果:
<?xml version="1.0" encoding="UTF-8"?>
<institute>
<id>167</id>
<name>Asia Pacific Institute Of Mangement</name>
<courses>
<course>
<id>1136</id>
<name>Post Graduate Diploma in Management (International Business)</name>
</course>
<course>
<id>1138</id>
<name>Post Graduate Diploma in Management (Banking & Financial Services)</name>
</course>
</courses>
<id>182</id>
<name>University of Delhi</name>
<courses>
<course>
<id>4283</id>
<name>Bachelor of Business Studies</name>
</course>
</courses>
</institute>
你可以做点什么:
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class Dom4JTest {
// DATA SET ORDERED BY INSTITUTE, COURSE (COULD BE THE ID)
// 3 Asia Pacific Institute Of Mangement 167 1136 Post Graduate Diploma in Management
// (International Business) New Delhi Delhi India
// 4 Asia Pacific Institute Of Mangement 167 1138 Post Graduate Diploma in Management (Banking &
// Financial Services) New Delhi Delhi India
// 5 University of Delhi 182 4283 Bachelor of Business Studies New Delhi Delhi India
private static List<String[]> data = new ArrayList<String[]>();
static {
data.add(new String[] { "3", "Asia Pacific Institute Of Mangement", "167", "1136",
"Post Graduate Diploma in Management (International Business)", "New Delhi", "Delhi", "India" });
data.add(new String[] { "4", "Asia Pacific Institute Of Mangement", "167", "1138",
"Post Graduate Diploma in Management (Banking & Financial Services)", "New Delhi", "Delhi", "India" });
data.add(new String[] { "5", "University of Delhi", "182", "4283", "Bachelor of Business Studies", "New Delhi", "Delhi", "India" });
}
public static void main(String[] args) throws IOException {
Document document = DocumentHelper.createDocument();
Element root = document.addElement("institute");
String id = null;
Element coursesElement = null;
// here the iteration over dataset, of course the result will not be just string but the idea will be the same...
for (String[] strs : data) {
if (id == null || !id.equals(strs[2])) {
id = strs[2];
root.addElement("id").addText(id);
root.addElement("name").addText(strs[1]);
coursesElement = root.addElement("courses");
}
Element course = coursesElement.addElement("course");
course.addElement("id").setText(strs[3]);
course.addElement("name").setText(strs[4]);
}
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(System.out, format);
writer.write(document);
}
}
答案 2 :(得分:1)
您可以构造POJO,然后使用JAXB API将其转换为XML。
答案 3 :(得分:0)