3

私は次のクエリを使用しています:

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>

ノード。

親切に提案を提供してください...

4

4 に答える 4

4

Oracle の XMLQuery 機能を使用して、XML を一度に生成できます。これを説明するために、まず 4 つのテーブルを作成してセットアップをシミュレートします。

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.

クエリが質問と同じ結果セットを返すように、これらの 4 つのテーブルを作成しました。

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('/')きれいな印刷のためだけに使用したことに注意してください。あなたはそれを残すことができます。

また、現在のクエリは 2 レベルの深さしかないため、以下のクエリを使用してクエリをいくらか短縮できます。ただし、2 つ以上のレベルがある場合は、簡単に拡張できるインライン バリアントを使用する必要があります。

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  /

お役に立てれば。

よろしく、
ロブ。

于 2012-06-18T10:04:00.027 に答える
2

多かれ少なかれこの結果を得るには:

<?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 &amp; 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);
    }

}
于 2012-06-18T06:49:12.663 に答える
2

POJO を構築し、JAXB API を使用して XML に変換できます。

于 2012-06-18T07:17:11.610 に答える
0

Oracle はクエリから XML をネイティブに作成できます

http://docs.oracle.com/cd/B19306_01/appdev.102/b14259/xdb13gen.htm

これが最速の方法になると思います

于 2012-06-18T07:54:41.727 に答える