Why So Scared

A blog about; Programming, Music and Random Stuff

Parse MusicBrainz XML with jQuery

For a project I’m working on to help people make posts over at http://www.warez-dnb.com/ i’ve been looking into parsing MusicBrainz, I thought id make a quick post to discuss the challenges and solutions I’ve come up with so far.

<html>
 <head>
 <script type="text/javascript" src="jquery.js"></script>
 <script type="text/javascript">
 $(document).ready(function(){
	$.ajax({
		type: "GET",
		url: "http://www.warez-dnb.com/test/getid.php?name=Caspa",
		dataType: "xml",
		success: function(xml) {
			$(xml).find('artist-list').each(function() {

				alert($(this).find("name").text());
			});
		}
	});
 });
 </script>
 </head>
 <body>

 </body>
</html>



This is the index.html file and jQuery code, its pretty simple, I was just testing that I could get a valid connection, then parsing the XML within artist-list and outputting anything that has the XML tag name

One question that might arise from looking at this code is why the url is hosted at warez-dnb, jQuery wont let you import XML from a remote web host, it has to be from the local server, i’ve written an extremely simple PHP function that will query musicbrainz and copy the result it gets to host it locally.

<?php
// Set your return content type
header('Content-type: application/xml');

// Website url to open
$daurl = 'http://musicbrainz.org/ws/1/artist/?type=xml&name=' . $_GET["name"];

// Get that website's content
$handle = fopen($daurl, "r");

// If there is something, read and return
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        echo $buffer;
    }
    fclose($handle);
}
?>
posted by Juo in Programming, jQuery, tidbit and have No Comments

Place your comment

Please fill your data and comment below.
Name
Email
Website
Your comment