我有一个List,里面有一个列表类型属性(Address)。我将整个信息作为xml发送到数据库中,以便在我的oracle表中更新/插入详细信息。
这是xml格式
DECLARE
x XMLType := XMLType( '
<person>
<row>
<name>Tom</name>
<Address>
<LocalAddress>
<State>California</State>
<City>Los angeles</City>
</LocalAddress>
<LocalAddress>
<State>California1</State>
<City>Los angeles1</City>
</LocalAddress>
</Address>
</row>
<row>
<name>Jim</name>
<Address>
<LocalAddress>
<State>California</State>
<City>Los angeles</City>
</LocalAddress>
</Address>
</row>
</person>');
v_person_name varchar2(1000);
v_city varchar2(1000);
BEGIN
FOR xmlrow IN
(SELECT column_value AS xml
FROM TABLE(XMLSequence(x.extract('/person/row')))
)
LOOP
SELECT extractValue(xmlrow,'/row/name/text()')
INTO v_person_name
FROM dual;
DBMS_OUTPUT.put_line(v_person_name);
FOR address IN
(SELECT column_value AS xml
FROM TABLE(xmlsequence(xmlrow.extract('/row/Address/LocalAddress')))
)
LOOP
-- extract address values same as above.
SELECT extractValue(xmlrow,'/LocalAddress/City/text()')
INTO v_city
FROM dual;
DBMS_OUTPUT.put_line(v_city);
END LOOP;
END LOOP;
END;
如何在oracle中解析整个xml?我发送这个作为xmltype变量。
它给了我一个编译错误
Error report -
ORA-06550: line 35, column 27:
PLS-00382: expression is of wrong type
ORA-06550: line 35, column 14:
PL/SQL: ORA-00932: inconsistent datatypes: expected - got -
ORA-06550: line 35, column 7:
PL/SQL: SQL Statement ignored
ORA-06550: line 41, column 37:
PLS-00302: component 'EXTRACT' must be declared
ORA-06550: line 41, column 37:
PLS-00302: component 'EXTRACT' must be declared
ORA-06550: line 41, column 30:
PL/SQL: ORA-00904: "XMLROW"."EXTRACT": invalid identifier
ORA-06550: line 40, column 7:
PL/SQL: SQL Statement ignored
ORA-06550: line 45, column 29:
PLS-00382: expression is of wrong type
ORA-06550: line 45, column 16:
PL/SQL: ORA-00932: inconsistent datatypes: expected - got -
ORA-06550: line 45, column 9:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
请帮我解决这个问题。 提前谢谢。
答案 0 :(得分:2)
我能够使用此方法解析嵌套的xml
SET serveroutput ON format wrapped;
DECLARE
x XMLType := XMLType('
<person>
<row>
<name>Tom</name>
<Address>
<LocalAddress>
<State>California</State>
<City>Los angeles</City>
</LocalAddress>
<LocalAddress>
<State>California1</State>
<City>Los angeles1</City>
</LocalAddress>
</Address>
</row>
<row>
<name>Jim</name>
<Address>
<LocalAddress>
<State>California</State>
<City>Los angeles</City>
</LocalAddress>
</Address>
</row>
</person>');
BEGIN
FOR r IN
(SELECT ExtractValue(Value(p),'/row/name/text()') AS name ,
Extract(Value(p),'/row/Address') As Address
FROM TABLE(XMLSequence(Extract(x,'/person/row'))) p
)
LOOP
DBMS_OUTPUT.put_line(r.name);
FOR row1 IN
(SELECT ExtractValue(Value(l),'/LocalAddress/City/text()') AS city
FROM TABLE(XMLSequence(Extract(r.Address,'/Address/LocalAddress'))) l
)
LOOP
DBMS_OUTPUT.put_line(row1.city);
-- do whatever you want with r.name, r.state, r.city
END LOOP;
-- do whatever you want with r.name, r.state, r.city
END LOOP;
END;
答案 1 :(得分:0)
当您使用XML类型时,我假设您需要访问单个标记及其各自的值。下面是一段代码示例,它将根据上面的XML结构从每一行中选择名称。注意:xml_in只是存储过程中xmltype变量的变量名。
select extractValue(column_value,'/row/name/text()') as name
from table (XMLSequence(xml_in.extract('/person/row')));
如果您需要进行嵌套解析,您也可以这样做:
for xmlrow in (select column_value as xml from table(xmlsequence(xml_in.extract('/person/row'))))) loop
select extractValue(xmlrow.xml,'/row/name/text()')
into v_person_name
from dual;
for address in (select column_value as xml from table(xmlsequence(xmlrow.xml.extract('/row/Address/LocalAddress')))) loop
-- extract address values same as above.
end loop;
end loop;