ColdFusion - 如何循环通过 XML 输出并添加到结构或数组? [英] ColdFusion - how to loop through XML output and adding to struct or array?

查看:20
本文介绍了ColdFusion - 如何循环通过 XML 输出并添加到结构或数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建分会军官及其各自职位的列表.数据来自通过 Web 服务访问的一系列 XML 键/值对(键:Member_Name,值:Joe Member.键:Position_Name,值:总裁等.)给定章节的每个官员都有自己的 Member_Name 和Position_Name.

I am trying to create a list of Chapter Officers and their respective positions. The data comes from a series of XML key/value pairs accessed via web service (Key: Member_Name, Value: Joe Member. Key: Position_Name, Value: President, and so forth.) Each officer for a given chapter has their own Member_Name and Position_Name.

我正在使用的 API 只会返回整个对象,因此我设置了一个数组来转换 XML 名称并保存所有内容:

The API I am working with will only return an entire object, so I set up an array to convert the XML name and hold everything:

<cfset keyValue = xmlSearch(soapBody,"//*[local-name()='KeyValueOfstringanyType']") />

我的想法是遍历该数组,并为键 Member_Name 和 Position_Name 的每个实例,将值添加到结构中:

My thought was to loop through that array, and for every instance of the key Member_Name and Position_Name, add the values to a struct:

<cfset chapterOfficers=structNew()>
<cfloop index="i" from="1" to="#arrayLen(keyValue)#">
    <cfif keyValue[i].Key.xmlText EQ 'Member_Name'>
        <cfset chapterOfficers.Name=keyValue[i].Value.xmlText>
    </cfif>
    <cfif keyValue[i].Key.xmlText EQ 'Position_Name'>
        <cfset chapterOfficers.Position = keyValue[i].Value.xmlText>
    </cfif>
    <cfif keyValue[i].Key.xmlText EQ 'Term_Name'>
        <cfset chapterOfficers.Term = keyValue[i].Value.xmlText>
    </cfif>
</cfloop>

转储这个结构给我一个漂亮整洁的小表格,上面有一个人的名字、那个人的职位和他们的任期——但只有那个(恰好是 XML 文件中的最后一个条目).即使添加 i=i+1 也没有任何效果 - 几乎就像循环从末尾开始,而不是继续.

Dumping this structure gives me a nice neat little table with one person's name, that person's position, and their term -- but only that one (which happens to be the last entry in the XML file). Even adding i=i+1 does not have any effect -- it's almost like the loop is starting at the end, and not continuing.

我尝试了其他向结构添加内容的方法,确实循环遍历所有内容,但键/值对以不相关的顺序出现.我知道结构无论如何都没有排序,但我需要有一些方法来对 XML 输出中的数据进行排序.我还尝试了各种其他循环,尝试将一系列像这样的小结构添加到数组中.它起作用了,但同样,只针对那个人——似乎没有发生实际的循环"!我可以同时看到我需要的所有信息——所以也许是为了让我做错了什么?

I've tried other ways of adding things to the structure, which does loop through everything, but the key/value pairs come out in an unrelated order. I know that structs are not ordered anyways, but I need to have some way to sequence the data from the XML output. I also tried various other loops, trying to add a series of little structs like this one to an array. It worked, but again, only for that one person -- no actual "looping" seemed to take place! I can see all of the information I need, all at the same time -- so maybe it's in putting it all in order that I am doing something wrong?

在此先感谢大家,感谢您向正确方向提出任何建议或推动!

Thank you all in advance, I appreciate any suggestions or nudges in the right direction!

更新:不知道这是否有帮助,但刚才我在我正在使用的循环中交换了 To 和 From 的值,并将 step 设置为 -1,它给了我列表中的第一个人.但还是没有循环.

UPDATE: don't know if this helps, but just now I swapped the values of To and From in the loop I am using, and set the step to -1, and it gave me the FIRST person in the list. But still did not loop.

