By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,576 Members | 1,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,576 IT Pros & Developers. It's quick & easy.

getElementsByTagName returning empty set

P: n/a
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").lengt h;

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("CustomerAddressBas e") with no luck as
well. Thanks in advance.

Ben

Dec 4 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Ben wrote:
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").lengt h;

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
--
Indiana Jones: The Name of God. Jehovah.
Professor Henry Jones: But in the Latin alphabet,
"Jehovah" begins with an "I".
Indiana Jones: J-...
Dec 9 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.