我能够将值从表1插入表2并成功执行PL / SQL过程,但是输出有点笨拙。我不知道为什么?
下面是代码:
create table airports_2_xml
(
airport xmltype
);
declare
cursor insert_xml_cr is select * from airports_1_orcl;
begin
for i in insert_xml_cr
loop
insert into airports_2_xml values
(
xmlelement("OneAirport",
xmlelement("Rank", i.Rank) ||
xmlelement("airport",i.airport) ||
xmlelement("Location",i.Location) ||
xmlelement("Country", i.Country) ||
xmlelement("Code_iata",i.code_iata) ||
xmlelement("Code_icao", i.code_icao) ||
xmlelement("Total_Passenger",i.Total_Passenger) ||
xmlelement("Rank_change", i.Rank_change) ||
xmlelement("Percent_Change", i.Percent_change)
));
end loop;
end;
/
select * from airports_2_xml;
输出:
为什么在输出中显示&lt,&gt ;?为什么我看不到完整的输出?
预期输出:
<OneAirport>
<Rank>3</Rank>
<Airport>Dubai International</Airport>
<Location>Garhoud</Location>
<Country>United Arab Emirates</Country>
<Code_IATA>DXB</Code_IATA>
<Code_ICAO>OMDB</Code_ICAO>
<Total_passenger>88242099</Total_passenger>
<Rank_change>0</Rank_change>
<Percent_Change>5.5</Percent_Change>
</OneAirport>
答案 0 :(得分:2)
主要问题是如何构造XML。您有一个OneAirport
的外部XMLElement,该元素的内容是一个字符串。
您正在从游标字段中生成单个XMLElement,但是随后将它们并置在一起,这将为您提供单个字符串,该字符串仍具有您期望的尖括号。因此,您正在尝试做一些简化的事情:
select
xmlelement("OneAirport", '<Rank>1</Rank><airport>Hartsfield-Jackson</airport>')
from dual;
XMLELEMENT("ONEAIRPORT",'<RANK>1</RANK><AIRPORT>HARTSFIELD-JACKSON</AIRPORT>')
--------------------------------------------------------------------------------
<OneAirport><Rank>1</Rank><airport>Hartsfield-Jackson</airp
,默认情况下,XMLElement()会将传入值中的实体转义,因此尖括号将转换为<
之类的“安全”等价物。如果没有这样做,或者您用noentityescaping
告诉了它不要这样做:
select xmlelement(noentityescaping "OneAirport", '<Rank>1</Rank><airport>Hartsfield-Jackson</airport>')
from dual;
XMLELEMENT(NOENTITYESCAPING"ONEAIRPORT",'<RANK>1</RANK><AIRPORT>HARTSFIELD-JACKS
--------------------------------------------------------------------------------
<OneAirport><Rank>1</Rank><airport>Hartsfield-Jackson</airport></OneAirport>
然后看起来会更好,但是实际上您仍然拥有一个带有单个字符串的元素(其字符很可能会造成问题),而不是您几乎肯定要使用的XML结构。
获取zctual结构的简单方法是使用XMLForest():
xmlelement("OneAirport",
xmlforest(i.Rank, i.airport, i.Location, i.Country, i.code_iata,
i.code_icao, i.Total_Passenger, i.Rank_change, i.Percent_change)
)
您不需要游标循环或任何PL / SQL;您可以这样做:
insert into airports_2_xml (airport)
select xmlelement("OneAirport",
xmlforest(i.Rank, i.airport, i.Location, i.Country, i.code_iata,
i.code_icao, i.Total_Passenger, i.Rank_change, i.Percent_change)
)
from airports_1_orcl i;
第二个问题是显示。如果您发出一些格式化命令,您将看到更多数据,例如:
set lines 120
set long 32767
set longchunk 32767
这将告诉您的客户端检索并显示更多的长数据(此处为XMLType),而不是现在显示的默认80个字符。
一旦生成嵌套的XML结构,就可以在查询第二张表时使用XMLSerialize()以使其更具可读性。
答案 1 :(得分:0)
在下面的代码块尝试:
declare
cursor insert_xml_cr is select * from airports_1_orcl;
v_airport_xml SYS.XMLTYPE;
begin
for i in insert_xml_cr
loop
SELECT XMLELEMENT ( "OneAirport",
XMLFOREST(i.Rank as "Rank"
,i.airport as "Airport"
,i.Location as "Location"
,i.Country as "Country"
,i.code_iata as "Code_iata"
,i.code_icao as "code_icao"
,i.Total_Passenger as "Total_Passenger"
, i.Rank_change as "Rank_change"
,i.Percent_change as "Percent_Change"
))
into v_airport_xml
FROM DUAL;
insert into airports_2_xml values (v_airport_xml);
end loop;
end;