更新:谢谢彼得,这是我正在使用的 XML 的一个示例:

UPDATE: Thanks Peter, here is an example of the XML I am working with:

<b:KeyValueOfstringanyType>
                    <b:Key>Member_Guid</b:Key>
                    <b:Value i:type="d:string">006e1c09-25f9-4178-86de-13c3e63200ce</b:Value>
                 </b:KeyValueOfstringanyType>
                 <b:KeyValueOfstringanyType>
                    <b:Key>Member_Type</b:Key>
                    <b:Value i:type="d:string">Entity</b:Value>
                 </b:KeyValueOfstringanyType>
                 <b:KeyValueOfstringanyType>
                    <b:Key>Member_Name</b:Key>
                    <b:Value i:type="d:string">Member, Joe</b:Value>
                 </b:KeyValueOfstringanyType>
                 <b:KeyValueOfstringanyType>
                    <b:Key>Position_Guid</b:Key>
                    <b:Value i:type="d:string">02ae1c09-5779-4891-8cd1-05cf475cf5af</b:Value>
                 </b:KeyValueOfstringanyType>
                 <b:KeyValueOfstringanyType>
                    <b:Key>Position_Type</b:Key>
                    <b:Value i:type="d:string">CommitteePosition</b:Value>
                 </b:KeyValueOfstringanyType>
                 <b:KeyValueOfstringanyType>
                    <b:Key>Position_Name</b:Key>
                    <b:Value i:type="d:string">President</b:Value>
                 </b:KeyValueOfstringanyType>
                 <b:KeyValueOfstringanyType>
                    <b:Key>Term_Guid</b:Key>
                    <b:Value i:type="d:string">044e1c09-a90b-495f-891f-afa13e653dee</b:Value>
                 </b:KeyValueOfstringanyType>
                 <b:KeyValueOfstringanyType>
                    <b:Key>Term_Type</b:Key>
                    <b:Value i:type="d:string">CommitteeTerm</b:Value>
                 </b:KeyValueOfstringanyType>
                 <b:KeyValueOfstringanyType>
                    <b:Key>Term_Name</b:Key>
                    <b:Value i:type="d:string">2011-2012</b:Value>
                 </b:KeyValueOfstringanyType>

对存档的每个分会官员重复.

Repeats for every chapter officer on file.

更新:这是我想出的代码.它做我想做的事,但有更好的方法可以做到,我敢肯定......

UPDATE: here is the code I came up. It does what I want it to do, but there are much better ways to do it, I am sure...

首先我从 SOAP 响应中获取结果,向下钻取"到我需要的级别,然后去除特定于 xml 的内容并将数据放入一个可用的数组中:

First I get the results from the SOAP response, "drill down" to the level I need, and then strip out the xml-specific stuff and get the data into a usable array:

<cfset soapBody = xmlParse(cfhttp.fileContent)>
<cfset soapBody = soapBody['s:Envelope']['s:Body'].QueryResponse.QueryResult.Objects.anyType.Fields />
<cfset keyValue = xmlSearch(soapBody,"//*[local-name()='KeyValueOfstringanyType']") />

然后

<cfset chapterOfficers=arrayNew(2)>
<cfset x=1>
<cfset y=1>

<cfloop index="i" from="1" to="#arrayLen(keyValue)#">
    <cfif keyValue[i].Key.xmlText EQ 'Member_Name'>
        <cfset memberName = keyValue[i].Value.xmlText>
        <cfset chapterOfficers[x][y]=#memberName#>
        <cfset y=y+1>
    </cfif>
    <cfif keyValue[i].Key.xmlText EQ 'Position_Name'>
        <cfset positionName = keyValue[i].Value.xmlText>
        <cfset chapterOfficers[x][y]=#positionName#>
        <cfset x=x+1>
        <cfset y=1>
    </cfif>
    <cfif keyValue[i].Key.xmlText EQ 'Member_Guid'>
        <cfset memberGuid = keyValue[i].Value.xmlText>
        <cfset chapterOfficers[x][3]=#memberGuid#>
    </cfif>
