1

I need to iterate through all the attributes (regardless of name) in the sample XML in this post and create a 2D "Array of Arrays" using JQuery.

I am using Microsoft's CAML query language to return attributes from a SharePoint list. It is returned as XML which is loaded into an object. Here is the XML being returned:

<?xml version="1.0"?>
<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><GetListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/"><GetListItemsResult><listitems xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
                <rs:data ItemCount="3">
                    <z:row ows_Attachments="0" ows_LinkTitle="Apple" ows_Color="Red" ows_Quantity="3.00000000000000" ows_Quantity2="5.00000000000000" ows_MetaInfo="1;#" ows__ModerationStatus="0" ows__Level="1" ows_Title="Apple" ows_ID="1" ows_UniqueId="1;#{FEDE3004-A0F4-421F-A76B-5BD51003B11C}" ows_owshiddenversion="2" ows_FSObjType="1;#0" ows_Created_x0020_Date="1;#2012-08-01 19:24:08" ows_Created="2012-08-01 19:24:08" ows_FileLeafRef="1;#1_.000" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-08-05 13:59:06" ows_FileRef="1;#Sandbox/bitest/Lists/Produce/1_.000"/>
                    <z:row ows_Attachments="0" ows_LinkTitle="Orange" ows_Color="Orange" ows_Quantity="5.00000000000000" ows_Quantity2="3.00000000000000" ows_MetaInfo="2;#" ows__ModerationStatus="0" ows__Level="1" ows_Title="Orange" ows_ID="2" ows_UniqueId="2;#{1E40887F-87BF-4029-93A3-A1096DF1D1A7}" ows_owshiddenversion="2" ows_FSObjType="2;#0" ows_Created_x0020_Date="2;#2012-08-01 19:24:42" ows_Created="2012-08-01 19:24:42" ows_FileLeafRef="2;#2_.000" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-08-05 13:59:08" ows_FileRef="2;#Sandbox/bitest/Lists/Produce/2_.000"/>
                    <z:row ows_Attachments="0" ows_LinkTitle="Corn" ows_Color="Yellow" ows_Quantity="9.00000000000000" ows_Quantity2="19.0000000000000" ows_MetaInfo="3;#" ows__ModerationStatus="0" ows__Level="1" ows_Title="Corn" ows_ID="3" ows_UniqueId="3;#{3873272A-61AA-49C0-B5D2-A228388ADE27}" ows_owshiddenversion="5" ows_FSObjType="3;#0" ows_Created_x0020_Date="3;#2012-08-01 19:24:57" ows_Created="2012-08-01 19:24:57" ows_FileLeafRef="3;#3_.000" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-08-08 13:58:38" ows_FileRef="3;#Sandbox/bitest/Lists/Produce/3_.000"/>
                </rs:data>
            </listitems></GetListItemsResult></GetListItemsResponse></soap:Body></soap:Envelope>

This xml appears to be "poorly formed" and so I am having trouble iterating through its attributes for use in a 2D array. There was another post here on StackOverflow that gave me some hope. It seems that because the XML is not well formed, it isn't working.

If there were some better way to work with this data that would allow me to load all of these attributes into an array of arrays (one array for each child node) I would love to hear about it. I'm pretty green to this and I am hoping to find something simple that will work with a variety of CAML queries in the future.

------------------------------------- Edit & New Info--------------------------------

Let me add some info to this question before posting a new one.

I've somewhat given up on creating a 2D array dynamically from all the listed attributes; myself and another have not been able to get it working. It appears that I will simply have to create a custom array each time.

My new, related question: Let's say I want to get all the "Quantity2" entries from this SOAP envelope.

What is the best way to iterate through each row entry using JavaScript / JQuery and return one value for each row? I can give the current code sample I am using; it works but I am afraid that it might get confused by data values in the XML:

$(xData.responseXML).find("z\\:row").each(function() {
    quantityOneArray[i] = parseFloat($(this).attr("ows_Quantity"));
    quantityTwoArray[i] = parseFloat($(this).attr("ows_Quantity2"));
    i++
});

I'd prefer to use DOM elements such as this example (which isn't valid):

for each $(this).childnode(function(){

Any thoughts would be appreciated!

Community
  • 1
  • 1
Shrout1
  • 2,497
  • 4
  • 42
  • 65
  • You are dealing with a soap envelope. The following SO question may be of help: http://stackoverflow.com/questions/8329322/is-it-possible-to-parse-a-soap-response-with-a-jquery-xml-handler – Kevin B Aug 14 '12 at 18:28
  • Thanks for the comment! While that question does address pulling attributes from a SOAP envelope, it doesn't address getting *all* of the attributes. I need to stuff every single returned value into an array. – Shrout1 Aug 14 '12 at 20:03

1 Answers1

0

Not really sure what you're after, but try something like:

//get the parent XML node
var elems = document.getElementsByTagName('soap:Envelope')​;

//turn the collection into an aray
var arrayElem = $(elems).toArray();

//create an array with each node
var arr = [];
$.each($('*', elems), function(i,e) {
    arr.push(e);
});

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • I can never seem to get JSFiddle to work... I've only been working with it for a week. I see your console.log commands but I don't see any results popping up anywhere in the fiddle. What am I doing wrong? And thank you for the comment! – Shrout1 Aug 14 '12 at 20:06
  • There are no results popping up, you need to open up the console (F12 in chrome) and see the objects there. – adeneo Aug 15 '12 at 02:37
  • Thanks! I'm still figuring out all the basics. – Shrout1 Aug 15 '12 at 14:36
  • All right - I saw the response in the console; it appears that the *whole* row is returning as an array entry. Bah. One would think that Microsoft would send back XML code that was easier to work with. I'm just not sure that it can be broken down! I think the only option is to iterate as many times as there are attributes that I am looking for. I'll have to run a find for that specific attribute each time and change the required attribute in the code for every SOAP call. – Shrout1 Aug 15 '12 at 14:45