递归层次结构查询到xml

时间:2013-03-08 07:20:06

标签: oracle recursion hierarchy

要使用dynatree(https://code.google.com/p/dynatree/)我希望结果在下面的xml表单中,但结果不是我想要的。 dynatree要求第二个组织树。

任何人都可以帮助我吗?

SELECT 
  XMLELEMENT("div",xmlattributes('tree' AS "id"),
       (SELECT DBMS_XMLGEN.getXMLType(
     DBMS_XMLGEN.newContextFromHierarchy('
        SELECT LEVEL,
           case
             when CONNECT_BY_ISLEAF = 0 then
              XMLELEMENT("ul", XMLELEMENT("li", xmlattributes(empno AS "id"), ename))
             when CONNECT_BY_ISLEAF = 1 then
              XMLELEMENT("li", xmlattributes(empno AS "id"), ename)
           end
          FROM emp
         START WITH mgr is null
        CONNECT BY PRIOR empno = mgr '
        )
       ) FROM DUAL
      )) XMLDOC  
 FROM DUAL;

当前结果

<div id="tree"><ul>
  <li id="7839">KING</li>
  <ul>
    <li id="7566">JONES</li>
    <ul>
      <li id="7788">SCOTT</li>
      <li id="7876">ADAMS</li>
    </ul>
    <ul>
      <li id="7902">FORD</li>
      <li id="7369">SMITH</li>
    </ul>
  </ul>
  <ul>
    <li id="7698">BLAKE</li>
    <li id="7499">ALLEN</li>
    <li id="7521">WARD</li>
    <li id="7654">MARTIN</li>
    <li id="7844">TURNER</li>
    <li id="7900">JAMES</li>
  </ul>
  <ul>
    <li id="7782">CLARK</li>
    <li id="7934">MILLER</li>
  </ul>
</ul>
</div>

期望的结果

<ul>
  <li id="7839">KING
   <ul>
    <li id="7566">JONES
     <ul>
      <li id="7788">SCOTT
       <ul>
        <li id="7876">ADAMS</li>
       </ul>
      </li>
      <li id="7902">FORD
       <ul>
        <li id="7369">SMITH</li>
       </ul>
      </li>
     </ul>
    </li>
    <li id="7698">BLAKE
     <ul>
      <li id="7499">ALLEN</li>
      <li id="7521">WARD</li>
      <li id="7654">MARTIN</li>
      <li id="7844">TURNER</li>
      <li id="7900">JAMES</li>
     </ul>
    </li>
    <li id="7782">CLARK
     <ul>
      <li id="7934">MILLER</li>
     </ul>
    </li>
   </ul>
  </li>
 </ul>

sql脚本如下。

CREATE TABLE EMP
       (EMPNO NUMBER(4) NOT NULL,
        ENAME VARCHAR2(10),
        JOB VARCHAR2(9),
        MGR NUMBER(4),
        HIREDATE DATE,
        SAL NUMBER(7, 2),
        COMM NUMBER(7, 2),
        DEPTNO NUMBER(2));

INSERT INTO EMP VALUES
        (7369, 'SMITH',  'CLERK',     7902,
        TO_DATE('17-12-1980', 'DD-MM-YYYY'),  800, NULL, 20);

INSERT INTO EMP VALUES
        (7499, 'ALLEN',  'SALESMAN',  7698,
        TO_DATE('20-02-1981', 'DD-MM-YYYY'), 1600,  300, 30);

INSERT INTO EMP VALUES
        (7521, 'WARD',   'SALESMAN',  7698,
        TO_DATE('22-02-1981', 'DD-MM-YYYY'), 1250,  500, 30);

INSERT INTO EMP VALUES
        (7566, 'JONES',  'MANAGER',   7839,
        TO_DATE('02-04-1981', 'DD-MM-YYYY'),  2975, NULL, 20);

INSERT INTO EMP VALUES
        (7654, 'MARTIN', 'SALESMAN',  7698,
        TO_DATE('28-09-1981', 'DD-MM-YYYY'), 1250, 1400, 30);

INSERT INTO EMP VALUES
        (7698, 'BLAKE',  'MANAGER',   7839,
        TO_DATE('01-05-1981', 'DD-MM-YYYY'),  2850, NULL, 30);

INSERT INTO EMP VALUES
        (7782, 'CLARK',  'MANAGER',   7839,
        TO_DATE('09-06-1981', 'DD-MM-YYYY'),  2450, NULL, 10);

INSERT INTO EMP VALUES
        (7788, 'SCOTT',  'ANALYST',   7566,
        TO_DATE('09-12-1982', 'DD-MM-YYYY'), 3000, NULL, 20);

INSERT INTO EMP VALUES
        (7839, 'KING',   'PRESIDENT', NULL,
        TO_DATE('17-11-1981', 'DD-MM-YYYY'), 5000, NULL, 10);

INSERT INTO EMP VALUES
        (7844, 'TURNER', 'SALESMAN',  7698,
        TO_DATE('08-09-1981', 'DD-MM-YYYY'),  1500, NULL, 30);

INSERT INTO EMP VALUES
        (7876, 'ADAMS',  'CLERK',     7788,
        TO_DATE('12-01-1983', 'DD-MM-YYYY'), 1100, NULL, 20);

INSERT INTO EMP VALUES
        (7900, 'JAMES',  'CLERK',     7698,
        TO_DATE('03-12-1981', 'DD-MM-YYYY'),   950, NULL, 30);

INSERT INTO EMP VALUES
        (7902, 'FORD',   'ANALYST',   7566,
        TO_DATE('03-12-1981', 'DD-MM-YYYY'),  3000, NULL, 20);

INSERT INTO EMP VALUES
        (7934, 'MILLER', 'CLERK',     7782,
        TO_DATE('23-01-1982', 'DD-MM-YYYY'), 1300, NULL, 10);

1 个答案:

答案 0 :(得分:0)

嗨逆转你的案例逻辑,我测试了它,发现它正在工作

SELECT 
--  XMLELEMENT("ul",null,
       (SELECT DBMS_XMLGEN.getXMLType(
     DBMS_XMLGEN.newContextFromHierarchy('
        SELECT LEVEL,
           case
             when CONNECT_BY_ISLEAF = 1 then
              XMLELEMENT("ul", XMLELEMENT("li", xmlattributes(empno AS "id"), ename))
             when CONNECT_BY_ISLEAF = 0 then
              XMLELEMENT("ul", XMLELEMENT("li", xmlattributes(empno AS "id"), ename), null)
           end
          FROM emp
         START WITH mgr is null
        CONNECT BY PRIOR empno = mgr '
        )
       ) FROM DUAL
      )
--      ) XMLDOC  
 FROM DUAL;

请检查您需要的输出与问题中指定的完全一致。

按照我的说法,输出应该如下:

<ul>
  <li id="7839">KING</li>
  <ul>
    <li id="7566">JONES</li>
    <ul>
      <li id="7788">SCOTT</li>
      <ul>
        <li id="7876">ADAMS</li>
      </ul>
    </ul>
    <ul>
      <li id="7902">FORD</li>
      <ul>
        <li id="7369">SMITH</li>
      </ul>
    </ul>
  </ul>
  <ul>
    <li id="7698">BLAKE</li>
    <ul>
      <li id="7499">ALLEN</li>
    </ul>
    <ul>
      <li id="7521">WARD</li>
    </ul>
    <ul>
      <li id="7654">MARTIN</li>
    </ul>
    <ul>
      <li id="7844">TURNER</li>
    </ul>
    <ul>
      <li id="7900">JAMES</li>
    </ul>
  </ul>
  <ul>
    <li id="7782">CLARK</li>
    <ul>
      <li id="7934">MILLER</li>
    </ul>
  </ul>
</ul>

检查并更新是否符合您的要求。