# Using PHP to parse the iTunes XML



## Fahrvergnuugen (Dec 16, 2002)

I want to create a search engine for my mp3 collection. In addition to just searching, I'm going to make it identify duplicates and fix screwed up ID3 tags.

I've looked at the PHP XML functions but I'm slightly confused as to how they work. Is anyone here familiar with them?


----------



## slur (Dec 16, 2002)

Yes, I'm quite familiar with those functions. For one of my projects had to parse XML orders from Yahoo and stuff everything into a MySQL database. The PHP XML parsing functions were a big help.

Here's the basic setup I wrote for my parser:

	$p = xml_parser_create();
	xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
	xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
	xml_set_element_handler($p, 'xmlStartHandler', 'xmlEndHandler');
	xml_set_character_data_handler($p, 'xmlCharHandler');
	xml_parse($p, $all_the_xml);
	xml_parser_free($p);


My handler functions are defined with these interfaces:

// the opening tag
function xmlStartHandler($parser, $tagname, $attribs)

// the closing tag
function xmlEndHandler($parser, $tagname)

// the data between
function xmlCharHandler($parser, $string)


The body of each of these functions is too complex to go into detail here, but if you just have them print out the arguments they receive you'll see how they are called, and in what order, with respect to your XML data. I'm using the default parsing behavior, which is probably what you'll use, but I believe you can tell the parser to do a depth-first traversal if you prefer.


----------



## Fahrvergnuugen (Dec 16, 2002)

so did you just create $all_the_xml using something like this:


```
$file = 'test.xml';
$fh = fopen($file, "r");
$all_the_xml = fread($fh, filesize($file));
```

I'm using that and it's working...I just have to figure out a fancy set of functions that will stuff it into a mysql table. I don't think searching through the xml file will be fast enough...


----------



## Fahrvergnuugen (Dec 16, 2002)

why this was moved to the web works forum is way beyond me  

just because it's php doesn't mean it has ANYTHING to do with web site design.


----------

