jaz*_*nie 5 xml 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>
Run Code Online (Sandbox Code Playgroud)
解决方案1:
我最终通过使用 Lookup 活动来获取连接到 HTTP 链接服务的 XML 数据集来完成此任务。返回的 XML 是从活动中作为 json 对象输出的,通常可以使用 Activity('GetSessionID').output.etc 访问该对象。但是,某些元素名称包含冒号(soap:Envelope 和soap:Body),当我将这些元素作为动态内容放入时,Azure 数据工厂给出了“BadRequest”错误。为了解决这个问题,我将其转换为 XML、字符串、去掉冒号、转换回 xml,然后转换为 json。从那里我可以像平常一样进入该物业。这是给我会话 ID 的动态内容:
@json(xml(replace(string(xml(activity('GetSessionID').output.firstRow)), ':', ''))).Envelope.Body.DoLoginResponse.DoLoginResult.SecurityProfile.Session.SessionId
解决方案2:
我认为将xml文件的一部分提取到字符串变量中是可以的。我的想法是,将xml文件转为字符串,根据表达式动态提取SessionId部分。
我在这里创建了一个简单的测试:
我正在使用 Lookup 活动来获取 xml 文件,您应该替换为您的 Web 活动。我声明了 2 个字符串变量XMLString并且SessionId:

在Set variable1Activity 中,添加动态内容@string(activity('Lookup1').output.value[0])来为变量赋值XMLString。如果您使用 Web 活动,则内容应该是@string(activity('<Web_Actvity_Name>').output).

在Set variable2Activity 中,添加动态内容@substring(variables('XMLString'),add(indexof(variables('XMLString'),'SessionId'),12),sub(indexof(variables('XMLString'),'}'),add(lastindexof(variables('XMLString'),'SessionId'),13)))来为变量赋值SessionId。
的值SessionId如下:
