-
July 13th, 2009, 10:30 AM
#1
Retrieve and parse XML
Hi all,
I have a publicly XML file ( http://meemi.it/api/JustB/followers ), and I'd like to retrieve and parse on my own web page, using AJAX. It is possible to do this?
-
July 13th, 2009, 01:33 PM
#2
Re: Retrieve and parse XML
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
July 13th, 2009, 01:38 PM
#3
Re: Retrieve and parse XML
Yes its possible. Check out this link for details.
http://www.devarticles.com/c/a/JavaS...cript-and-XML/
I'm not sure what you are able to do but if you have access to .NET C#/VB or PHP you can easily create a function that converts your XML document into a JSON string which is incredibly easy to use as it can be converted into a Javascript object by using the eval( string ) function.
I also recommend using one of the available JavaScript libraries to help with the cross browser complications that are presented when using Javascript and XML. My personal favorite is JQuery, but prototype is easy to use as well and there are many others out there.
-
July 13th, 2009, 01:46 PM
#4
Re: Retrieve and parse XML
Thanks for the hint, PeejAvery, I know how to write the code. It works if I have the file locally, but i can't make it go for that url. I'll post the JS code:
Code:
/*
* Questa funzione crea un oggetto XMLHttpRequest,
* a seconda del tipo di browser dell'utente
*
*/
function createXMLHttpRequest(){
var request = false;
//Se nel browser esiste XMLHttpRequest
if (window.XMLHttpRequest){
if (typeof XMLHttpRequest != 'undefined'){
try{
//Assegna un nuovo oggetto a request
request = new XMLHttpRequest();
alert("oggetto creato");
} catch(e){
request = false;
}
}
//Altrimenti, se esiste ActiveXObject
} else if ( window.ActiveXObject ){
try{
request = new ActiveXObject ( 'Msxml2.XMLHTTP' );
} catch(e){
try{
request = new ActiveXObject ( 'Microsoft.XMLHTTP' );
} catch(e){
request = false;
}
}
}
return request;
}
/**
* Questa funzione prende in ingresso l'oggetto /p_request/ e passa /p_data/
* a /p_URL/ con il metodo /p_method/. L'oggetto /p_request/ chiama /p_func/
* a /onreadystatechange/
*
* @param {Object} p_request Oggetto XMLHttpRequest
* @param {String} p_URL URL a cui passare data
* @param {String} p_data i dati da passare
*
* @param {String} p_func La stringa contenente il nome della funzione.
* @param {String} p_method GET o POST
*/
function requestData(p_request, p_URL, p_data,p_func, p_method) {
//L'oggetti XMLHttpRequest esiste?
if (p_request) {
alert("requestData chiamata")
p_request.open(p_method, p_URL, true);
p_request.onreadystatechange = p_func;
p_request.send(p_data);
}
}
/*
* Questa funzione effettua il parsing della risposta
* ottenuta dal server. In questo caso dovrebbe creare
* una lista degli utenti che seguono JustB
*/
function parseResponse( ) {
if (request.readyState == 4 ) {
if(request.status == 200)
var response = request.responseXML;
var paramList = response.getElementsByTagName("user");
var out = "<ul>";
for (i=0; i < paramList.length; i++)
out += '<li>' + paramList[i].firstChild.nodeValue +'</li>';
out += '</ul>';
document.getElementById('list').innerHTML = out;
} else {
alert('C\'รจ stato un problema processando i dati: \n' +
request.statusText);
request = null;
}
}
var request = createXMLHttpRequest();
var url = "http://meemi.it/api/JustB/followers";
var data = null;
var method = "GET";
window.onload = requestData(request, url, data, parseResponse, method)
I was wondering if it was a problem of cross site scripting and if it was possible to solve it.
-
July 13th, 2009, 02:09 PM
#5
Re: Retrieve and parse XML
I was wondering if it was a problem of cross site scripting and if it was possible to solve it.
This would defiantly cause a problem. Can be fixed by making an AJAX call to a page on your website which retrieves the said XML file via C# or some other server side code then returns the contents of that XML file for the AJAX request.
If using server side code is an option let me know and i can give you an example of what I'm talking about. As far as using only javascript i don't believe this is possible.
-
July 13th, 2009, 02:45 PM
#6
Re: Retrieve and parse XML
Thanks for the help, monalin, but it's actually a problem, because I'd like to create a Firefox extension, so I have to use JS. I can't understand well the reasons behind this non working: if I would like to parse, for example, " http://twitter.com/help/test.xml " with JS, can't I do it??
-
July 13th, 2009, 03:04 PM
#7
Re: Retrieve and parse XML
Plain and simple...You cannot violate cross domain access using JavaScript. If you were accessing something on your own domain using relative paths, there would be no problem.
Your best option would be to use a server-side language to read the contents from the other domain.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
July 13th, 2009, 04:01 PM
#8
Re: Retrieve and parse XML
Originally Posted by jubstuff
Thanks for the help, monalin, but it's actually a problem, because I'd like to create a Firefox extension, so I have to use JS. I can't understand well the reasons behind this non working: if I would like to parse, for example, " T " with JS, can't I do it??
I believe if you load the JavaScript from that domain also its possible to access the XML document. But unless you have access to that domain it would be difficult.
If you have your own domain however you can use a combination of the two methods to get the desired results. For example you could use an iframe to load http://mysite.com/display.aspx which contains the javascript you need.
The display.aspx not only loads the JavaScript also makes an AJAX request to http://mysite.com/render.aspx which will use server side code to read the desired XML file and allow you to parse through it in the javascript.
Just and idea, but it just might work, let me know what you think.
-
July 31st, 2010, 12:56 PM
#9
Re: Retrieve and parse XML
Yes it is possible to parse xml in javascript.
What you can do send the xml response for ajax call made. After that you can XML parsing in Javascript. You can learn XML parsing at http://www.hiteshagrawal.com/javascr...-in-javascript
Thanks,
Hitesh Agarwal
-
August 2nd, 2010, 10:10 AM
#10
Re: Retrieve and parse XML
Welcome to the forums, cyberhitesh. Please remember to keep your posts relevant. Both of your replies are from year old threads.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|