0

I'm trying to make a page where a user may enter a url into an input box, click submit, and the script will return the WebCite page. (WebCite is a url caching service. For example, if I "archive" www.google.com, the archive page is www.webcitation.org/65YgIgei6.) So WebCite has a query syntax that when given a url to cache, an email, and the parameter &returnxml=true, it will return an xml file. (For example, http://www.webcitation.org/archive?url=http://www.google.com&email=testtt@test.com&returnxml=true leads to an xml file where the text between the <webcite_url> tags is the archive page.)

So I would like some Javscript (or jquery) that will search the xml file for "<webcite_url>" and "</webcite_url>" and return the url within those tags. http://jsfiddle.net/gxHWk/ is the basic idea.

btw, I read stackoverflow.com/questions/6648454/search-and-output-data-from-an-xml-file-using-javascript, but I can't figure out how to adapt the code there to my circumstances.

(*removed "http://" from some links because of spam filter)

Thanks!

3 Answers3

0

That's what jQuery is built to do! jsFiddle

$(XMLstring).find('webcite_url').text();
Sinetheta
  • 9,189
  • 5
  • 31
  • 52
  • wow that's great! But one thing: you copied the text of the xml file and just set it as a variable. My question is: how can I have code that does that automatically? in other words, how can I make the code access http://www.webcitation.org/archive?url=http://www.google.com&email=testtt@test.com&returnxml=true and set a variable to the content of that file? – Anthony Newman Feb 19 '12 at 03:10
  • oh and since I'm querying a different domain (webcite.org), I can't use jquery.get() ajax method as shown at http://think2loud.com/224-reading-xml-with-jquery/ – Anthony Newman Feb 19 '12 at 03:28
  • your question was "how to parse xml" not "how to perform cross domain ajax". Since that service doesn't give JSON, the simple answer is "you can't". The long answer is that you'll have to setup a proxy on your server to do the requests. See: http://jquery-howto.blogspot.com/2009/04/cross-domain-ajax-querying-with-jquery.html – Sinetheta Feb 19 '12 at 03:36
  • ok sorry about that. There was a method provided in [my first question on SO](http://stackoverflow.com/questions/9147363/extracting-page-title-using-javascript) that allows one to perform cross domain ajax without creating a proxy (maybe I should have brought that up earlier). See http://jsfiddle.net/zXVcy/. That script searched an html document for the content of the `` tags. Is it possible to use that same method for XML files? I tried that [here](http://jsfiddle.net/TAEsY/1/) but it didn't work. – Anthony Newman Feb 19 '12 at 04:43
0

a solution without jQuery:

  1. load xml with iframe
  2. go through dom
var iframe = document.createElement("iframe");
iframe.src = "http://www.webcitation.org/archive?url=http://www.google.com&email=testtt@test.com&returnxml=true";
document.body.appendChild(iframe);
iframe.onload = function() {
    var url = this.contentDocument.getElementsByTagName("webcite_url")[0].firstChild.nodeValue;

    // do whatever you want with the url over here
}

But be aware: This is just a general pointer. There might be Browser incompatibilities here.

umjasnik
  • 36
  • 3
  • ok I tried this, but got an error ("Error: Permission denied for to get property XMLDocument.getElementsByTagName"). See http://jsfiddle.net/Ku9Tc/5/. – Anthony Newman Feb 19 '12 at 04:39
  • it's the same origin policy, you simply can't access the content from an other domain. So use a proxy that redirects the request on the server or try something like http://easyxdm.net/wp/ – umjasnik Feb 19 '12 at 05:33
0

jQuery is built to parse xml files. The problem for you is how to parse data from another domain. JSONP is the answer for you. You append ?callback=?" to the end of your request and put jsonp for your data options.

$.ajax({
  url: "http://www.webcitation.org/archive?url=http://www.google.com&email=testtt@test.com&returnxml=true?callback=?",
  dataType:'jsonp',
  success: function (XMLstring) {
    $(XMLstring).find('webcite_url').text();
  }
});
steveyang
  • 9,178
  • 8
  • 54
  • 80
  • hey this looks closer to what I'm looking for. however, I tried this [here](http://jsfiddle.net/A2S2S/1/), and got an error: `unterminated regular expression literal...` This looks like a minor thing to fix, but I myself am not sure how to fix it – Anthony Newman Feb 19 '12 at 18:21
  • Hi @AnthonyNewman. I think it might be a different problem than "parsing XML" so I suggest you open an new question for it. :) – steveyang Feb 20 '12 at 04:00
  • ok thanks, I'll do that! I think you've put me on the right track. – Anthony Newman Feb 20 '12 at 04:09
  • Pleasure, you could also tick my answer and let people have similar problem to find the solutions more easily. Happy coding! – steveyang Feb 20 '12 at 04:16
  • new question [here](http://stackoverflow.com/questions/9356109/unterminated-regular-expression-literal) @steven - I've ticked your answer. can you let me know if it worked? (I'm new here) – Anthony Newman Feb 20 '12 at 04:16
  • @steven.yang: Wrong. JSONP is not magic; you cannot use it to request arbitrary URLs. – SLaks Feb 20 '12 at 04:19
  • @SLaks Is there any other way to retrive XML data from a foreign domain? – steveyang Feb 20 '12 at 05:32
  • yeah we were discussing [here](http://stackoverflow.com/questions/9356109/unterminated-regular-expression-literal). still hoping there's a solution – Anthony Newman Feb 20 '12 at 05:34
  • @steven.yang: No; you cannot circumvent the same-origin policy. – SLaks Feb 20 '12 at 05:48