2012-02-15 120 views
0

我們使用基於Java的應用程序來部署XML文件,這些文件被寫入Oracle數據庫。 db中的XML blob存儲爲NCLOB數據類型。我將如何從數據庫獲取NCLOB XML數據並將其轉換回XML格式?我需要使用Java(我是一個完整的Java noob,BTW),還是我可以在這裏使用PowerShell(我的首選項)?取決於方法,我將如何做到這一點?如何從Oracle數據庫獲取NCLOB XML數據

我想這樣做的原因主要是前/後部署驗證(比較部署之前和之後的XML內容)。

由於提前, 基思

回答

2

這是我在過去所做的那樣使用ODAC在甲骨文CLOB讀/寫XML。這應該適用於NCLOB,只需很少的修改。

# Load ODAC. This might fail if it is not installed or is the wrong bitness. 
$assembly = [System.Reflection.Assembly]::LoadWithPartialName("Oracle.DataAccess") 

# Connect to Oracle. 
$connStr = 'Data Source=hostname:1521/sidname; User Id=sys; Password=password; DBA Privilege=SYSDBA;' 
$conn = New-Object Oracle.DataAccess.Client.OracleConnection -ArgumentList $connStr 
$conn.Open() 

# Query the table. 
$q = "select MY_CLOB_FIELD from My_Table" 
$command = new-object Oracle.DataAccess.Client.OracleCommand($q, $conn) 

# Process records. 
$reader = $command.ExecuteReader() 
while ($reader.Read()) { 
    # Read the CLOB field and cast to an XML document. 
    $xmlDoc = [xml] $reader.getstring(0) # XML 

    #... XML Processing Here .... 

    # Commit the updated XML. 
    $sql = "UPDATE My_Table SET MY_CLOB_FIELD = :1" 
    $updateCmd = New-Object Oracle.DataAccess.Client.OracleCommand ($sql, $conn) 

    $param = New-Object Oracle.DataAccess.Client.OracleParameter (
     "xml", #Name 
     [Oracle.DataAccess.Client.OracleDbType]::Clob, #Type 
     $xmlDoc.OuterXml, #Data 
     'Input' #Direction 
    ) 

    $newParam = $updateCmd.Parameters.Add($param) 
    $result = $updateCmd.ExecuteNonQuery() 
} 
+0

謝謝安迪。我會試試你的解決方案。快速的問題,我通過DataSource傳遞什麼信息?在我的PowerShell腳本中,我通常使用sqlplus語法(例如'username/password @ // hostname:Port/SID')連接到我們的dB。如何將此連接字符串轉換爲在您的腳本中使用? – Keith 2012-02-16 03:30:32

+0

@Keith查看你在'.NET Framework數據提供者for Oracle'部分下的選項[here](http://www.connectionstrings.com/oracle)。 '數據源'是一個TNS別名,但你不必使用它。看看'省略tnsnames.ora'選項。 – 2012-02-16 03:40:30

+0

@Keith別忘了,你需要安裝ODAC才能使用'Oracle.DataAccess'。 – 2012-02-16 03:41:28

0

這是我最後使用的代碼:

$Assembly = [System.Reflection.Assembly]::LoadWithPartialName("System.Data.OracleClient") 

$conn = New-Object System.Data.OracleClient.OracleConnection(` 
「Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=hostname)(PORT=port)) ` 
(CONNECT_DATA=(SERVICE_NAME=SID)));User Id=username;Password=password;」); 

$conn.Open() 

# Query the table. 
$q = "SELECT column FROM table WHERE column='something'" 
$command = New-Object System.Data.OracleClient.OracleCommand ($q, $conn) 

$xmlfile = "c:\temp\xml\temp.xml" 
# Process records. 
$reader = $command.ExecuteReader() 
while ($reader.Read()) 
{ 
    # Read the NCLOB field and cast to an XML document. 
    $xmlDoc = [xml] $reader.getstring(0) # XML 
    $xmlDoc.Save($xmlfile) 
} 
$conn.Close() 

@Andy,用於操縱我的方向是正確的感謝! :-)

+0

被警告 - [System.Data.OracleClient中的類型已被棄用,並且將在未來的.NET Framework版本中被刪除。](http://msdn.microsoft.com/zh-cn/library/system.data .oracleclient.aspx) – 2012-02-16 23:23:59

+0

當它!我沒有注意到這一點。感謝您的高舉。無論如何,我沿着這條路走下去的原因是因爲我不斷嘗試使用'Oracle.DataAccess.Client.OracleConnection'進行連接。這裏是我碰到錯誤之前:'$ assembly = [System.Reflection.Assembly] :: LoadWithPartialName(「Oracle.DataAccess」)' '$ conn = New-Object Oracle.DataAccess.Client.OracleConnection(數據源=(描述=(地址=(協議= TCP)(主機=主機名)(端口=端口))'' (CONNECT_DATA =(SERVICE_NAME = SID))); User Id = username; Password =密碼;「);' – Keith 2012-02-17 00:09:20

+0

這裏是錯誤信息:'New-Object:帶有」1「參數的調用」.ctor「的異常:」試圖加載格式不正確的程序。「 – Keith 2012-02-17 00:10:53