Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript xml loading

Status
Not open for further replies.

solmyr72

Programmer
Aug 8, 2004
20
IL
Hi,

I'm new to the javascript XML(DOM) API.
I'm trying to:
- Read some xml data (from the server, synchronously).
- Parse it (using javascript DOM API).

Unfortunately, javascript keept telling me that it found an empty xml document (no child nodes)...
Could anyone guess what's wrong ?
BTW I can tell the request *does* arrive to my server (I'm logging it).

Thanks a lot.


My server-side xml (generated through java/jsp).
Notes:
- I can tell this *is* being called:
- I've tried it with and without the '<? xml ... ?> directive.
Code:
<%@page contentType="text/xml" %>
<? xml version="1.0" ?>
<users><user name='john'></user></users>

<%
	System.out.println("server sending xml response");
%>

My javascript (meant for Firefox 5):
Code:
// Firefox 5 code:
function testXml(){
   var xmldoc = document.implementation.createDocument(null, null, null);
   xmldoc.async = false; 			
   xmldoc.load("[URL unfurl="true"]http://myDomain/MT/MANTIS/pelTest");[/URL]
   var children= xmldoc.childNodes;
   alert(children.length);
 
Just a few suggestions see if they lead to positive results.

[1] The prolog <? xml with a space between is not valid. Take the space out.
[tt]<[highlight]?x[/highlight]ml version="1.0" ?>[/tt]
[2] Take out the last jsp output line out, it makes the xml fail at parsing because it would be mal-formed.
[tt][red]//[/red]System.out.println("server sending xml response");[/tt]
[3] Set the first two param to empty string, not null.
[tt]var xmldoc = document.implementation.createDocument([blue]"", ""[/blue], null);[/tt]
[4] Name the jsp page to having extension .jsp. (This may not be necessary, I am not sure the necessary settings where it may work as you script it without extension.)
[tt]xmldoc.load("[blue].jsp[/blue]");[/tt]
[5] You can monitor whether there is a parsing/loading error by alerting the return value of the load line. If the result is false, you can anticipate problem.
[tt] var bret=xmldoc.load(" alert(bret); //should be returned true if things work[/tt]
 
Amendment
[2/[red]ignored[/red]] Upon re-reading, you can ignore the note [2]. It is immaterial to the problem. It writes to server log and hence has no impact on the .load line. My bad!
 
Thank you very much, 'tsuji', for this detailed reply.
You were right, of course, especially with [5] : the 'load' function returns false.
Would you happen you know if javascript/browsers can report the exact error details ?

In any case, thanks very much.
 
May be you can test a two-line test page (test.jsp) say like this.
[tt]
<%@page contentType="text/xml;charset=UTF-8" %>
<% out.print("<users><user name='john'></user></users>"); %>
[/tt]
The .load line should point to it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top