javascriptjqueryxmlinfopath2010

Getting node data from infopath formatted XML


I need to get data from known nodes in an infopath xml. I have the following code Facility returns nothing when I am expecting '140x. Once I have the key to this I will be able to for each my nodes with siblings like my:CompletedByRpt. :( It must work in IE10+ #FeelMyPain

var xmlDoc = $.parseXML(xml); // In debugger I have the xml here. 
var $xml = $(xmlDoc);

Facility = $xml.find('my:cover').find('my:Facility').text();
$("#Facility").append('Facility =' + Facility );

Using this xml source that I do not control: (I made it much smaller and scrubbed a bit)

<?xml version="1.0" encoding="UTF-8"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:PortWineSurvey-Template:-myXSD-2012-11-20T17-06-24" PIVersion="1.0.0.0" href="https://foo.com/Form%20Templates/PortWineSurvey_Template.xsn" productVersion="14.0.0" solutionVersion="1.0.0.583" ?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.3"?>
<my:myFields
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls"
    xmlns:ma="http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes"
    xmlns:d="http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields"
    xmlns:q="http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields"
    xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution"
    xmlns:dms="http://schemas.microsoft.com/office/2009/documentManagement/types"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-11-20T17:06:24"
    xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-us">
  <my:Cover>
    <my:winery>39B</my:winery>
    <my:Facility>140x</my:Facility>
    <my:FacilityTypeGroup>
      <my:FacilityType></my:FacilityType>
      <my:FacilityType>1</my:FacilityType>
      <my:FacilityType>4</my:FacilityType>
      <my:FacilityType>8</my:FacilityType>
      <my:FacilityType>2</my:FacilityType>
      <my:FacilityType>16</my:FacilityType>
    </my:FacilityTypeGroup>
    <my:OtherFacilityType>false</my:OtherFacilityType>
    <my:OtherFacilityTypeComments></my:OtherFacilityTypeComments>
    <my:CompletedByRpt>
      <my:CompletedByRpt_Title>The</my:CompletedByRpt_Title>
      <my:CompletedByRpt_Name>Man</my:CompletedByRpt_Name>
    </my:CompletedByRpt>
    <my:CompletedByRpt>
      <my:CompletedByRpt_Title>The Other</my:CompletedByRpt_Title>
      <my:CompletedByRpt_Name>Man</my:CompletedByRpt_Name>
    </my:CompletedByRpt>
    <my:FacilityOperator_Type>Private</my:FacilityOperator_Type>
    <my:NameOfPFSORpt>
      <my:NameOfPFSORpt_Title>Sr.</my:NameOfPFSORpt_Title>
      <my:NameOfPFSORpt_Name>Inigo Montoya</my:NameOfPFSORpt_Name>
      <my:NameOfPFSORpt_Pos>My big title</my:NameOfPFSORpt_Pos>
      <my:NameOfPFSORpt_Org>My cool workplace</my:NameOfPFSORpt_Org>
    </my:NameOfPFSORpt>
    <my:FacilityOwner_Type>Private</my:FacilityOwner_Type>
  </my:Cover>
  <my:FVF>
    <my:FVF_Compliance>2</my:FVF_Compliance>
  </my:FVF>
  <my:PVFU>
    <my:PVFU_Applicable>true</my:PVFU_Applicable>
    <my:PVFU_Suggestions></my:PVFU_Suggestions>
  </my:PVFU>
  <my:ICIO>
    <my:ICIO_Application>ipsum</my:ICIO_Application>
    <my:ICIO_ApprovedPFSP>2</my:ICIO_ApprovedPFSP>
  </my:ICIO>
  <my:Q1>
    <my:Q1_Ans>
      <my:Q1_Ans_A>0</my:Q1_Ans_A>
      <my:Q1_Ans_B>0</my:Q1_Ans_B>
      <my:Q1_Ans_C>0</my:Q1_Ans_C>
      <my:Q1_Ans_D>0</my:Q1_Ans_D>
      <my:Q1_Ans_E>0</my:Q1_Ans_E>
      <my:Q1_Ans_F>1</my:Q1_Ans_F>
    </my:Q1_Ans>
    <my:Q1_Ans_Val>0</my:Q1_Ans_Val>
    <my:Q1_Confidence>1</my:Q1_Confidence>
    <my:Q1_Comment>Some comments</my:Q1_Comment>
  </my:Q1>
</my:myFields>

My thanks in advance for your patience and help.


Solution

  • Your issue is that the colon (:) is a special character in the jQuery selector. It is used to denote that the following sequence of characters is a pseudo selector, eg. :first or :eq().

    To prevent this behaviour you need to escape the : by preceding it with \\, like this:

    Facility = $xml.find('my\\:cover').find('my\\:Facility').text();
    $("#Facility").append('Facility =' + Facility );