Inconsistent elements in XML returned from QryExp

Answered Inconsistent elements in XML returned from QryExp

  • Sunday, November 04, 2007 7:15 PM
     
     

    H'lo, and thanks for looking.  Hopefully this is an easy one for someone.

     

    I'm making a call to retrieve contacts and specific elements via JavaScript.  Relevent query bits:

     

    Code Block

    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +

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

    " <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\" xmlns=\"http://schemas.microsoft.com/crm/2006/WebServices\">" +

    " <q1:EntityName>contact</q1:EntityName>" +

    " <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +

    " <q1:Attributes>" +

    " <q1:Attribute>fullname</q1:Attribute>" +

    " <q1:Attribute>emailaddress1</q1:Attribute>" +

    " <q1:Attribute>contactid</q1:Attribute>" +

    " <q1:Attribute>telephone1</q1:Attribute>" +

    " <q1:Attribute>parentcustomerid</q1:Attribute>" +

    " </q1:Attributes>" +

    " </q1:ColumnSet>"

     

     

     

    Query works great, but parsing the results is causing me some pain using a for loop and GetElementsByTagName(), due to the fact that elements are not returned in a node if blank.  Sample return nodes:

     

    Code Block

    <BusinessEntity xmlns="http://schemas.microsoft.com/crm/2006/WebServices" xsi:type="contact">

    <fullname>Mike McCready<</< FONT>fullname>

    <emailaddress1>cookiem@ftm.com<</< FONT>emailaddress1>

    <contactid>{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}<</< FONT>contactid>

    <telephone1>206-555-3666<</< FONT>telephone1>

    <parentcustomerid type="account" dsc="0" name="Flight To Mars">{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}<</< FONT>parentcustomerid>

    </< FONT>BusinessEntity>

    <BusinessEntity xmlns="http://schemas.microsoft.com/crm/2006/WebServices" xsi:type="contact">

    <fullname>Les Claypool<</< FONT>fullname>

    <contactid>{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}<</< FONT>contactid>

    <parentcustomerid type="account" dsc="0" name="Primus">{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}<</< FONT>parentcustomerid>

    </< FONT>BusinessEntity>

     

     

     

    Is there a way to persist the attribute set structure, retaining node/element structure,even if an element is blank?

     

    Thanks much.

All Replies

  • Monday, November 05, 2007 8:44 AM
    Moderator
     
     Answered
    No, CRM will not return empty attributes. This is deliberate to allow management of nulls. However, I don't see why this should cause much of a problem when parsing the results

     

  • Monday, November 05, 2007 7:35 PM
     
     

    Bummer.

     

    I'm just figuring out XML parsing via JavaScript - noob, here.  I was looking to parse the results more gracefully than the indexof/substring method I've got from a code sample, and have been building on.

     

    So I was happy to find GetElementsByTagName, which I thought I could use in a For loop to get the values, but the inconsistent return has thrown a fiery monkey into my planned approach - since this returns tag-specific independent arrays, non-required fields have a different array length if they return or not.

     

    Excuse my hackery.

     

    Code Block

     var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

     var result = xmlhttp.responseXML.xml;
     var BEs= result.split("<BusinessEntities>");
     var BE = BEs[1].split("<BusinessEntity");

            xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async=false;
            xmlDoc.loadXML(result); 
           
    for (i = 0; i <= BE.length; i++)
         { 
       var firstphone = BE[i].indexOf("<telephone1>")+12;
       var secondphone = BE[i].indexOf("</telephone1>");
       var phone1 = BE[i].substring(firstphone,secondphone)

    //  Parse XML
            var contName = xmlDoc.getElementsByTagName("fullname")[i].childNodes[0].nodeValue;
            var contID = xmlDoc.getElementsByTagName("contactid")[i].childNodes[0].nodeValue;
            var email1 = xmlDoc.getElementsByTagName("emailaddress1")[i].childNodes[0].nodeValue;

     

    .....More Fun.....

    }

     

     

    Using this I may get the incorrect element based on i and the amount of returned/filled elements.  (phone isn't required, so I frequently get an incorrect phone for a contact in the results)

     

    I thought if I could return a uniform xmldoc, this would work.  I tried returning childnodes of "businessentity", but haven't managed to make that work.  Any suggestions?  Or am I stuck with the substring method?

     

    Thanks much for the reply, btw.