XML DOM troubles

S

sklett

** I tried to find a more suitable NG for this issue but didn't find much
(on the MS server) the ones I did find haven't had posts in a loooong time.
Sorry for the somewhat off topic post **

My Situation:
client side scripts make an ajax request for some data
aspx page processes request and writes back an XML (not perfectly formed)
file
client side script receives data and attempts to parse the XML (this is
where I'm having a hard time)

Here is a sample of what the XML looks like:
[xml sample]
<?xml version="1.0" encoding="utf-8" ?>
<MedicalProfessionals>
<count>3</count>
<MedicalProfessional>
<FirstName>David</FirstName>
<LastName>Jones</LastName>
<Address1>123 SomeStreet</Address1>
<Address2>Suite #4</Address2>
<City>Irvine</City>
<State>California</State>
<ZipCode>92614</ZipCode>
<Phone>949-333-2222</Phone>
<AltPhone>949-888-9874</AltPhone>
<Fax>949-111-2345</Fax>
<Notes>This is a good Dr.</Notes>
<UPIN>11223344</UPIN>
<NSInternalID>1</NSInternalID>
</MedicalProfessional>
</MedicalProfessionals>
[/xml sample]

Here is some of the client parsing script:
[client script parsing code]
medicalProfessionals =
medProdXMLDoc.getElementsByTagName("MedicalProfessional");
alert('number of medical professionals returned: ' +
medicalProfessionals.length);
alert(medicalProfessionals[0].childNodes.length);
for(i = 0; i < medicalProfessionals[0].childNodes.length; i++)
{
alert(medicalProfessionals[0].childNodes.nodeName);
alert(medicalProfessionals[0].childNodes.nodeValue);
}
[/client script parsing code]

When the above codes runs, it correctly reports the length of
medicalProfessionals
It correctly reports the number of childNodes
It correctly reports the NAME of the child nodes in the loop
It reports null for all the child node values - this is the part that is
kicking my butt.

I have checked that the XML being sent to the client is populated with data
for those nodes and it is. I really don't know why it would report them as
null.

Does anyone see a problem with this code? Any ideas? I'm about to revert
to a delimited string and parse the "old fashioned" way but I would much
rather use XML.

2 more questions:
1) Is there a way to load an XML STRING using firefox? I have been trying
to pass the string to the load() method of the firefox XML Dom Parser object
but it wants a file path. Any ideas?
2) Is there a way to access a childNode by nodeName instead of index? I
hate accessing things by index as it's so prone to bugs. I know I can
implement a search function to find them, but it sure seems like this is
something that should be available out of the box. maybe I'm missing it?

Thanks for reading and again, sorry for the off topic post, but you guys are
all very knowledgeable with web technologies and I didn't know where else to
turn. :0)

-Steve
 
M

Martin Honnen

sklett wrote:

<MedicalProfessional>
<FirstName>David</FirstName>

medicalProfessionals =
medProdXMLDoc.getElementsByTagName("MedicalProfessional");
alert('number of medical professionals returned: ' +
medicalProfessionals.length);
alert(medicalProfessionals[0].childNodes.length);
for(i = 0; i < medicalProfessionals[0].childNodes.length; i++)
{
alert(medicalProfessionals[0].childNodes.nodeName);
alert(medicalProfessionals[0].childNodes.nodeValue);
}
[/client script parsing code]

When the above codes runs, it correctly reports the length of
medicalProfessionals
It correctly reports the number of childNodes
It correctly reports the NAME of the child nodes in the loop
It reports null for all the child node values -


Well in the DOM if you have an element node then nodeValue is defined as
null. It is _not_ the text content of the element. If you want that then
you need the text property for MSXML or textContent for Firefox/Mozilla
or Opera 9.
And if you script XML on the web then be aware that the different
parsers your script will encounter might treat white space different,
for instance Mozilla will have text nodes with white space between
element nodes. So a script looking for an element node with e.g.
childNodes[certainIndex] might find an element node with one parser
(e.g. MSXML) but a text node with another parser (e.g. Mozilla). Thus if
you are looking for element child nodes then if you loop through
childNodes or index it check the nodeType, or better yet don't use
childNodes but rather XPath (e.g. selectSingleNode/selectNodes with
MSXML, DOM Level 3 XPath API with Mozilla and with Opera 9) to look for
the child elements.




Client-side scripting questions are better asked in comp.lang.javascript
or if they relate to XML in comp.text.xml. The Microsoft server has
microsoft.public.xml for instance and various JScript scripting groups.
 
S

sklett

Hi Martin,

Thank you for the post. I have added the xml newsgroup and will try out
Google groups to access the others (my ISP doesn't have a news server that I
can use)

I'm not sure if it's appropriate for me to continue this thread here now
that you have directed me to the NGs so I will take the issue over to one of
them, hopefully you are active on the other NGs so we can pick this up again
;0)

Have a good day,
Steve


Martin Honnen said:
sklett wrote:

<MedicalProfessional>
<FirstName>David</FirstName>

medicalProfessionals =
medProdXMLDoc.getElementsByTagName("MedicalProfessional");
alert('number of medical professionals returned: ' +
medicalProfessionals.length);
alert(medicalProfessionals[0].childNodes.length);
for(i = 0; i < medicalProfessionals[0].childNodes.length; i++)
{
alert(medicalProfessionals[0].childNodes.nodeName);
alert(medicalProfessionals[0].childNodes.nodeValue);
}
[/client script parsing code]

When the above codes runs, it correctly reports the length of
medicalProfessionals
It correctly reports the number of childNodes
It correctly reports the NAME of the child nodes in the loop
It reports null for all the child node values -


Well in the DOM if you have an element node then nodeValue is defined as
null. It is _not_ the text content of the element. If you want that then
you need the text property for MSXML or textContent for Firefox/Mozilla or
Opera 9.
And if you script XML on the web then be aware that the different parsers
your script will encounter might treat white space different, for instance
Mozilla will have text nodes with white space between element nodes. So a
script looking for an element node with e.g. childNodes[certainIndex]
might find an element node with one parser (e.g. MSXML) but a text node
with another parser (e.g. Mozilla). Thus if you are looking for element
child nodes then if you loop through childNodes or index it check the
nodeType, or better yet don't use childNodes but rather XPath (e.g.
selectSingleNode/selectNodes with MSXML, DOM Level 3 XPath API with
Mozilla and with Opera 9) to look for the child elements.




Client-side scripting questions are better asked in comp.lang.javascript
or if they relate to XML in comp.text.xml. The Microsoft server has
microsoft.public.xml for instance and various JScript scripting groups.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,068
Latest member
MakersCBDIngredients

Latest Threads

Top