Closed Bug 241392 Opened 20 years ago Closed 20 years ago

Failure to read individual XML Node values.

Categories

(Core :: DOM: Core & HTML, enhancement)

x86
Windows XP
enhancement
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: firec1de, Unassigned)

References

()

Details

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7b) Gecko/20040421
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7b) Gecko/20040421

In http://db.thedilly.com/chat.xml?room=1, I test for mozilla compatibility on
any level with the ability to parse XML on a Node level. The source is available
in its entirety as is on under that URL. The testing code starts near "/*
FAILURE FOR MOZILLA" as it is commented out. This project works under IE, and
uses the documentation found HERE: http://www.mozilla.org/xmlextras/#QA and HERE
http://lxr.mozilla.org/seamonkey/source/extensions/xmlextras/tests/


Reproducible: Always
Steps to Reproduce:
1. Load url in browser.
2. Click 'Ok' to alert message.
3. View source and save to computer.
4. Uncomment testing information located near "/* FALURE FOR MOZILLA"
5. Change line 302 from 'var url = "chat.xml?";' to 'var url =
"http://db.thedilly.com/chat.xml?";'
6. Save changes and reload document. Errors appear in Javascript Console.
Application freezes after each alert with failed read. Uncomment individually
for bug testing.

Actual Results:  
Mozilla fails to render data to alerts showing that it cannot access XML DOM
Nodes in any manner documented in comments.

Expected Results:  
Reproducable in IE. Chat will load a list of messages and buddies. When logged
in can send a recieve data.
Those steps don't quite work to reproduce because of the cross-site restrictions
on HTMLHttpRequest, but I grabbed your XML and put up a testcase at
http://web.mit.edu/bzbarsky/www/parseXML.html (not permanent, but I'll leave it
there for a few weeks).

Everything seems correct.  Note that your script has the following issues:

1)  |xmlObj.childNodes[0]| is a doctype node, not the root element.  The root
    element is at |xmlObj.childNodes[2]|.  If you want the rootElement, I
    suggest |xmlObj.documentElement|.
2)  The various properties you're trying to get off the child node don't exist.
3)  |xmlObj.getElementsByTagName("server_time").nodeValue| should be
    |xmlObj.getElementsByTagName("server_time")[0].nodeValue|, no?  You're
    trying to get the nodevalue of a node list, and it doesn't have one, of
    course.  Can also use |.item(0)| instead of |[0]| here.
4)  |xmlObj.getElementById("server_time").value| won't do anything, since there
    is no node in that document with that ID.... (I don't know whether your DTD
    declares that to be an ID attribute somewhere, but Mozilla's XML parser is
    not validating).

If you fix these errors, do you still see a problem?
Summary: Failure to read individual XML Node values. → Failure to read individual XML Node values.
http://validator.w3.org/check?uri=http%3A%2F%2Fdb.thedilly.com%2Fchat.xml%3Froom%3D1%3Bshow%3Dxml

i see it validating in the w3's validator. i just now removed those XML nodes
from the doc since they werent being used. 

i updated the documentation for the error checking to show the actual errors
that occurred and retested using the steps you suggested. the commented outputs
are either what showed up under the Javascript Console or the alert box. You may
want to update the html and XML document stored remotely with the current
document as not to confuse anyone.

This remains unresolved. A source example of the changes you suggest would be
helpful.
> i see it validating in the w3's validator.

I didn't say the document was invalid.  I said you messed up the DOM calls to
access it.

> using the steps you suggested.

Er.. you left the childNodes[0] thing and all...

> You may want to update the html and XML document stored remotely

Did that.

> A source example of the changes you suggest would be helpful.

I'm not sure what you're trying to accomplish, so I don't know what changes you
want to see.  I added some "XXXbz" comments to the document at
http://web.mit.edu/bzbarsky/www/parseXML.html that should maybe clarify some issues.

I highly recommend taking a look at
http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html -- you seem to
be heavily confusing nodes and nodelists (two totally different things), which
is the cause of most of the "problems" you see...

Since we're accessing the parsed document just fine, marking invalid --
everything is behaving exactly as it should.
Status: UNCONFIRMED → RESOLVED
Closed: 20 years ago
Resolution: --- → INVALID
>I didn't say the document was invalid.  I said you messed up the DOM calls to
access it.

Those werent intended to be valid. the attempt was to retrieve a text value
usable by the Javascript application. In this respect I've found no documents on
the mozilla site or through goggle outlining how to retrieve individual text
values. I hope this clears up some frustration.

>> You may want to update the html and XML document stored remotely
>Did that.

Appreciated.

>I'm not sure what you're trying to accomplish, so I don't know what changes you
want to see.  I added some "XXXbz" comments to the document at
http://web.mit.edu/bzbarsky/www/parseXML.html that should maybe clarify some issues.

