getElementsByTagName returning empty set

B

Ben

I have a web service that returns the following xml:

<?xml version="1.0" encoding="utf-8" ?>
<NewDataSet>
<Addresses>
<XML_F52E2B61-18A1-11d1-B105-00805F49916B>
<CustomerAddressBase
CustomerAddressId="259DA89E-3B3F-DB11-90EA-0016353D15C7"
AddrName="PRIMARY" />
<CustomerAddressBase
CustomerAddressId="269DA89E-3B3F-DB11-90EA-0016353D15C7" />
<CustomerAddressBase
CustomerAddressId="9778EE83-3B70-DB11-B20C-0016353D15C7" AddrName="test
address" />
</XML_F52E2B61-18A1-11d1-B105-00805F49916B>
</Addresses>
</NewDataSet>

I want to loop through each CustomerAddressBase tag and pull out the
CustomerAddressId and AddrName. I can use getElementsByTagName on tag
XML_F52E2B61-18A1-11d1-B105-00805F49916B with no problems, but when I
try to use it to pull CustomerAddressBase I get an empty set and no
length. Here's the code I've been using to test this:

// Instantiate at connection to the Web service and call the get
method.
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var objXmlDoc = new ActiveXObject("Microsoft.XMLDOM");

// crmForm.all.salutation.DataValue =
"http://bert:5555/webservices/clientaddress.asmx/SRReturnAddr?AccountGUID="
+ s

xmlhttp.open("get",
"http://bert:5555/webservices/clientaddress.asmx/SRReturnAddr?AccountGUID="
+ s, false);
xmlhttp.send();

//crmForm.all.emailaddress1.DataValue = xmlhttp.responseText;
objXmlDoc.loadXML(xmlhttp.responseText);
//var noOfCustomerAddressTags =
objXmlDoc.getElementsByTagName("XML_F52E2B61-18A1-11d1-B105-00805F49916B").length;

var xmltag=
objXmlDoc.getElementsByTagName("XML_F52E2B61-18A1-11d1-B105-00805F49916B");

var CustomerAddressTags=
xmltag[0].getElementsByTagName("CustomerAddressBase");

var noOfCustomerAddressTags =
xmltag[0].getElementsByTagName("CustomerAddressBase").length;

crmForm.all.emailaddress1.DataValue =
CustomerAddressTags[0].firstChild.nodeValue;

for (var x = 0; x < noOfCustomerAddressTags; x++)
{
crmForm.all.emailaddress1.DataValue = "test";
}



Does anyone have any idea why I can't pull the CustomerAddressBase?
I've tried it using
objXmlDoc.getElementsByTagName("CustomerAddressBase") with no luck as
well. Thanks in advance.

Ben
 
T

Thomas 'PointedEars' Lahn

Ben said:
I have a web service that returns the following xml:

<?xml version="1.0" encoding="utf-8" ?>
<NewDataSet>
<Addresses>
<XML_F52E2B61-18A1-11d1-B105-00805F49916B>
<CustomerAddressBase
CustomerAddressId="259DA89E-3B3F-DB11-90EA-0016353D15C7"
AddrName="PRIMARY" />
<CustomerAddressBase
CustomerAddressId="269DA89E-3B3F-DB11-90EA-0016353D15C7" />
<CustomerAddressBase
CustomerAddressId="9778EE83-3B70-DB11-B20C-0016353D15C7" AddrName="test
address" />
</XML_F52E2B61-18A1-11d1-B105-00805F49916B>
</Addresses>
</NewDataSet>

I want to loop through each CustomerAddressBase tag and pull out the
CustomerAddressId and AddrName. I can use getElementsByTagName on tag
XML_F52E2B61-18A1-11d1-B105-00805F49916B with no problems, but when I
try to use it to pull CustomerAddressBase I get an empty set and no
length. Here's the code I've been using to test this:

// Instantiate at connection to the Web service and call the get
method.
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var objXmlDoc = new ActiveXObject("Microsoft.XMLDOM");

// crmForm.all.salutation.DataValue =
"http://bert:5555/webservices/clientaddress.asmx/SRReturnAddr?AccountGUID="
+ s

xmlhttp.open("get", ^^^
"http://bert:5555/webservices/clientaddress.asmx/SRReturnAddr?AccountGUID="
+ s, false);

Should be "GET".
xmlhttp.send();

//crmForm.all.emailaddress1.DataValue = xmlhttp.responseText;
objXmlDoc.loadXML(xmlhttp.responseText);
//var noOfCustomerAddressTags =
objXmlDoc.getElementsByTagName("XML_F52E2B61-18A1-11d1-B105-00805F49916B").length;

var xmltag=
objXmlDoc.getElementsByTagName("XML_F52E2B61-18A1-11d1-B105-00805F49916B");

var CustomerAddressTags=
xmltag[0].getElementsByTagName("CustomerAddressBase");

var noOfCustomerAddressTags =
xmltag[0].getElementsByTagName("CustomerAddressBase").length;

crmForm.all.emailaddress1.DataValue =
CustomerAddressTags[0].firstChild.nodeValue;

for (var x = 0; x < noOfCustomerAddressTags; x++)
{
crmForm.all.emailaddress1.DataValue = "test";
}

Let's clean this up at first, making it easily legible:

var xmltags = objXmlDoc.getElementsByTagName(
"XML_F52E2B61-18A1-11d1-B105-00805F49916B");

/* var noOfCustomerAddressTags = xmltags.length; */

var customerAddressTags =
xmltag[0].getElementsByTagName("CustomerAddressBase");

var noOfCustomerAddressTags = customerAddressTags.length;

crmForm.all.emailaddress1.DataValue =
customerAddressTags[0].firstChild.nodeValue;

// this loop does not make sense,
// especially since noOfCustomerAddressTags was not defined above
for (var x = 0; x < noOfCustomerAddressTags; x++)
{
crmForm.all.emailaddress1.DataValue = "test";
}
Does anyone have any idea why I can't pull the CustomerAddressBase?

Probably you are making too many assumptions without testing any of them.
Proper coding would include feature tests such as

var o = ..., o2, o3;
if (o && o.getElementsByTagName
&& (o2 = o.getElementsByTagName(...))
&& (o3 = o2[0])
&& ...)
{
...
}

And you should use a debugger; links are in the FAQ:

http://jibbering.com/faq/


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top