Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing XML with Javascript
    text
    copied!<p>I have an XML Feed that returns data. I am trying to create an Array of elements for a tag but the array always has a lenght of ZERO even if the data is populated.</p> <pre><code>$.ajax({ url: submitUrl, type: "POST", dataType: "xml", data: { request: soapRequest, endPoint: soapEndPoint, header: soapHeader }, error: function() { $('#consolelog').html('&lt;p&gt;Failure.&lt;/p&gt;'); }, success: function(soapResponse) { if (window.DOMParser) { parser=new DOMParser(); xmlDoc=parser.parseFromString(soapResponse,"text/xml"); } else { //IE xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async=false; xmlDoc.loadXML(soapResponse); } consoleID = document.getElementById('consoleLog'); consoleID.appendChild(document.createTextNode("xmlDoc = " + xmlDoc)); linebreak = document.createElement('br'); consoleID.appendChild(linebreak); var xmlArray = xmlDoc.getElementsByTagName("FAgent"); consoleID = document.getElementById('consoleLog'); consoleID.appendChild(document.createTextNode("XMLArray length = " + xmlArray.length)); linebreak = document.createElement('br'); consoleID.appendChild(linebreak); consoleID = document.getElementById('consoleLog'); consoleID.appendChild(document.createTextNode("XML Doc = " + xmlDoc.documentElement.childNodes)); linebreak = document.createElement('br'); consoleID.appendChild(linebreak); </code></pre> <p>the console shows the following output.</p> <pre><code>xmlDoc = [object Document] XMLArray length = 0 XML Doc = [object NodeList] </code></pre> <p>I would like to loop through the array but as it has 0 length I can't figure out how to access the elements?</p> <p>Also the soap response shows up as a file when i check the inspector on the browser developer console</p> <p>XML is </p> <pre><code>&lt;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"&gt; &lt;soap:Body&gt; &lt;GenerateQuotesResponse xmlns="http://www.xxxxx.yyyyy/"&gt; &lt;GenerateQuotesResult&gt; &lt;CustomerName&gt;Test&lt;/CustomerName&gt; &lt;Supplier/&gt; &lt;QuoteDate&gt;2012-11-28T08:06:04.3860424-06:00&lt;/QuoteDate&gt; &lt;QuoteStatus&gt;0&lt;/QuoteStatus&gt; &lt;QuoteStatusMessage/&gt; &lt;As&gt; &lt;Apt&gt; &lt;AptStatusCode&gt;0&lt;/AptStatusCode&gt; &lt;AptStatusMessage&gt;Apt Processing Successful&lt;/AptStatusMessage&gt; &lt;IO&gt;KAL&lt;/IO&gt; &lt;IA&gt;AL&lt;/IA&gt; &lt;FAgents&gt; &lt;FAgent&gt; &lt;ContractID&gt;2553&lt;/ContractID&gt; &lt;AgentName&gt;Test&lt;/AgentName&gt; &lt;Address1&gt;Test Ave.&lt;/Address1&gt; &lt;Address2/&gt; &lt;PostalCode/&gt; &lt;Phone/&gt; &lt;FAX/&gt; &lt;UnitOfMeasure&gt;US&lt;/UnitOfMeasure&gt; &lt;Currency&gt;USD&lt;/Currency&gt; &lt;TaxesIncluded&gt;YES&lt;/TaxesIncluded&gt; &lt;ColtCardAccepted&gt;YES&lt;/ColtCardAccepted&gt; &lt;Note/&gt; &lt;AgentStatusCode&gt;0&lt;/AgentStatusCode&gt; &lt;AgentStatusMessage&gt;Agent Processing Successful&lt;/AgentStatusMessage&gt; &lt;PricingTiers&gt; &lt;PricingTier&gt; &lt;LowVolume&gt;0.00&lt;/LowVolume&gt; &lt;HighVolume&gt;249.00&lt;/HighVolume&gt; &lt;UnitPrice&gt;5.15&lt;/UnitPrice&gt; &lt;VolUOM&gt;US&lt;/VolUOM&gt; &lt;FormattedPrice&gt;5.15&lt;/FormattedPrice&gt; &lt;CurrCode&gt;USD&lt;/CurrCode&gt; &lt;/PricingTier&gt; &lt;PricingTier&gt; &lt;LowVolume&gt;250.00&lt;/LowVolume&gt; &lt;HighVolume&gt;499.00&lt;/HighVolume&gt; &lt;UnitPrice&gt;5.00&lt;/UnitPrice&gt; &lt;VolUOM&gt;US&lt;/VolUOM&gt; &lt;FormattedPrice&gt;5.00&lt;/FormattedPrice&gt; &lt;CurrCode&gt;USD&lt;/CurrCode&gt; &lt;/PricingTier&gt; &lt;PricingTier&gt; &lt;LowVolume&gt;2000.00&lt;/LowVolume&gt; &lt;HighVolume&gt;999999.00&lt;/HighVolume&gt; &lt;UnitPrice&gt;4.55&lt;/UnitPrice&gt; &lt;VolUOM&gt;US&lt;/VolUOM&gt; &lt;FormattedPrice&gt;4.55&lt;/FormattedPrice&gt; &lt;CurrCode&gt;USD&lt;/CurrCode&gt; &lt;/PricingTier&gt; &lt;/PricingTiers&gt; &lt;/FAgent&gt; &lt;FAgent&gt; &lt;ContractID&gt;5xxx&lt;/ContractID&gt; &lt;AgentName&gt;test 2&lt;/AgentName&gt; &lt;Address1/&gt; &lt;Address2/&gt; &lt;PostalCode/&gt; &lt;Phone/&gt; &lt;FAX/&gt; &lt;UnitOfMeasure&gt;US&lt;/UnitOfMeasure&gt; &lt;Currency&gt;USD&lt;/Currency&gt; &lt;TaxesIncluded&gt;YES&lt;/TaxesIncluded&gt; &lt;ColtCardAccepted&gt;YES&lt;/ColtCardAccepted&gt; &lt;Note&gt;Test 7&lt;/Note&gt; &lt;AgentStatusCode&gt;0&lt;/AgentStatusCode&gt; &lt;AgentStatusMessage&gt;Agent Processing Successful&lt;/AgentStatusMessage&gt; &lt;PricingTiers&gt; &lt;PricingTier&gt; &lt;LowVolume&gt;0.00&lt;/LowVolume&gt; &lt;HighVolume&gt;500.00&lt;/HighVolume&gt; &lt;UnitPrice&gt;4.80&lt;/UnitPrice&gt; &lt;VolUOM&gt;US&lt;/VolUOM&gt; &lt;FormattedPrice&gt;4.80&lt;/FormattedPrice&gt; &lt;CurrCode&gt;USD&lt;/CurrCode&gt; &lt;/PricingTier&gt; &lt;PricingTier&gt; &lt;LowVolume&gt;501.00&lt;/LowVolume&gt; &lt;HighVolume&gt;1000.00&lt;/HighVolume&gt; &lt;UnitPrice&gt;4.55&lt;/UnitPrice&gt; &lt;VolUOM&gt;US&lt;/VolUOM&gt; &lt;FormattedPrice&gt;4.55&lt;/FormattedPrice&gt; &lt;CurrCode&gt;USD&lt;/CurrCode&gt; &lt;/PricingTier&gt; &lt;/PricingTiers&gt; &lt;/FAgent&gt; &lt;/FAgents&gt; &lt;/Apt&gt; &lt;/As&gt; &lt;/GenerateQuotesResult&gt; &lt;/GenerateQuotesResponse&gt; &lt;/soap:Body&gt; &lt;/soap:Envelope&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload