Jump to content

[SOLVED] jQuery: Parse xml?


trq

Recommended Posts

I must be missing something simple here. Ive setup a few simple files for testing, but I cant for the life of me get any output produced.

 

front.php

<html>
    <head>
        <title>ajax test</title>
        <script type="text/javascript" language="javascript" src="/media/js/jquery/jquery.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $.get('http://dev.oblivion/back.php', {}, function() {
                    $(this).find('users').each(function() {
                        $(this).find('user').each(function() {
                            var fname = $(this).find('fname').text();
                            var lname = $(this).find('lname').text();
                        });
                    });
                });
                $('#foo').html(fname + ' ' + lname);
            });
        </script>
    </head>
    <body>
        <div id="foo"></div>
    </body>
</html>

 

back.php

<?php

$xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
$xml .= "<users>\n";
$xml .= " <user>\n";
$xml .= "  <fname>thorpe</fname>\n";
$xml .= "  <lname>hepburn</lname>\n";
$xml .= " </user>\n";
$xml .= "</users>";

header("Content-Type: text/xml");
echo $xml;

?>

 

Any pointers much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/158899-solved-jquery-parse-xml/
Share on other sites

this might help a bit it's a lower level implementation of the ajax functionality. the variables fname and lname are out of the scope when you try to append it to a div.

$(document).ready(function(){
$.ajax({
	type: "POST",
	url: "xml.php",
	dataType: "xml",
	success: function(xml) {
		$(xml).find('users').each(function(){
			var lname= $(this).find('lname').text();
			var fname = $(this).find('fname').text();
			console.log(lname);
			console.log(fname);


		});
	}
});
//out of the scope will return "undefined undefined"
$('#foo').html(fname + ' ' + lname);
});

Thanks DJ. I feel like a bit of a dill. You know? I was looking at the function calls as simple loops, console.log pointed out that I was actually getting the data (with a few adjustments), so with that in place it was easy enough to spot that lname fname where out of scope by the time I tried adding them to the #foo div.

 

All fixed now, I did have to initialise data to be an empty object however because I was getting a 411 error.

 

Anyway, thanks heaps. Its not very often I do much client side stuff. I'm loving jQuery though.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.