我有一个保存在变量中的clob xml文件。 我想获得每个员工的数据。
例如我的代码
DECLARE
V_CLOB CLOB;
V_TEMP VARCHAR2(4000);
BEGIN
With data as (
SELECT
to_clob('<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
<Employee_Info>
<Employee>
<Name>Masashi Okamura</Name>
<Department>Design Department</Department>
<Telephone>03-1452-4567</Telephone>
<Email>okamura@xmltr.co.jp</Email>
<Salary>3800</Salary>
</Employee>
<Employee>
<Name>John Johnson</Name>
<Department>Design Department</Department>
<Telephone>03-1112-3517</Telephone>
<Email>johnson@gmail.co.jp</Email>
<Salary>4100</Salary>
</Employee>
</Employee_Info>
<TOTAL>7900</TOTAL>
<NO_OF_EMPLOYEES>2</NO_OF_EMPLOYEES>
</ROOT>') AS XML_FILE
FROM DUAL)
SELECT *
INTO V_CLOB
FROM DATA;
--LOOP employee records and insert in a table
END;
/
我想第一次来
' <Employee>
<Name>Masashi Okamura</Name>
<Department>Design Department</Department>
<Telephone>03-1452-4567</Telephone>
<Email>okamura@xmltr.co.jp</Email>
<Salary>3800</Salary>
</Employee>'
然后第二次
' <Employee>
<Name>John Johnson</Name>
<Department>Design Department</Department>
<Telephone>03-1112-3517</Telephone>
<Email>johnson@gmail.co.jp</Email>
<Salary>4100</Salary>
</Employee>'
你能帮帮我吗?
提前致谢
答案 0 :(得分:1)
首先,我将使用XMLType
而不是CLOB。如果你真的想使用clob,只需替换&#34; V_XML&#34;的每一个实例。在我的例子中使用&#34; XMLTYPE.createXML(V_CLOB)&#34;
我在这里使用SQL XML函数,因为我使用的不仅仅是PL / SQL XML函数,但有些人可能会有更好的答案使用它们。有关此主题的详细信息,请参阅the XML DB Developer's Guide。
DECLARE
V_XML XMLTYPE;
V_EMP_COUNT NUMBER;
V_QUERY VARCHAR2(100);
V_TEMP VARCHAR2(4000);
BEGIN
With data as (
SELECT
to_clob('<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
<Employee_Info>
<Employee>
<Name>Masashi Okamura</Name>
<Department>Design Department</Department>
<Telephone>03-1452-4567</Telephone>
<Email>okamura@xmltr.co.jp</Email>
<Salary>3800</Salary>
</Employee>
<Employee>
<Name>John Johnson</Name>
<Department>Design Department</Department>
<Telephone>03-1112-3517</Telephone>
<Email>johnson@gmail.co.jp</Email>
<Salary>4100</Salary>
</Employee>
</Employee_Info>
<TOTAL>7900</TOTAL>
<NO_OF_EMPLOYEES>2</NO_OF_EMPLOYEES>
</ROOT>') AS XML_FILE
FROM DUAL)
SELECT XMLTYPE.createXML(XML_FILE)
INTO V_XML
FROM DATA;
select nvl(XMLQUERY('/ROOT/Employee_Info/count(Employee)' PASSING V_XML RETURNING CONTENT).getNumberVal(),0)
into V_EMP_COUNT from dual;
for i in 1..V_EMP_COUNT LOOP
--LOOP employee records and insert in a table
v_query := '/ROOT/Employee_Info/Employee['|| i || ']';
SELECT xmlquery(v_query PASSING V_XML RETURNING CONTENT).getStringVal() into V_TEMP from dual;
--insert into my_table (my_col) values (v_temp);
dbms_output.put_line(v_temp);
END LOOP;
END;
/
答案 1 :(得分:1)
您不需要PL / SQL - 可以使用XMLTABLE在SQL中完成:
Oracle 11g R2架构设置:
CREATE TABLE data ( value XMLTYPE ) -- or CLOB instead of XMLTYPE
/
INSERT INTO data ( value )
SELECT Employee -- or t.Employee.getClobVal() if using a CLOB
FROM XMLTABLE(
'/ROOT/Employee_Info/Employee'
PASSING XMLTYPE( '<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
<Employee_Info>
<Employee>
<Name>Masashi Okamura</Name>
<Department>Design Department</Department>
<Telephone>03-1452-4567</Telephone>
<Email>okamura@xmltr.co.jp</Email>
<Salary>3800</Salary>
</Employee>
<Employee>
<Name>John Johnson</Name>
<Department>Design Department</Department>
<Telephone>03-1112-3517</Telephone>
<Email>johnson@gmail.co.jp</Email>
<Salary>4100</Salary>
</Employee>
</Employee_Info>
<TOTAL>7900</TOTAL>
<NO_OF_EMPLOYEES>2</NO_OF_EMPLOYEES>
</ROOT>' )
COLUMNS Employee XMLTYPE PATH '.'
) t
/
查询1 :
SELECT d.value.getClobVal()
FROM DATA d
<强> Results 强>:
| D.VALUE.GETCLOBVAL() |
|-----------------------------------------------|
| <Employee> |
| <Name>Masashi Okamura</Name> |
| <Department>Design Department</Department> |
| <Telephone>03-1452-4567</Telephone> |
| <Email>okamura@xmltr.co.jp</Email> |
| <Salary>3800</Salary> |
| </Employee> |
|-----------------------------------------------|
| <Employee> |
| <Name>John Johnson</Name> |
| <Department>Design Department</Department> |
| <Telephone>03-1112-3517</Telephone> |
| <Email>johnson@gmail.co.jp</Email> |
| <Salary>4100</Salary> |
| </Employee> |