locked
Use JQuery to iterate through CAML query response xml RRS feed

  • Question

  • I am attempting to iterate through the xdata.responseXML that is returned by a CAML query sent to web services in a SOAP envelope. Basic question is, how do I use JQuery to iterate through the attributes in this response XML?

    1. Query
    2. Returned XML
    3. Code example (where I believe the problem is)

    1. Query:

            var soapEnv = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
                    <soapenv:Body> \
                         <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                            <listName>Produce</listName> \
                                <ViewFields> \
                                   <FieldRef Name='Quantity' /> \
                                   <FieldRef Name='Quantity2' /> \
                               </ViewFields> \
                        </GetListItems> \
                    </soapenv:Body> \
                </soapenv:Envelope>";

    2. Returned XML snippet:

    <z:row ows_ContentTypeId="0x010089EC9D497BD5FF4A83359272E25DA060" ows_Title="Apple" ows_LinkTitleNoMenu="Apple" ows_LinkTitle="Apple" ows_LinkTitle2="Apple" ows_Color="Red" ows_Quantity="3.00000000000000" ows_Quantity2="5.00000000000000" ows_ID="1" ows_ContentType="Item" ows_Modified="2012-08-05 13:59:06" ows_Created="2012-08-01 19:24:08" ows_Author="901;#Public, John Q" ows_Editor="901;#Public, John Q" ows_owshiddenversion="2" ows_WorkflowVersion="1" ows__UIVersion="512" ows__UIVersionString="1.0" ows_Attachments="0" ows__ModerationStatus="0" ows_SelectTitle="1" ows_Order="100.000000000000" ows_GUID="{2C6129A4-11C7-4D2B-B113-BB957B59D76A}" ows_FileRef="1;#Sandbox/bitest/Lists/Produce/1_.000" ows_FileDirRef="1;#Sandbox/bitest/Lists/Produce" ows_Last_x0020_Modified="1;#2012-08-01 19:24:08" ows_Created_x0020_Date="1;#2012-08-01 19:24:08" ows_FSObjType="1;#0" ows_SortBehavior="1;#0" ows_PermMask="0x7fffffffffffffff" ows_FileLeafRef="1;#1_.000" ows_UniqueId="1;#{FEDE3004-A0F4-421F-A76B-5BD51003B11C}" ows_SyncClientId="1;#" ows_ProgId="1;#" ows_ScopeId="1;#{ACA664A3-744D-4671-9610-773BB98C2733}" ows__EditMenuTableStart="1_.000" ows__EditMenuTableStart2="1" ows__EditMenuTableEnd="1" ows_LinkFilenameNoMenu="1_.000" ows_LinkFilename="1_.000" ows_LinkFilename2="1_.000" ows_ServerUrl="/Sandbox/bitest/Lists/Produce/1_.000" ows_EncodedAbsUrl="http://mydomain.com/Sandbox/bitest/Lists/Produce/1_.000" ows_BaseName="1_" ows_MetaInfo="1;#" ows__Level="1" ows__IsCurrentVersion="1" ows_ItemChildCount="1;#0" ows_FolderChildCount="1;#0" /> 

    3. Code I am trying to use (adapted from this StackOverflow post):

    function processResultXML(xData, status) {
    	arraybuilder = "";
    	arraynamebuilder = "";
    	ItemArray = [];
    	ItemNameArray = [];
    	$(xData.responseXML).find("z\\:row").each(j, function() {
    		arraybuilder = "";
            arraynamebuilder = "";
            $.each(this.attributes, function(i, attrib){
    			arraybuilder+=attrib.name;
    			arraynamebuilder+=attrib.value;
    			}
    		ItemNameArray[j] = arraynamebuilder;
    		ItemArray[j] = arraybuilder;
    	}
    }

    I don't seem to be returning anything...

    • Do I have a glaring error in my code?
    • Am I accessing the xData.responseXML object incorrectly?
    • Am I iterating through the XML attributes inappropriately?
    Monday, August 13, 2012 3:17 PM

Answers

  • The answer is quite simple. The XML string that is being returned by SharePoint is not parsed as an XML document in Internet Explorer by default. Google Chrome does it, but IE does not. Here is a link to the Stack Overflow thread that resolved my problem. Below is the JQuery code that parses the XML string as an XML document and allows the data to be iterated.

    var xmlDoc = $.parseXML(xData.xml); 
    $(xmlDoc).find("z\\:row").each(...

    Also note that "responseXML" was not valid in this circumstance. For whatever reason this SOAP response did not have the ".responseXML" property but only the ".xml" property. Using the .xml property was also part of the solution.

    Thank you for trying to solve the problem however.


    • Marked as answer by Shrout1 Thursday, August 23, 2012 12:38 PM
    • Edited by Shrout1 Thursday, August 23, 2012 12:42 PM Edited for clarity
    Thursday, August 23, 2012 12:37 PM

All replies

  • Hi Shrout1,

    First, you provided xml is not a standard xml, so jquery can't parsing it correctly. The xml should be format as the following:
    <?xml version="1.0" encoding="utf-8" ?>
    <z:row xmlns:z="http://www.w3.org/TR/html4/"
        ows_ContentTypeId="0x010089EC9D497BD5FF4A83359272E25DA060"
        ows_Title="Apple"
        ows_LinkTitleNoMenu="Apple"
        ows_LinkTitle="Apple"
        ows_LinkTitle2="Apple"
        ows_Color="Red"
        ows_Quantity="3.00000000000000"
        ows_Quantity2="5.00000000000000"
        ows_ID="1"
        ows_ContentType="Item"
        ows_Modified="2012-08-05 13:59:06"
        ows_Created="2012-08-01 19:24:08"
        ows_Author="901;#Public, John Q"
        ows_Editor="901;#Public, John Q"
        ows_owshiddenversion="2"
        ows_WorkflowVersion="1"
        ows__UIVersion="512"
        ows__UIVersionString="1.0"
        ows_Attachments="0"
        ows__ModerationStatus="0"
        ows_SelectTitle="1"
        ows_Order="100.000000000000"
        ows_GUID="{2C6129A4-11C7-4D2B-B113-BB957B59D76A}"
        ows_FileRef="1;#Sandbox/bitest/Lists/Produce/1_.000"
        ows_FileDirRef="1;#Sandbox/bitest/Lists/Produce"
        ows_Last_x0020_Modified="1;#2012-08-01 19:24:08"
        ows_Created_x0020_Date="1;#2012-08-01 19:24:08"
        ows_FSObjType="1;#0"
        ows_SortBehavior="1;#0"
        ows_PermMask="0x7fffffffffffffff"
        ows_FileLeafRef="1;#1_.000"
        ows_UniqueId="1;#{FEDE3004-A0F4-421F-A76B-5BD51003B11C}"
        ows_SyncClientId="1;#"
        ows_ProgId="1;#"
        ows_ScopeId="1;#{ACA664A3-744D-4671-9610-773BB98C2733}"
        ows__EditMenuTableStart="1_.000"
        ows__EditMenuTableStart2="1"
        ows__EditMenuTableEnd="1"
        ows_LinkFilenameNoMenu="1_.000"
        ows_LinkFilename="1_.000"
        ows_LinkFilename2="1_.000"
        ows_ServerUrl="/Sandbox/bitest/Lists/Produce/1_.000"
        ows_EncodedAbsUrl="http://mydomain.com/Sandbox/bitest/Lists/Produce/1_.000"
        ows_BaseName="1_"
        ows_MetaInfo="1;#"
        ows__Level="1"
        ows__IsCurrentVersion="1"
        ows_ItemChildCount="1;#0"
        ows_FolderChildCount="1;#0">
    </z:row>
    Second, the javascript you provided is wrong, i change it as the following:
            function processResultXML(xData) {
                arraybuilder = "";
                arraynamebuilder = "";
                ItemArray = [];
                ItemNameArray = [];
                var j = 0;
                $(xData.responseXML).find("z\\:row").each(function () {
                    arraybuilder = "";
                    arraynamebuilder = "";
                    $.each(this.attributes, function (i, attrib) {
                        arraybuilder += attrib.name;
                        arraynamebuilder += attrib.value;
                    });
                    ItemNameArray[j] = arraynamebuilder;
                    ItemArray[j] = arraybuilder;
    
                    j++;
                });
            }
    Last, i suggest you do some debugging in javascript develop, you will get the error details from debugging:
    http://msdn.microsoft.com/en-us/library/dd565628(v=vs.85).aspx
    http://stackoverflow.com/questions/1635098/ie-developer-toolbar-how-to-debug-javascript

    Thanks,
    Lhan Han


    • Edited by Lhan Han Tuesday, August 14, 2012 9:49 AM
    • Marked as answer by Lhan Han Thursday, August 23, 2012 9:26 AM
    • Unmarked as answer by Lhan Han Thursday, August 23, 2012 9:26 AM
    • Marked as answer by Qiao Wei Thursday, August 23, 2012 9:27 AM
    • Unmarked as answer by Shrout1 Thursday, August 23, 2012 12:38 PM
    Tuesday, August 14, 2012 9:45 AM
  • Thank you for the response!

    Unfortunately, that XML is returned by the CAML query and I have no power to change its format. I will try using your adapted code! Thank you for taking the time to try correcting it. Odd because it seems that the original StackOverflow post didn't have any comments about users encountering problems.

    Also, I am using IE8 at the moment and I have been using the built in developer tools. I would very much like to use Visual Web Developer Express to debug, but I am having troubles figuring out the tricks to make it work (such as the one listed here)

    I'll try that code!

    Tuesday, August 14, 2012 12:03 PM
  • The answer is quite simple. The XML string that is being returned by SharePoint is not parsed as an XML document in Internet Explorer by default. Google Chrome does it, but IE does not. Here is a link to the Stack Overflow thread that resolved my problem. Below is the JQuery code that parses the XML string as an XML document and allows the data to be iterated.

    var xmlDoc = $.parseXML(xData.xml); 
    $(xmlDoc).find("z\\:row").each(...

    Also note that "responseXML" was not valid in this circumstance. For whatever reason this SOAP response did not have the ".responseXML" property but only the ".xml" property. Using the .xml property was also part of the solution.

    Thank you for trying to solve the problem however.


    • Marked as answer by Shrout1 Thursday, August 23, 2012 12:38 PM
    • Edited by Shrout1 Thursday, August 23, 2012 12:42 PM Edited for clarity
    Thursday, August 23, 2012 12:37 PM