我们使用基于Java的应用程序来部署XML文件,这些文件被写入Oracle数据库。 db中的XML blob存储为NCLOB数据类型。我如何从数据库中获取NCLOB XML数据并返回XML格式?我需要使用Java(我是一个完整的Java noob,BTW),还是我可以在这里使用PowerShell(我的偏好)?根据方法,我该怎么做?
我想这样做的原因主要是部署前/后验证(比较部署之前和之后的XML内容)。
提前致谢, 基思
答案 0 :(得分:2)
以下我使用ODAC在Oracle 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()
}
答案 1 :(得分: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,谢谢你指引我正确的方向! : - )