简体   繁体   English

如何在SQL Server中读取此XML

[英]how to read this xml in sql server

I am having xml as result for webservice api . 我将xml作为webservice api的结果。 i need to parse the result and update to database table. 我需要解析结果并更新到数据库表。 my xml is below . 我的xml在下面。 it is a response text. 它是一个响应文本。

<?xml version="1.0" encoding="utf-8"?> 
<double>1</double>

Sqlserver 2008 Code : Sqlserver 2008代码:

    declare @xml xml, @rate DECIMAL(10,4)
  set @xml=REPLACE(@ResponseText ,'encoding="utf-8"','')
  select @rate= @xml.value('(/double)[1]','decimal')

I want to get the value of double but it always return the null . 我想获取double的值,但它始终返回null。

Please help me out . 请帮帮我。

Hi , have Done changes as per your suggestion still no getting. 嗨,根据您的建议进行了更改仍然没有得到。

 declare @xml XML
 DECLARE @responsetext VARCHAR(900)

 declare @rate DECIMAL(10,4)
 SET @responsetext = '<?xml version="1.0" encoding="utf-8"?>
  <double xmlns="http://www.webserviceX.NET/">1</double>'
 set @xml=REPLACE(@ResponseText ,'encoding="utf-8"','')
 select @rate= @xml.value('(/double)[1]','decimal')
 select @rate

You need to declare the namespace when querying using the value() Method. 使用value()方法查询时,需要声明名称空间。

Change the first parameter of value() from 将value()的第一个参数更改为

'(/double)[1]'

to

'declare namespace x="http://www.webserviceX.NET/"; (/x:double)[1]'

So the full example will look like this 因此完整的示例如下所示

declare @xml XML
 DECLARE @responsetext VARCHAR(900)

 declare @rate DECIMAL(10,4)
 SET @responsetext = '<?xml version="1.0" encoding="utf-8"?>
  <double xmlns="http://www.webserviceX.NET/">1</double>'
 set @xml=REPLACE(@ResponseText ,'encoding="utf-8"','')
 select @rate= @xml.value('declare namespace x="http://www.webserviceX.NET/"; (/x:double)[1]','decimal')
 select @rate

which should return 1.000 (decimal) 应返回1.000(十进制)

DECLARE @X XML = '<?xml version="1.0" encoding="utf-8"?> 
<double xmlns="http://www.webserviceX.NET/">1</double>'

SELECT @X.value ('declare namespace x="http://www.webserviceX.NET/"; (/x:double)[1]', 'decimal')

Updated to reflect your use of namespace; 更新以反映您对名称空间的使用; the general point is that you don't need to do string manipulation to make this work. 总的来说,您不需要进行字符串操作即可完成这项工作。 The header is fully supported. 标题完全受支持。 However, namespaces are important. 但是,名称空间很重要。

Not an answer, just some sample code - this returns a 1, not a NULL: 没有答案,只有一些示例代码-这将返回1,而不是NULL:

declare @xml xml,        @rate DECIMAL(10,4)
declare @ResponseText varchar(900)

set @ResponseText = '<?xml version="1.0" encoding="utf-8"?> <double>1</double>'

set @xml=REPLACE(@ResponseText ,'encoding="utf-8"','')
select @rate= @xml.value('(/double)[1]','decimal')
select @rate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM