SSIS Web Service 组件使用
默认分类
2015-07-27
480
0
一、Web Service Task
拖一个Web Service Task 并双击打开,创建一个新的HTTP Connection。 本文使用 http://www.webservicex.net/geoipservice.asmx 的Web Service做示例,注意后面带了参数WSDL ,直接访问就返回了这个web service的定义文件。当然这里可以不加这个参数,下面的步骤要手动下载这个文件。
设置WSDL文件的目录,然后点击 Download WSDL 下载Web Service 定义文件 如果上面一步没有加上 ?WSDL 那就需要手动下载然后指定文件。
选择服务和方法,并且输入查询值,这里我们输入了阿里云的DNS,其中勾选Variable则可以指定变量。
在Output 标签里面设定输出文件名
保存执行以后可以获得以下结果:
<?xml version="1.0" encoding="utf-16"?> <GeoIP xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <ReturnCode xmlns="http://www.webservicex.net/">1</ReturnCode> <IP xmlns="http://www.webservicex.net/">223.5.5.5</IP> <ReturnCodeDetails xmlns="http://www.webservicex.net/">Success</ReturnCodeDetails> <CountryName xmlns="http://www.webservicex.net/">China</CountryName> <CountryCode xmlns="http://www.webservicex.net/">CHN</CountryCode> </GeoIP>
二、转换XML并且入库
首先拖动一个Data Flow Task并打开
然后拖入一个XML Source 选择我们生成的xml ,因为没有XSD因此我们要手动生成一个。点击Generate XSD保存以后报错。这是因为我们xml文件里面有多个namespaces导致的。
如图我们的xml文件里面有两个 namespace
把这些统统删掉 结果如下
<GeoIP> <ReturnCode>1</ReturnCode> <IP>223.5.5.5</IP> <ReturnCodeDetails>Success</ReturnCodeDetails> <CountryName>China</CountryName> <CountryCode>CHN</CountryCode> </GeoIP>

此时我们重新生成就可以了
但是我们不能每次都手动操作,所以我们需要添加一个XML Task 来自动帮我们去掉多重的namespace。
设置如下
其中的remove.xsl 文件代码如下:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">接着重新设置下我们的 XML Source<xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes"/> <!-- Stylesheet to remove all namespaces from a document --> <!-- NOTE: this will lead to attribute name clash, if an element contains two attributes with same local name but different namespace prefix --> <!-- Nodes that cannot have a namespace are copied as such --> <!-- template to copy elements --> <xsl:template match="*"> <xsl:element name="{local-name()}"> <xsl:apply-templates select="@* | node()"/> </xsl:element> </xsl:template> <!-- template to copy attributes --> <xsl:template match="@*"> <xsl:attribute name="{local-name()}"> <xsl:value-of select="."/> </xsl:attribute> </xsl:template> <!-- template to copy the rest of the nodes --> <xsl:template match="comment() | text() | processing-instruction()"> <xsl:copy/> </xsl:template>
</xsl:stylesheet>
但是。。。点击Colums 还是没有字段显示。。看了官方样例以后发现要加个跟节点。 就是myTable 这个
因为不熟悉xsl的关系。我又加了一个xml task ,了解的小伙伴请告诉我怎么把两个xsl并起来
代码如下
<?xml version="1.0" encoding="utf-8"?> <myTable> <GeoIP> <ReturnCode>1</ReturnCode> <IP>223.5.5.5</IP> <ReturnCodeDetails>Success</ReturnCodeDetails> <CountryName>China</CountryName> <CountryCode>CHN</CountryCode> </GeoIP> </myTable>接着就简单了。连接数据库或者文本文件即可
参考文档:
Sample XML File: Customers and Orders (LINQ to XML) Importing XML documents using SQL Server Integration Services How to add top level element to XML using XSLT? Web Services Task in SSIS 2008