Nigel Charman
2005-07-19 09:29:25 UTC
I am having trouble writing XML data to an XML column using a
PreparedStatement, when the XML data contains the XML declaration
<?xml version="1.0" encoding="UTF-8"?>.
As an example, the following java code succeeds:
Statement stmt = con.createStatement();
stmt.execute("Insert into xmltab VALUES('<?xml version=\"1.0\"
encoding=\"UTF-8\"?><root><a/></root>')");
while this version fails:
PreparedStatement ps = con.prepareStatement("Insert into xmltab
VALUES(?)");
ps.setString(1, "<?xml version=\"1.0\"
encoding=\"UTF-8\"?><root><a/></root>");
ps.execute();
with the exception:
com.microsoft.sqlserver.jdbc.SQLServerException: XML parsing: line 1,
character 38, unable to switch the encoding
The PreparedStatement succeeds if either:
i) the encoding in the XML declaration is set to "UTF-16", or
ii) the encoding attribute in the XML declaration is omitted (in my
understanding, the XML document encoding should then default to UTF-8.
It seems strange that I am seeing different results between having no
encoding attribute and having the encoding set to UTF-8).
In the bigger picture, I am trying to read the XML data from file, or
other source, and would like to be able to pass the XML data as a stream
to the PreparedStatement. I am getting the same "unable to switch the
encoding" exception when using PreparedStatement.setCharacterStream()
method, and am getting the exception "Operand type clash: image is
incompatible with xml" when using PreparedStatement.setBinaryStream(
).
Can anyone shed any light on how I can write UTF-8 encoded XML data to
SQL Server when using a PreparedStatement?
I am using the SQL Server Express June CTP with default setup, and the
JDBC driver with the URL
"jdbc:sqlserver://serverName/trustedAuthentication=true".
Regards,
Nigel Charman
PreparedStatement, when the XML data contains the XML declaration
<?xml version="1.0" encoding="UTF-8"?>.
As an example, the following java code succeeds:
Statement stmt = con.createStatement();
stmt.execute("Insert into xmltab VALUES('<?xml version=\"1.0\"
encoding=\"UTF-8\"?><root><a/></root>')");
while this version fails:
PreparedStatement ps = con.prepareStatement("Insert into xmltab
VALUES(?)");
ps.setString(1, "<?xml version=\"1.0\"
encoding=\"UTF-8\"?><root><a/></root>");
ps.execute();
with the exception:
com.microsoft.sqlserver.jdbc.SQLServerException: XML parsing: line 1,
character 38, unable to switch the encoding
The PreparedStatement succeeds if either:
i) the encoding in the XML declaration is set to "UTF-16", or
ii) the encoding attribute in the XML declaration is omitted (in my
understanding, the XML document encoding should then default to UTF-8.
It seems strange that I am seeing different results between having no
encoding attribute and having the encoding set to UTF-8).
In the bigger picture, I am trying to read the XML data from file, or
other source, and would like to be able to pass the XML data as a stream
to the PreparedStatement. I am getting the same "unable to switch the
encoding" exception when using PreparedStatement.setCharacterStream()
method, and am getting the exception "Operand type clash: image is
incompatible with xml" when using PreparedStatement.setBinaryStream(
).
Can anyone shed any light on how I can write UTF-8 encoded XML data to
SQL Server when using a PreparedStatement?
I am using the SQL Server Express June CTP with default setup, and the
JDBC driver with the URL
"jdbc:sqlserver://serverName/trustedAuthentication=true".
Regards,
Nigel Charman