Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何循环XML输出并添加到结构或数组?_Xml_Loops_Coldfusion_Struct - Fatal编程技术网

如何循环XML输出并添加到结构或数组?

如何循环XML输出并添加到结构或数组?,xml,loops,coldfusion,struct,Xml,Loops,Coldfusion,Struct,我正在尝试创建一份分会官员及其各自职位的列表。数据来自通过web服务访问的一系列XML键/值对(键:Member\u Name,value:Joe Member.key:Position\u Name,value:President,等等)。给定章节的每个官员都有自己的成员名和职位名 我使用的API将只返回整个对象,因此我设置了一个数组来转换XML名称并保存所有内容: <cfset keyValue = xmlSearch(soapBody,"//*[local-name()='KeyVa

我正在尝试创建一份分会官员及其各自职位的列表。数据来自通过web服务访问的一系列XML键/值对(键:Member\u Name,value:Joe Member.key:Position\u Name,value:President,等等)。给定章节的每个官员都有自己的成员名和职位名

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

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

我的想法是循环遍历该数组,对于关键成员名称和位置名称的每个实例,将值添加到结构中:

<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也没有任何效果——就像循环从末尾开始,而不是继续

我尝试过其他方法向结构中添加内容,它确实会遍历所有内容,但键/值对的顺序是不相关的。我知道结构无论如何都不是有序的,但我需要有某种方法来对XML输出中的数据进行排序。我还尝试了其他各种循环,尝试向数组中添加一系列像这样的小结构。它起作用了,但又一次,只对那一个人——似乎没有真正的“循环”发生!我可以看到我需要的所有信息,所有这些信息都在同一时间——所以也许是因为我把所有信息都整理好了,我做错了什么

提前谢谢大家,我非常感谢你们给我的任何建议和建议

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

更新:谢谢Peter,下面是我正在使用的XML的一个示例:

<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>

成员Guid
006e1c09-25f9-4178-86de-13c3e63200ce
成员类型
实体
成员姓名
成员,乔
位置\u Guid
02ae1c09-5779-4891-8cd1-05cf475cf5af
位置类型
委员会
职位名称
主席:
术语组Guid
044e1c09-a90b-495f-891f-afa13e653dee
术语类型
委员会成员
术语名称
2011-2012
对档案中的每一章官员重复

更新:这是我的代码。它做了我想要它做的,但有更好的方法去做,我相信

首先,我从SOAP响应中获得结果,“深入”到我需要的级别,然后去掉特定于xml的内容,将数据放入一个可用的数组中:

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

然后


我做一些其他的处理,检查变量的存在等等,然后用

<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>



#第三章官员[x][2]#


我能够将成员Guid添加到数组中并使用它,这样网站访问者就可以单击一个人的名字来查看更多详细信息(公司、电子邮件地址等)。就这样!你怎么认为?再次感谢你抽出时间,我真的很感激

以下是我可能会如何解决这个问题:

<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>
这保持了顺序,使更一般的查找更容易,如果您的主要用途是直接输出到HTML,也会使查找更容易

我已经在那里硬编码了列键,但是您也可以先做一个预循环来整理这些列键,如果它们很容易更改的话



希望这一切都有意义?

以下是我可能会如何解决这个问题:

<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>
这保持了顺序,使更一般的查找更容易,如果您的主要用途是直接输出到HTML,也会使查找更容易

我已经在那里硬编码了列键,但是您也可以先做一个预循环来整理这些列键,如果它们很容易更改的话



希望这一切都有意义?

在Coldfusion 10或Railo 4中,您可以使用来帮助清理解决方案:

<cfscript>
    soapBody = XmlParse(cfhttp.filecontent);
    fields = xmlSearch(soapBody,"//*[local-name()='Fields']");
    chapterOfficers = _.map(fields, function (field) {
        var officer = {};
        _.each(field.xmlChildren, function (KeyValueOfstringanyType) {
            var key = KeyValueOfstringanyType['b:Key'].xmlText;
            var value = KeyValueOfstringanyType['b:Value'].xmlText;
            officer[key] = value;
        });
        return officer;
    });
</cfscript>

<cfoutput>
<cfloop array="#chapterOfficers#" index="officer">
    <a href="OfficerDetail.cfm?sessionGuid=#URL.sessionGuid#&memberGuid=#officer.Member_Guid#">#officer.Member_Name#</a> 
    #officer.Position_Name#<br />
</cfloop>
</cfoutput>

soapBody=XmlParse(cfhttp.filecontent);
fields=xmlSearch(soapBody,“/*[local-name()='fields']”);
chapterOfficers=uu.map(字段,函数(字段){
var-officer={};
_.each(field.xmlChildren,函数(KeyValueOfstringanyType){
var key=KeyValueOfstringanyType['b:key'].xmlText;
var value=KeyValueOfstringanyType['b:value'].xmlText;
军官[钥匙]=价值;
});
返回主任;
});
#军官。职位名称
现在不是更好了吗?我不确定您的SOAP响应是什么样子的,但是您应该能够调整xmlSearch()以匹配KeyValueOfstringanyType的父元素。我还为您删除了所有不必要的CFOutput。另外,我建议切换到JSON而不是XML。解析起来容易多了


(免责声明:我编写了underline.cfc库)

在Coldfusion 10或Railo 4中,您可以使用来帮助清理解决方案:

<cfscript>
    soapBody = XmlParse(cfhttp.filecontent);
    fields = xmlSearch(soapBody,"//*[local-name()='Fields']");
    chapterOfficers = _.map(fields, function (field) {
        var officer = {};
        _.each(field.xmlChildren, function (KeyValueOfstringanyType) {
            var key = KeyValueOfstringanyType['b:Key'].xmlText;
            var value = KeyValueOfstringanyType['b:Value'].xmlText;
            officer[key] = value;
        });
        return officer;
    });
</cfscript>

<cfoutput>
<cfloop array="#chapterOfficers#" index="officer">
    <a href="OfficerDetail.cfm?sessionGuid=#URL.sessionGuid#&memberGuid=#officer.Member_Guid#">#officer.Member_Name#</a> 
    #officer.Position_Name#<br />
</cfloop>
</cfoutput>

soapBody=XmlParse(cfhttp.filecontent);
fields=xmlSearch(soapBody,“/*[local-name())=