- 浏览: 681134 次
- 性别:
- 来自: 中山
文章分类
最新评论
-
wuhuizhong:
jFinal支持Rest风格吗?可以想spring mvc那样 ...
在JFinal的Controller中接收json数据 -
wuhuizhong:
在jfinal中应如何获取前端ajax提交的Json数据?ht ...
在JFinal的Controller中接收json数据 -
wuhuizhong:
jfinal如何处理json请求的数据:问题: 在某些api接 ...
在JFinal的Controller中接收json数据 -
wuhuizhong:
Ubuntu14.04 安装 Oracle 11g R2 Ex ...
Oracle 11g release 2 XE on Ubuntu 14.04 -
alanljj:
这个很实用,已成功更新,谢过了!
odoo薪酬管理模块l10n_cn_hr_payroll
Consider the following classic employees table:
SQL> SELECT employee_id,
2 manager_id,
3 first_name,
4 salary,
5 hire_date
6 FROM employees;
EMPLOYEE_ID MANAGER_ID FIRST_NAME SALARY HIRE_DATE
----------- ---------- -------------------- ---------- ---------
100 Steven 24000 17-JUN-87
101 100 Neena 17000 21-SEP-89
102 100 Lex 17000 13-JAN-93
103 102 Alexander 9000 03-JAN-90
104 103 Bruce 6000 21-MAY-91
105 103 David 4800 25-JUN-97
106 103 Valli 4800 05-FEB-98
107 103 Diana 4200 07-FEB-99
108 101 Nancy 12000 17-AUG-94
109 108 Daniel 9000 16-AUG-94
110 108 John 8200 28-SEP-97
...
The manager_id is also an employee_id. So, we can build the following hierarchical query:
SQL> SELECT
2 rpad(' ', 4 * LEVEL -1, ' ') || first_name tree,
3 LEVEL,
4 employee_id,
5 manager_id,
6 salary,
7 hire_date
8 FROM employees
9 START WITH manager_id IS NULL
10 CONNECT BY PRIOR employee_id = manager_id
11 ORDER siblings BY first_name;
TREE LEVEL EMPLOYEE_ID MANAGER_ID SALARY HIRE_DATE
-------------------- ---------- ----------- ---------- ---------- ---------
Steven 1 100 24000 17-JUN-87
Adam 2 121 100 8200 10-APR-97
Alexis 3 185 121 4100 20-FEB-97
Anthony 3 187 121 3000 07-FEB-99
James 3 131 121 2500 16-FEB-97
Julia 3 186 121 3400 24-JUN-98
Laura 3 129 121 3300 20-AUG-97
Mozhe 3 130 121 2800 30-OCT-97
Nandita 3 184 121 4200 27-JAN-96
TJ 3 132 121 2100 10-APR-99
Alberto 2 147 100 12000 10-MAR-97
...
Now what we want is to transform the above result set to XML. We could use DBMS_XMLGEN.getxml
like this:
SQL> SELECT DBMS_XMLGEN.getxml('
2 SELECT
3 employee_id,
4 manager_id,
5 first_name,
6 salary,
7 hire_date
8 FROM employees
9 START WITH manager_id IS NULL
10 CONNECT BY PRIOR employee_id = manager_id
11 ORDER siblings BY first_name
12 ') xml
13 FROM dual;
XML
----------------------------------------------------
<?xml version="1.0"?>
<ROWSET>
<ROW>
<EMPLOYEE_ID>100</EMPLOYEE_ID>
<FIRST_NAME>Steven</FIRST_NAME>
<SALARY>24000</SALARY>
<HIRE_DATE>17-JUN-87</HIRE_DATE>
</ROW>
<ROW>
<EMPLOYEE_ID>121</EMPLOYEE_ID>
<MANAGER_ID>100</MANAGER_ID>
<FIRST_NAME>Adam</FIRST_NAME>
<SALARY>8200</SALARY>
<HIRE_DATE>10-APR-97</HIRE_DATE>
</ROW>
...
</ROWSET>
But this is not what we want. We want the hierarchical structure to be maintained in the XML output. In other words, we would like to have hierarchical XML with recursive elements.
Starting with Oracle Database 10g Release 1 (10.0.1), we can use DBMS_XMLGEN.newContextFromHierarchy
like this:
SQL> SELECT XMLELEMENT
2 ("employees",
3 (SELECT DBMS_XMLGEN.getxmltype
4 (DBMS_XMLGEN.newcontextfromhierarchy
5 ('SELECT level,
6 XMLElement("employee",
7 XMLElement("id", employee_id),
8 XMLElement("name", first_name),
9 XMLElement("salary", salary),
10 XMLElement("hiredate", hire_date))
11 FROM employees
12 START WITH manager_id is null
13 CONNECT BY PRIOR employee_id = manager_id
14 ORDER SIBLINGS BY first_name'))
15 FROM DUAL)) xmldoc
16 FROM DUAL;
XMLDOC
-----------------------------------------------------------------------
<employees>
<employee>
<id>100</id>
<name>Steven</name>
<salary>24000</salary>
<hiredate>1987-06-17</hiredate>
<employee>
<id>121</id>
<name>Adam</name>
<salary>8200</salary>
<hiredate>1997-04-10</hiredate>
<employee>
<id>185</id>
<name>Alexis</name>
<salary>4100</salary>
<hiredate>1997-02-20</hiredate>
</employee>
</employee>
...
</employees>
And that’s what we want.
Here is what the documentation says about the DBMS_XMLGEN.newContextFromHierarchy
function:
newContextFromHierarchy (queryString IN VARCHAR2) RETURN ctxHandle;
Parameter: queryString (IN) – the query string, the result of which must be converted to XML. The query is a hierarchical query typically formed using a CONNECT BY clause, and the result must have the same property as the result set generated by a CONNECT BY query. The result set must have only two columns, the level number and an XML value. The level number is used to determine the hierarchical position of the XML value within the result XML document.
Returns: Context handle. Call this function first to obtain a handle that you can use in the getXML() and other functions to get a hierarchical XML with recursive elements back from the result.
Since the function returns a context handle, it is a good practice to
close the context to release all resources associated with that
context, including the SQL cursor and bind and define buffers, and so
on. This can be done using the procedure DBMS_XMLGEN.closeContext
and requires the use of PL/SQL:
SQL> CREATE TABLE xml_documents OF XMLTYPE
2 /
Table created.
SQL> DECLARE
2 qryctx DBMS_XMLGEN.ctxhandle;
3 BEGIN
4 qryctx :=
5 DBMS_XMLGEN.newcontextfromhierarchy
6 ('SELECT level,
7 XMLElement("employee",
8 XMLElement("id", employee_id),
9 XMLElement("name", first_name),
10 XMLElement("salary", salary),
11 XMLElement("hiredate", hire_date))
12 FROM employees
13 START WITH manager_id is null
14 CONNECT BY PRIOR employee_id=manager_id
15 ORDER SIBLINGS BY first_name');
16
17 INSERT INTO xml_documents
18 SELECT XMLELEMENT ("emloyees", DBMS_XMLGEN.getxmltype (qryctx))
19 FROM DUAL;
20
21 DBMS_XMLGEN.closecontext (qryctx);
22 END;
23 /
PL/SQL procedure successfully completed.
SQL> SELECT sys_nc_rowinfo$
2 FROM xml_documents
3 /
SYS_NC_ROWINFO$
------------------------------------------------------------------------------
<employees>
<employee>
<id>100</id>
<name>Steven</name>
<salary>24000</salary>
<hiredate>1987-06-17</hiredate>
<employee>
<id>121</id>
<name>Adam</name>
<salary>8200</salary>
<hiredate>1997-04-10</hiredate>
<employee>
<id>185</id>
<name>Alexis</name>
<salary>4100</salary>
<hiredate>1997-02-20</hiredate>
</employee>
</employee>
...
</employees>
A couple of notes:
-
The
XMLType
datatype was first introduced in Oracle Database 9i Release 1 (9.0.1) -
Oracle XML DB is a feature of the Oracle Database. It provides a high-performance, native XML storage and retrieval technology. You can verify that XML DB has been installed by simply running the SQL below:
SQL> SELECT comp_name 2 FROM dba_registry 3 WHERE comp_name LIKE '%XML%'; COMP_NAME ------------------------------------ Oracle XML Database
Sources and Resources:
Related articles:
- Beware of Comments in SQL
- Producing XML from SQL using cursor expressions
- Cool SQL Analytic Function: NTILE
- How to select an xmltree recursive?
from: http://awads.net/wp/2007/02/20/an-easy-way-to-convert-a-hierarchical-query-result-to-xml/
发表评论
-
用函数unistr将Oracle数据库中的Unicode转换为中文
2016-07-19 11:51 7918例子: DECLARE V_EXT_DES V ... -
ORACLE APPLICATION EXPRESS 5.0 升级
2016-05-12 11:43 580Oracle11GR2 XE 缺省是安装了oracle ap ... -
Oracle ACL(Access Control List)
2016-05-12 11:36 889在oralce 11g中假如你想获取server的ip或者h ... -
了解systemstate dump
2016-04-26 14:09 487当数据库出现严重的性能问题或者hang了的时候,我们非常需要 ... -
通过ORACLE的UTL_HTTP工具包发送包含POST参数的请求
2016-03-18 16:25 5152DECLARE req utl_http. ... -
Shell: extract more from listener.log(分析监听日志)
2016-03-16 14:57 1148统计一天内每小时的session请求数 # fgrep ... -
ORA-01031: insufficient privileges 问题解决笔记
2016-02-01 15:53 1186A) File $Oracle_HOME/network/a ... -
listener.log中报Warning: Subscription For Node Down Event Still Pending问题的解决方法
2016-01-07 16:34 1633一套Oracle 10.2.0.1 for aix的数据库环 ... -
Oracle触发器和MySQL触发器之间的区别
2015-11-19 12:55 670Oracle触发器格式: CREATE [OR RE ... -
查询正在执行的存储过程
2015-11-13 09:27 20501、找正在执行的PROCEDURE的 sid ,serial# ... -
undo表空间损坏的处理过程
2015-10-14 13:49 1219磁碟陣列故障,分區/rman上包括undo和archivel ... -
登录oracle资料库时很久无反应的问题处理一例
2015-10-11 10:56 993原因是系统存在僵死的进程,促使session处于激活状态.首 ... -
TNS-12560问题解决
2015-10-01 19:52 613tnsping远程主机实例出现TNS-12560: TNS ... -
查看undo中sql语句的占用情况
2015-08-06 17:18 1764查看undo中sql语句的占用情况 select * ... -
Install Open System Architect And ODBC Instant Client
2015-05-21 14:03 749How to Install Open System Arc ... -
恢复oracle中用pl sql误删除drop掉的表
2015-04-03 16:12 553查看回收站中表 select object_name,or ... -
在Oracle Linux 6.6上安装Oracle 10gR2
2015-01-15 15:36 2681查看硬體配置 # df -h Filesystem ... -
kill
2015-01-03 11:36 457--根据某一对象查询进程 col owner fo ... -
Oracle 数据库Storage存储迁移笔记
2014-12-27 11:08 9861.确认数据文件、控制文件、临时文件、日志文件 位置 / ... -
異地備份資料庫的開啟步驟
2014-11-19 14:03 487使用EMC設備執行異地備份, 資料庫的複製是開啟的狀態下, ...
相关推荐
MicroMenu provides an easy-to-use hierarchical menu interface for AVR firmware written in the C language. It was written as a way to teach myself linked-lists, and I've cleaned it up and made it ...
- **层次神经自编码器**(Hierarchical Neural Autoencoder)是一种深度学习模型,它通过多层次结构来处理自然语言文本。 - **段落与文档**是该模型的主要处理对象。这意味着该模型旨在理解和表示包含多个句子的段落...
often semi- crystalline arrays and are assembled into highly organized multilamellar systems, an organization warranting a substantial degree of stability. At the same time, they exhibit remarkable ...
address the two issues, we propose Orion, a hybrid hierarchical control plane for large-scale networks. Orion can effectively reduce the computational complexity growth of SDN control plane from super...
BIRCH (balanced iterative reducing and clustering using hierarchies) is an unsupervised data mining algorithm used to perform hierarchical clustering over particularly large data-sets. An advantage of...
layers in a probabilistically sound way. Our experiments show that the algorithm learns useful high-level visual features, such as object parts, from unlabeled images of objects and natural scenes. We...
You can declare functions in a way that allows the compiler to expand them inline rather than calling them through the usual function call mechanism. Pros: Inlining a function can generate more ...
"This technique allows a program to save hierarchical information like the data in a TreeView in a way that is easy to understand."<END><br>11 , OLE.zip Demonstrates the use of OLE.<END><br>12 , ...
### 层次狄利克雷过程 (Hierarchical Dirichlet Process, HDP) #### 概述 层次狄利克雷过程(Hierarchical Dirichlet Process, HDP)是一种非参数贝叶斯模型,用于处理涉及多组数据的聚类问题。在HDP中,每组数据...
At a glance connections can be identified, paths discovered and data relationships grasped all in an easy to use format. TreeGX will allow you to present hierarchical data in new visually engaging ...
The purpose of the Action is to "Convert the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn_t otherwise because of incompatibility ...
We present a simple but powerful algorithm for optimizing the usage of hardware occlusion queries in arbitrary complex scenes. Our method minimizes the number of issued queries and reduces the delays ...
CHAMELEON A Hierarchical Clustering Algorithm :变色龙的层次聚类算法.ppt
7. InputStreamReader is a Java class used to convert a byte stream (InputStream) into a character stream (Reader), facilitating the reading of text data in different character encodings. 8. In Java, ...
<br>A mind mapper, and at the same time an easy-to-operate hierarchical editor with strong emphasis on folding. These two are not really two different things, just two different descriptions of a ...
就是这本书 http://www.ibmpressbooks.com/bookstore/product.asp?isbn=0131856715
Hierarchical Dirichlet Process简单介绍