简体   繁体   English

如何使用 Azure 数据工厂将 XML 元素读入变量

[英]How to read an XML element into a variable with Azure Data Factory

I am trying to read in data from an API with Azure Data Factory.我正在尝试使用 Azure 数据工厂从 API 读取数据。 First I need to call a log in method, which provides an XML response.首先我需要调用一个登录方法,它提供了一个 XML 响应。 I need to take an element from that XML and put it into my next API call to get the data that I need.我需要从 XML 中取出一个元素并将其放入我的下一个 API 调用中以获取我需要的数据。

Currently I am using the Copy data tool to call the log in method and save the XML to blob storage.目前我正在使用复制数据工具调用登录方法并将 XML 保存到 blob 存储。 How do I now read an element of that XML into a variable?我现在如何将 XML 的元素读入变量?

If there is a better way of doing this then please advise, but I would still like to know how to read an XML element into a variable.如果有更好的方法,请提出建议,但我仍然想知道如何将 XML 元素读入变量。

Edit: here is the XML being returned.编辑:这是返回的 XML。 I need to capture the SessionID.我需要捕获 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>

Solution1:解决方案1:

I ended up getting it done by using the Lookup activity sourcing an XML data set connected an HTTP linked service.我最终通过使用查找活动来完成它,该活动采购了连接 HTTP 链接服务的 XML 数据集。 The returned XML is output from the activity as a json object which is normally accessable with activity('GetSessionID').output.etc. The returned XML is output from the activity as a json object which is normally accessable with activity('GetSessionID').output.etc. However, some of the element names contain a colon (soap:Envelope and soap:Body) and Azure Data Factory gave me a "BadRequest" error when I put these in as dynamic content.但是,某些元素名称包含冒号(soap:Envelope 和 soap:Body)和 Azure 当我将它们作为动态内容放入时,数据工厂给了我一个“BadRequest”错误。 To get around this I converted it to XML, to string, stripped out the colons, converted back to xml, then to json.为了解决这个问题,我将它转换为 XML,字符串,去掉冒号,转换回 xml,然后转换为 json。 From there I could access the property like normal.从那里我可以像往常一样访问该物业。 This is the dynamic content that gave me the session id:这是给我 session id 的动态内容:

@json(xml(replace(string(xml(activity('GetSessionID').output.firstRow)), ':', ''))).Envelope.Body.DoLoginResponse.DoLoginResult.SecurityProfile.Session.SessionId


Solution2:解决方案2:

I think it is ok to extract a part of the xml file into a string variable.我认为可以将 xml 文件的一部分提取到字符串变量中。 My idea is to convert the xml file into a string, and dynamically extract the SessionId part according to the expression.我的想法是将xml文件转换成字符串,根据表达式动态提取SessionId部分。

I created a simple test here:我在这里创建了一个简单的测试:

  1. I'm using Lookup activity to get the xml file, you should replace with your web activity.我正在使用查找活动来获取 xml 文件,您应该替换为 web 活动。 I declared 2 String variables XMLString and SessionId :我声明了 2 个字符串变量XMLStringSessionId 在此处输入图像描述

  2. In Set variable1 activity, add dynamic content @string(activity('Lookup1').output.value[0]) to assign value to variable XMLString .Set variable1活动中,添加动态内容@string(activity('Lookup1').output.value[0])为变量XMLString赋值。 If you are using Web activity, the content should be @string(activity('<Web_Actvity_Name>').output) .如果您使用的是 Web 活动,则内容应为@string(activity('<Web_Actvity_Name>').output) 在此处输入图像描述

  3. In Set variable2 activity, add dynamic content @substring(variables('XMLString'),add(indexof(variables('XMLString'),'SessionId'),12),sub(indexof(variables('XMLString'),'}'),add(lastindexof(variables('XMLString'),'SessionId'),13))) to assign value to variable SessionId .Set variable2活动中,添加动态内容@substring(variables('XMLString'),add(indexof(variables('XMLString'),'SessionId'),12),sub(indexof(variables('XMLString'),'}'),add(lastindexof(variables('XMLString'),'SessionId'),13)))为变量SessionId赋值。 在此处输入图像描述 The value of SessionId is as follows: SessionId的值如下:
    在此处输入图像描述

I ended up getting it done by using the Lookup activity sourcing an XML data set connected an HTTP linked service.我最终通过使用查找活动来完成它,该活动采购了连接 HTTP 链接服务的 XML 数据集。 The returned XML is output from the activity as a json object which is normally accessable with activity('GetSessionID').output.etc . The returned XML is output from the activity as a json object which is normally accessable with activity('GetSessionID').output.etc . However, some of the element names contain a colon (soap:Envelope and soap:Body) and Azure Data Factory gave me a "BadRequest" error when I put these in as dynamic content.但是,某些元素名称包含冒号(soap:Envelope 和 soap:Body)和 Azure 当我将它们作为动态内容放入时,数据工厂给了我一个“BadRequest”错误。 To get around this I converted it to XML, to string, stripped out the colons, converted back to xml, then to json.为了解决这个问题,我将它转换为 XML,字符串,去掉冒号,转换回 xml,然后转换为 json。 From there I could access the property like normal.从那里我可以像往常一样访问该物业。 This is the dynamic content that gave me the session id: @json(xml(replace(string(xml(activity('GetSessionID').output.firstRow)), ':', ''))).Envelope.Body.DoLoginResponse.DoLoginResult.SecurityProfile.Session.SessionId这是给我 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.

相关问题 Azure 数据工厂中的 XML 验证 - XML validation in Azure Data Factory XML 文件使用 Azure 数据工厂 - XML File Using Azure Data Factory 在Azure数据工厂中针对Azure SQL Server进行XML文件验证 - XML File Validation Against Azure SQL Server in Azure Data Factory 如何在 Azure 数据工厂中使用复制数据活动将 xml 解析为 json 时删除转义字符? - How to remove escaped character when parsing xml to json with copy data activity in Azure Data Factory? 我现在如何将 XML 的元素读入变量 - How do I now read an element of that XML into a variable Azure 数据工厂将数据从 XML 复制到 SQL 突触池 - Azure Data Factory copy data from XML to SQL Synapse pool 使用 azure 数据工厂将多个 xml 文件合并到 csv 文件 - Merging multiple xml files to a csv file using azure Data Factory 如何从 c# 中的 xml 数据中读取多个节点元素 - How to read multiple node element from xml data in c# Azure Data Factory是否可以提取存储在Azure Data Lake Store中的XML文件? - Can Azure Data Factory ingest an XML file stored in Azure Data Lake Store? 通过 Azure 数据工厂 V2 将 XML 数据加载到 SQL 表(Azure)中 - Load XML Data Into SQL Table (Azure) via Azure Data Factory V2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM