Ok, I've got to say that some of what you are saying is actually making
sense... 

You are right that it is impossible to have white space outside of a tag
other than the root tag since every other tag is wrapped in a parent
tag, and therefore it is "in" an ancestor tag.  Seeing an example with
text other than white space helped.  

In all my examples, I have been assuming that if data (text) was stored
inside of a start and end tag, then there would not also be nested tags
at that level.  Obviously that would not always be true.

Also, you said I was "You are confusing elements with nodes." And I
think you are right-- I didn't think there was a difference, and here is
why:
In my code, I iterated over the parent element's childNodes array
expecting each index in the array to correspond with each TAG (element)
which was a direct descendant of that parent element.  Obviously I got
more than that since the text nodes were part of that array as well.  

My introduction to XML was through ColdFusion and in CF you do not
access the text of an element the same way you would a child element.  

Example:
<cfset string = "
        <fruit title=""test"">
                Gosh, fruit sure is tasty!
                <banana />
                <apple />
                <pear />
        </fruit>">

<cfset xml = xmlparse(string)>

<cfdump var="#xml#"><br>

<cfoutput>
        <cfloop collection="#xml.fruit#" item="i">
                #i#<br>
        </cfloop>
</cfoutput>

The output of that code is:
banana
apple
pear

Apparently CF treats the collection as ONLY the child elements (tags)
NOT all the child nodes (which apparently includes the XmlText.

Browser Example:
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
    
        var string = '\
                <fruit title=\"test\">\
                        Gosh, fruit sure is tasty!\
                        <banana />\
                        <apple />\
                        <pear />\
                </fruit>';
                
        if (window.ActiveXObject)
          {
          var doc=new ActiveXObject("Microsoft.XMLDOM");
          doc.async="false";
          doc.loadXML(string);
                var objParseError = doc.parseError;
          }
        // code for Mozilla, Firefox, Opera, etc.
        else
          {
          var parser=new DOMParser();
          var doc=parser.parseFromString(string,"text/xml");
          }
        var xml = doc.documentElement;

         for (var i = 0; i < xml.childNodes.length; i++)
                {
                        document.write(xml.childNodes[i].tagName + ' ' +
xml.childNodes[i].text + '<br>');
                }

</SCRIPT>       

The output of that code in IE would be:
undefined Gosh, fruit sure is tasty!
banana 
apple 
pear

The output of that code in FireFox would be:
undefined undefined
banana undefined
undefined undefined
apple undefined
undefined undefined
pear undefined
undefined undefined


As you can see my results are very different, but I think understanding
that CF was looping only over child elements while my Javascript was
looping over all child nodes (which is a superset of the list of
elements) helps quite a bit.  Obviously IE ignored all text which was
only white space.  

Truthfully, I'm not sure how to iterate over child elements excluding
text nodes in JavaScript.  
I could use getElementsByTagName() but only helps if I know the names of
the tags.  What would be the proper way to loop over just the child
tags??

At any rate, while I don't like the way JavaScript iterates over all
child notes including the text, and how Mozilla drastically differs from
IE, I think I'll have to admit that I do see why that text is
significant.  I had never thought of having text AND child tags at the
same time.

~Brad

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
ColdFusion MX7 and Flex 2 
Build sales & marketing dashboard RIA’s for your business. Upgrade now
http://www.adobe.com/products/coldfusion/flex2?sdid=RVJT

Archive: 
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:281346
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4

Reply via email to