/    Sign up×
Community /Pin to ProfileBookmark

Reading XML from Javascript

Hi,

I am stuck on a Javascript problem. It would be grateful if anybody help me.

From javascript, I am opening a popup window and requesting a url, which sends a xml as a response to the popup window. From this javascript, I want to read that xml content:

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<aaa>
<vvvv><![CDATA[1]]></vvvv>
</aaa>

I tried

del_window=window.open(“http://abc.com/_xmlservice”,””,”width=1,height=1“);
var ele = del_window.document.documentElement; //this is returning “HTML”

and

del_window.document.getElementsByTagName(“xxx”)

But nothing worked out.

Thanks in advance

Jayakrishna

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Aug 25.2005 — Play with this:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;
var reqXML; <br/>
function LoadXMLDoc(url){
if(window.XMLHttpRequest){
reqXML = new XMLHttpRequest();
}
else if(window.ActiveXObject){
reqXML = new ActiveXObject("Microsoft.XMLHTTP");
}

<i> </i> if(reqXML){
<i> </i> reqXML.open("POST", url, true);
<i> </i> reqXML.onreadystatechange = BuildXMLResults;
<i> </i> reqXML.send(null);
<i> </i> }
<i> </i> }

<i> </i> function BuildXMLResults(){
<i> </i> if(reqXML.readyState == 4){
<i> </i> if(reqXML.status == 200){
<i> </i> var xmlDoc = reqXML.responseXML.documentElement;
<i> </i> var nodes = xmlDoc.childNodes;
<i> </i> document.getElementById("divOut").innerHTML = nodes[0].firstChild.text;
<i> </i> }
<i> </i> }
<i> </i> }

<i> </i> window.onload = function(){
<i> </i> LoadXMLDoc("http://radio.javaranch.com/pascarello/rss.xml");
<i> </i> }
<i> </i>&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="divOut"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;


Eric
Copy linkTweet thisAlerts:
@BigMoosieAug 25.2005 — Eric, BuildXMLResults() is called 4 time I beleive, each time the state changes, every time I see AJAX implemented there is a condition like you have done to make sure that it is state 4 (completed). Are there any practical uses to executing scripts on any of the other states?
Copy linkTweet thisAlerts:
@A1ien51Aug 25.2005 — For anyone that does not what the readyState stand for this is the meaning behind them:

0 = uninitialized

1 = loading

2 = loaded

3 = interactive

4 = complete

The only time we get to mainpulate the returned data on the client is when we reach complete or stage 4. Now could we do anything with the other stages? We could display a status to the user, but take the code I posted for and write out the timestamps when the script calls this. You would see that it is fast and rather useless IMO. Some people use stage 2 or 3 to use a timeout with an abort process in case the request takes too long, but I do not implement that since there are too many factors to look at. (What about dial-up users with a large XML file being transfered?)

We could avoid the if else satement in the build function so it is only called when we reach complete like I did below:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;
var reqXML; <br/>
function LoadXMLDoc(url){
if(window.XMLHttpRequest){
reqXML = new XMLHttpRequest();
}
else if(window.ActiveXObject){
reqXML = new ActiveXObject("Microsoft.XMLHTTP");
} <br/>
if(reqXML){
reqXML.open("POST", url, true);
reqXML.onreadystatechange = function(){
if(reqXML.readyState==4){
if(reqXML.status == 200)BuildXMLResults();
else alert("There was a problem retrieving the XML data:n" + reqXML.statusText);
}
}
reqXML.send(null);
}
}

<i> </i> function BuildXMLResults(){
<i> </i> var xmlDoc = reqXML.responseXML.documentElement;
<i> </i> var nodes = xmlDoc.childNodes;
<i> </i> document.getElementById("divOut").innerHTML = nodes[0].firstChild.text;
<i> </i> }

<i> </i> window.onload = function(){
<i> </i> LoadXMLDoc("http://radio.javaranch.com/pascarello/rss.xml");
<i> </i> }
<i> </i>&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="divOut"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;


I personally do not use this method to make my request to the server. I use an OO approach that has a built in queing mechanism. (We use it in my new book Ajax in Action.) When I get some free time and I am allowed to write some articles on the book, I will make sure to have that available to download since it makes it a lot easier for us to code! Just call the loader and it does all of the crap for us.

Note for anyone reading this:

If anyone wants a chance to win a copy of my book as it is being released. Look at my link on my blog: http://radio.javaranch.com/pascarello/2005/08/22/1124735177543.html to see how to enter. I am sure in the future I will try to hook up with the site here to hold a little contest since everyone likes free stuff.

Eric
Copy linkTweet thisAlerts:
@BigMoosieAug 25.2005 — Thanks Eric, I will enter your competition, hope I win!

Sinse you seem to be the expert I might ask another question, I like to keep my code condensed, so I use this to create the HTTP object, can you let me know if it is flawed at all?

var a;a=(a=window.XMLHttpRequest)?new a():((a=window.ActiveXObject)?new a("Microsoft.XMLHTTP"):0);
Copy linkTweet thisAlerts:
@jk_banduauthorAug 25.2005 — Thanks Eric for the solution, but I am getting the status value as 0, due to which I could not able to read the data.

-JK
Copy linkTweet thisAlerts:
@jk_banduauthorAug 25.2005 — when I directly query the xml url, I am getting the http status 200(checked with Live Http headers).
Copy linkTweet thisAlerts:
@BigMoosieAug 25.2005 — Is the xml file on the same server and at the same domain?
Copy linkTweet thisAlerts:
@A1ien51Aug 25.2005 — If you are running it on your local computer you are going to get 0 returned. You can add that into the check.

if(reqXML.status == 200 || reqXML.status == 0)

Eric
Copy linkTweet thisAlerts:
@BigMoosieAug 25.2005 — ...I see scripts that are supposed to do the same thing but many times bigger, for instance:

var h=false;
/*@cc_on
@if (@_jscript_version &gt;= 5)
try {
h = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
h = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
h = false;
}
}
@end @*/
if (!h &amp;&amp; typeof XMLHttpRequest != 'undefined') {
try {
h = new XMLHttpRequest();
} catch (e) {
h = false;
}
}


Why all the trys and chatches and funny comment hacks? Is there any point?
Copy linkTweet thisAlerts:
@A1ien51Aug 25.2005 — the /*@cc_on stuff is JScript

Some of the versions of the ActiveX uses Msxml2.XMLHTTP.

I perfer just to check for Microsoft.XMLHTTP since it is covers it all.

Eric
×

Success!

Help @jk_bandu spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...