</cfloop>

我做一些其他的处理,检查变量的存在等等,然后输出军官的名字和他们各自的职位

I do some other processing, checking for existence of variables, etc, and then output the names of the Officers and their respective positions with

<cfloop from="1" to="#arrayLen(chapterOfficers)#" index="x">
    <p>
        <cfoutput><a href="OfficerDetail.cfm?sessionGuid=<cfoutput>#URL.sessionGuid#</cfoutput>&memberGuid=<cfoutput>#chapterOfficers[x][3]#</cfoutput>">#chapterOfficers[x][1]#</a></cfoutput><br />
        <cfoutput>#chapterOfficers[x][2]#</cfoutput><br />
    </p>
</cfloop>

我能够将 Member_Guid 添加到数组中并使用它,以便网站访问者可以单击一个人的姓名以查看更多详细信息(公司、电子邮件地址等).就是这样!你怎么认为?再次感谢您抽出宝贵时间,非常感谢!

I was able to add Member_Guid to the array and use it so site visitors can click on a person's name to see further detail (company, email address, etc). And that is about it! What do you think? Again, thanks a lot for taking the time, I really appreciate it!

推荐答案

我可能会这样解决:

<cfset var ChapterOfficers = StructNew()>
<cfset var CurMemberGuid = '' />

<cfloop index="local.CurPair" array=#keyValue#>

    <cfif CurPair.Key.XmlText EQ 'Member_Guid' >
        <cfset CurMemberGuid = CurPair.Value.XmlText />
        <cfset ChapterOfficers[CurMemberGuid] = StructNew() />
    <cfelse>
        <cfset ChapterOfficers[CurMemberGuid][CurPair.Key.XmlText] = CurPair.Value.XmlText />
    </cfif>

</cfloop>

它使用您已完成的现有 XmlSearch,并假定 Member_Guid 始终是第一个键/值对.我已经使用了 var/local 范围,假设这是在一个函数内部(它可能应该是),但如果不只是删除它们.

It uses the existing XmlSearch you've done, and assumes Member_Guid is always the first key/value pair. I've used var/local scope assuming this is going inside a function (which it probably should be), but if not just remove them.

它使用结构体,因此查找特定 GUID 很容易,但不会保留顺序(但如果需要,您可以保留一个单独的数组来执行此操作),并且您不必记住哪个数组位置与哪个匹配键.

It uses structs so lookup of a particular GUID is easy, but order isn't preserved (though if necessary you could keep a separate array to do that), and you don't have to remember which array position matches to which key.

如果您想根据其他字段进行查找,您还可以将数据转换为查询,如下所示:

If you wanted to lookup based on other fields, you could also convert the data into a query, like so:

<cfset var ChapterOfficers = QueryNew('Member_Guid,Member_Type,Member_Name,Position_Guid,Position_Type,Position_Name,Term_Guid,Term_Type,Term_Name')>
<cfset var CurRow = 1 />

<cfloop index="local.CurPair" array=#keyValue#>

    <cfif CurPair.Key.XmlText EQ 'Member_Guid' >
        <cfset QueryAddRow(ChapterOfficers) />
    </cfif>

    <cfset QuerySetCell(ChapterOfficers,CurPair.Key.XmlText,CurPair.Value.XmlText) />

</cfloop>

这样可以保持顺序并使更一般的查找更容易,如果您的主要用途是直接输出到 HTML,也会更容易.

This maintains order and makes more general lookup easier, also makes it easier if your main use is outputting direct to HTML.

我已经在那里对列键进行了硬编码,但如果它们可能会更改,您也可以先进行预循环来整理它们.

I've hard-coded the column keys there, but you could also do a pre-loop to collate those first, if they are something that is liable to change.


希望这一切都有意义吗?


Hopefully this all makes sense?

这篇关于ColdFusion - 如何循环通过 XML 输出并添加到结构或数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