1

Below I have a piece of PHP code that essentially rips an XML file from another site and prints it to my index.php page. I'm doing this because of access-control-allow-origin so that I can get the data using AJAX (not allowed from other domains (is this known as a proxy?)).

<?php
    header ("Content-Type:text/xml");
    $url = 'http://pathtoxmlfilehere.com/blablabla.xml';
    $xml = file_get_contents($url);
    print $xml;
?>

So, now I have this file on the same server as mine, I just need to make an AJAX call to get it, right? So, I'm using jQuery so might as well make use of $.get().

$.get('PathToLocalXmlThatIUsedPhpToDownload', function(data) {
    // What goes in here?
    // Do I need to parse the data as XML?
});

I need to put the data into an object/array (not sure which is more appropriate) so that I can manipulate/display it easily. I'm struggling to do this, if anyone can help me out I'd really appreciate it. I have read other similar questions and their solutions don't seem to work for me, hence why I've decided to post my exact situation in order to find more exact answers.

Thanks

2
  • Which of the similar questions have you read and which of their solutions have you tried? That would be good to know so that the context to these is very, very exact as you wrote you want something very exact this time. Please provide this very exact context. Commented Jul 8, 2013 at 20:46
  • From the accepted answer I must admit I fail to see how your question is exactly different to existing Q&A material. What I can say is that your question is at least properly formulated and the answer here as well, so if you could outline what exactly your issue was here, would be good to have this out-written. Commented Jul 8, 2013 at 20:54

1 Answer 1

1

You can specify an xml datatype in your get request. The data object will then be the XML root element:

$.get('PathToLocalXmlThatIUsedPhpToDownload', function(data) {
   //work with xml here
}, 'xml');

It's probably more efficient to work with the XML directly, rather than converting it to a javascript object and then working with the data. However, if it makes more sense in the context of your problem to use a javascript object, see the answer to this post: Tool (javascript) to convert a XML string to JSON

http://api.jquery.com/jQuery.get/

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer. How do I work with XML directly using JavaScript?
I'd love to help, but that's out of scope for this question because there is no concise answer. You could start by searching for javascript XML manipulation tutorials and then post questions about specific problems you're having. Good luck!
Thanks, no problem! I've just posted a more specific question and solved my problem almost immediately after posting. Thanks again 8-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.