[英]How to read an XML element into a variable with Azure Data Factory
我正在尝试使用 Azure 数据工厂从 API 读取数据。 首先我需要调用一个登录方法,它提供了一个 XML 响应。 我需要从 XML 中取出一个元素并将其放入我的下一个 API 调用中以获取我需要的数据。
目前我正在使用复制数据工具调用登录方法并将 XML 保存到 blob 存储。 我现在如何将 XML 的元素读入变量?
如果有更好的方法,请提出建议,但我仍然想知道如何将 XML 元素读入变量。
编辑:这是返回的 XML。 我需要捕获 SessionID。
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<DoLoginResponse xmlns="http://foo.bar">
<DoLoginResult>
<OperationStatus>true</OperationStatus>
<StatusMessage />
<SecurityProfile>
<User></User>
<Session>
<SessionId>d2cf6ea6-120f-4bff-a5d1-adad9063d9d2</SessionId>
</Session>
<IsFirstLogon>true</IsFirstLogon>
<IsSystemOwner>false</IsSystemOwner>
</SecurityProfile>
</DoLoginResult>
</DoLoginResponse>
</soap:Body>
</soap:Envelope>
解决方案1:
我最终通过使用查找活动来完成它,该活动采购了连接 HTTP 链接服务的 XML 数据集。 The returned XML is output from the activity as a json object which is normally accessable with activity('GetSessionID').output.etc. 但是,某些元素名称包含冒号(soap:Envelope 和 soap:Body)和 Azure 当我将它们作为动态内容放入时,数据工厂给了我一个“BadRequest”错误。 为了解决这个问题,我将它转换为 XML,字符串,去掉冒号,转换回 xml,然后转换为 json。 从那里我可以像往常一样访问该物业。 这是给我 session id 的动态内容:
@json(xml(replace(string(xml(activity('GetSessionID').output.firstRow)), ':', ''))).Envelope.Body.DoLoginResponse.DoLoginResult.SecurityProfile.Session.SessionId
解决方案2:
我认为可以将 xml 文件的一部分提取到字符串变量中。 我的想法是将xml文件转换成字符串,根据表达式动态提取SessionId部分。
我在这里创建了一个简单的测试:
我正在使用查找活动来获取 xml 文件,您应该替换为 web 活动。 我声明了 2 个字符串变量XMLString
和SessionId
:
在Set variable1
活动中,添加动态内容@string(activity('Lookup1').output.value[0])
为变量XMLString
赋值。 如果您使用的是 Web 活动,则内容应为@string(activity('<Web_Actvity_Name>').output)
。
在Set variable2
活动中,添加动态内容@substring(variables('XMLString'),add(indexof(variables('XMLString'),'SessionId'),12),sub(indexof(variables('XMLString'),'}'),add(lastindexof(variables('XMLString'),'SessionId'),13)))
为变量SessionId
赋值。 SessionId
的值如下:
我最终通过使用查找活动来完成它,该活动采购了连接 HTTP 链接服务的 XML 数据集。 The returned XML is output from the activity as a json object which is normally accessable with activity('GetSessionID').output.etc
. 但是,某些元素名称包含冒号(soap:Envelope 和 soap:Body)和 Azure 当我将它们作为动态内容放入时,数据工厂给了我一个“BadRequest”错误。 为了解决这个问题,我将它转换为 XML,字符串,去掉冒号,转换回 xml,然后转换为 json。 从那里我可以像往常一样访问该物业。 这是给我 session id 的动态内容: @json(xml(replace(string(xml(activity('GetSessionID').output.firstRow)), ':', ''))).Envelope.Body.DoLoginResponse.DoLoginResult.SecurityProfile.Session.SessionId
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.