ColdFusion - how to loop through XML output and adding to struct or array?

后端 未结 2 1274
梦如初夏
梦如初夏 2021-01-25 14:03

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,

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-25 14:18

    In Coldfusion 10 or Railo 4, you could use the Underscore.cfc library to help clean your solution up a lot:

    
        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;
        });
    
    
    
    
        #officer.Member_Name# 
        #officer.Position_Name#

    Now isn't that nicer? I'm not exactly sure what your SOAP response looks like, but you should be able to tweak the xmlSearch() to match on the parent element of KeyValueOfstringanyType. I also removed all the unnecessary cfoutputs for you. Also, I'd recommend switching to JSON instead of XML. It's a LOT easier to parse.

    (Disclaimer: I wrote the Underscore.cfc library)

提交回复
热议问题