is there a way that the values will return as the actual values in the XML.
"null" is not something the browser can append to the file nor is "[object
Text]". This is to what I am referring.

the example I was referring to was one that would retrieve the text inside the
XML, as noted here:

<advertisements>
Your ad here for only $50.<br>Text ad's dirt cheap!<br><br><a
href="advertising.wtd" target="_blank">Find out more...</a>
</advertisements>

this is not null, and should be usable if there is in fact a way to retrieve it.
Ah, I see.  If you just want the text, you can use the .textContent property
(see
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Node3-textContent).
 Note that this is only present in Mozilla since version 1.5; if you need to
support builds older than that you may want to look into using DOM ranges and
their toString() method
(http://www.w3.org/TR/2000/REC-DOM-Level-2-Traversal-Range-20001113/ranges.html#Level-2-Range-idl
and scroll down).

If by "text" you mean "a serialization of the DOM inside the XML node into a
string", that's a slightly harder problem that's not solvable with the W3C apis
without writing your own serializer.  You can use

  var string = (new XMLSerializer()).serializeToString(node);

to do it using Mozilla's XML serializer.

You're correct that our documentation is abysmal in this regard.  I've filed bug
241414 on improving it.
I would suggest creating some code examples for whatever ends up making its way
into documentation. Even still it took me some trial and error to figure out how
exactly this behaved. It's nothing like the IE syntax. Here is some testing to
show how to navigate through the xml files nested structure.

//					alert(xmlObj.textContent+"\n\nWhole XML document");
//					alert(xmlObj.childNodes.item(0).textContent+"\n\nItem 0"); //
//					alert(xmlObj.childNodes.item(1).textContent+"\n\n settings"); // settings
//					alert(xmlObj.childNodes.item(1).childNodes.item(0).textContent+"\n\n"); //
					alert(xmlObj.childNodes.item(1).childNodes.item(1).textContent+"\n\n total
in room"); // total in room
//					alert(xmlObj.childNodes.item(1).childNodes.item(2).textContent+"\n\n"); //
					alert(xmlObj.childNodes.item(1).childNodes.item(3).textContent+"\n\n
topic"); // topic
//					alert(xmlObj.childNodes.item(1).childNodes.item(4).textContent+"\n\n"); //
					alert(xmlObj.childNodes.item(1).childNodes.item(5).textContent+"\n\n last
message"); // last message
//					alert(xmlObj.childNodes.item(1).childNodes.item(6).textContent+"\n\n"); //
//					alert(xmlObj.childNodes.item(1).childNodes.item(7).textContent+"\n\n"); //
//					alert(xmlObj.childNodes.item(1).childNodes.length+"\n\nItem length");
//					alert(xmlObj.childNodes.item(2).textContent+"\n\nItem 2"); // 
					alert(xmlObj.childNodes.item(3).textContent+"\n\n messages"); // messages
					alert(xmlObj.childNodes.item(3).childNodes.item(1).textContent+"\n\n
message"); // message
				
alert(xmlObj.childNodes.item(3).childNodes.item(1).childNodes.item(1).textContent+"\n\n
name"); // name
				
alert(xmlObj.childNodes.item(3).childNodes.item(1).childNodes.item(3).textContent+"\n\n
body"); // body
				
alert(xmlObj.childNodes.item(3).childNodes.item(1).childNodes.item(5).textContent+"\n\n
action"); // action
				
alert(xmlObj.childNodes.item(3).childNodes.item(1).childNodes.item(7).textContent+"\n\n
account id"); // account id
					alert(xmlObj.childNodes.item(3).childNodes.length+"\n\nItem length"); //
total length = (given length -1)/2
					alert((xmlObj.childNodes.item(3).childNodes.length - 1)/2+"\n\nactual
length"); // total length = (given length -1)/2
//					alert(xmlObj.childNodes.item(4).textContent+"\n\nItem 4"); //
					alert(xmlObj.childNodes.item(5).textContent+"\n\n lists"); // lists
//					alert(xmlObj.childNodes.item(6).textContent+"\n\nItem 6"); // 
					alert(xmlObj.childNodes.item(7).textContent+"\n\n advertisement"); //
advertisement
					alert(xmlObj.childNodes.length+"\n\nItem length");




As you can see, this is different from IE because IE creates a '.length' which
represents how many elements are in node where Mozilla returns double the number
plus one for some reason. This could confuse a lot of people. 
> where Mozilla returns double the number plus one for some reason

The reason is described in bug 26179.  The short story is that IE randomly
decided to drop whitespace-only text nodes from the DOM.
Component: DOM: Core → DOM: Core & HTML
QA Contact: ian → general
You need to log in before you can comment on or make changes to this bug.