2014-03-26 60 views
1

解析XML時,逃避我有一個簡單的表是這樣的:壞性格從Oracle CLOB

MyTable (id number(10), configuration CLOB) 
在Oracle 11g中(11.2.0.1.0)

在Windows上運行。

現在,這裏是我的代碼:

declare 
vconfiguration clob; 

vxml xmltype; 
vstring varchar(255); 
begin 
    select configuration into vconfiguration from mytable where id=1; 

    vxml := xmltype(vconfiguration); 

    dbms_output.put_line(vconfiguration); 

    vstring := vxml.extract('/SmtpConfiguration/From/text()','xmlns="http://www.blabla.com/Drivers/Smtp"').getStringVal(); 
    dbms_output.put_line('From=' || vstring); 
end; 

這裏是輸出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<SmtpConfiguration 
xmlns="http://www.blabla.com/Drivers/Smtp"> 

<From>"[email protected]"</From> 
    <MultiRecipients>TO</MultiRecipients> 

<ServerAddress>smtp_server1</ServerAddress> 
    <ServerPort>25</ServerPort> 

<UseAuthentication>false</UseAuthentication> 

<UseSSL>false</UseSSL> 
</SmtpConfiguration> 

From=&quot;[email protected]&quot; 

我想要做的是一樣的東西insert into anothertable(from) values (vstring);

如何獲得的從價值用適當的標籤「而不是&」;?

回答

1

您可以改用extractvalue(); from the documentation

SQL函數extractValueXMLType方法getStringVal()在其治療實體編碼的不同。函數extractValue unescapes任何編碼的實體;方法getStringVal()返回具有完整實體編碼的數據。的

因此而不是調用提取物,你可以做到這一點,這需要一個查詢,因爲它不是一個函數PL/SQL承認本身:

select extractValue(vxml, '/SmtpConfiguration/From/text()', 
    'xmlns="http://www.blabla.com/Drivers/Smtp"') 
into vstring 
from dual; 
dbms_output.put_line('From=' || vstring); 

這使輸出:

From="[email protected]" 
0

你也可以這樣做DBMS_XMLGEN.CONVERT

declare 
    vconfiguration clob; 

    vxml   xmltype; 
    vstring  varchar (255); 
begin 

    vconfiguration := '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <SmtpConfiguration 
    xmlns="http://www.blabla.com/Drivers/Smtp"> 
    <From>"[email protected]"</From> 
     <MultiRecipients>TO</MultiRecipients> 
    <ServerAddress>smtp_server1</ServerAddress> 
     <ServerPort>25</ServerPort> 
    <UseAuthentication>false</UseAuthentication> 
    <UseSSL>false</UseSSL> 
    </SmtpConfiguration>'; 

    vxml := xmltype (vconfiguration); 
    DBMS_OUTPUT.put_line (vconfiguration); 

    vstring := 
    DBMS_XMLGEN.CONVERT (
     vxml.EXTRACT ('/SmtpConfiguration/From/text()','xmlns="http://www.blabla.com/Drivers/Smtp"').getStringVal() 
    ,DBMS_XMLGEN.ENTITY_DECODE); 
    DBMS_OUTPUT.put_line ('From=' || vstring); 
end;