Problem with commas not showing up

A

Allan

Hi All,

I am having a problem parsing an xml file I am getting from another server.

This is the portion of the xml I am getting I am interested in:

<DestinationAddress>
<City>Leawood</City>
<StateOrProvinceCode>KS</StateOrProvinceCode>
<PostalCode>66209</PostalCode>
<CountryCode>US</CountryCode>
</DestinationAddress>


I am trying to write the above result in an html page as follow:

Leawood, KS, 662009, US

However my code keeps producing the following omitting the commas:

, LeawoodKS662009US



This is my code (I am using jscript!!)

....
var objXMLDOM = Server.CreateObject("MSXML2.DOMDocument");
objXMLDOM.loadXML(objXMLHttp.responseText);
objXMLDOM.async = false;

var Address = objXMLDOM.getElementsByTagName("DestinationAddress");

var AddressText = "";

for (var i = 0; i < Address.length; i++)
{
AddressText = AddressText + ", " + Address.item(i).text }

Response.Write(AddressText);
 
K

Keith M. Corbett

Allan said:
I am trying to write the above result in an html page as follow:

Leawood, KS, 662009, US

However my code keeps producing the following omitting the commas:

, LeawoodKS662009US

You want to process multiple text nodes, four of them in this example. But
your program is getting all the text at once.
var Address = objXMLDOM.getElementsByTagName("DestinationAddress");

Now you have a handle on a sequence of elements, in this case a sequence of
length 1, containing the one DestinationAddress element.
for (var i = 0; i < Address.length; i++)
{
AddressText = AddressText + ", " + Address.item(i).text }

Indeed, your program is doing exactly what you told it to. :)

You need to get a node list containing the children nodes of the
DestinationAddress element and iterate over that list. Then for each
element, you need to get a node list containing the text node(s). (The DOM
API may break up a sequence of text into more than one text node.)

/kmc
 
K

Keith M. Corbett

Also: this adds a comma every time through the loop. You only want to add a
comma after the first iteration.

/kmc
 

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

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top