Can I point you in the right direction without actually writing the code?
There's more than one way to do this.
The first way is with SAX, which means something like Simple API for XML. This register's "call-back functions" for the events that occur when SAX finds the beginnig of a tag, some charater data between tags, and closing tags.
Really quickly, you'll create functions like this, and have then do pattern-matching for the data you want:
function handle_open_element($parser, $element, $attributes) {
switch($element) {
case 'whatYouWant' :
$element_name = $element;
break;
.
.
.
...same kind of thing for handle_close_element (not much to do here) and handle_character_data (this is where the teamID is found).
You have to create a parser and register the call-back (event) handlers:
$parser = xml_parser_create();
xml_set_element_handler ($parser, 'handle_open_element', 'handle_close_element');
xml_set_charater_handler($parser, 'handle_character_data');
Then read the file and parse it:
$file = 'team.xml';
$filePointer = @fopen ($file, 'r') or die("Oops");
while($contents = fread($filePointer, filesize($file))) {
xml_parse($filePointer, $contents, feof($filePointer));
}
The trick is deciding what to do with each element name and corresponding charater data...stuff them in an associative array, I guess.
This way is fast and "easy".
If you can afford the processing hit, make a DOM document and XPATH.
Rather than scanning the file once, so that you have once chance to get everything you need done, placing a DOM tree in memory allows you to "go back to the well" as much as you want, whenever you want.
Do this:
if (!$doc = xmldocfile('response2_tmp.xml')) {
die("Error in XML document");
}
$root = $doc->root();
$children = get_children($root);
$elementCount = 1;
$xpath = $doc->xpath_new_context();
$question_name_xpath = $xpath->xpath_eval("//TEAMS/TEAMID");
$team_nodeset = $team_xpath->nodeset;
//create an array containing the contents of the team nodes
for ($x=0; $x<sizeof($team_nodeset); $x++) {
$team_children = $team_nodeset[$x]->children();
$teams[] = $team_children[0]->content;
}
oh, yeah...you need something like this:
/ function to return an array of children, given a parent node
function get_children($node) {
$temp = $node->children();
$collection = array();
for ($x=0; $x<sizeof($temp); $x++) {
// filter out all nodes except elements
if ($temp[$x]->type == XML_ELEMENT_NODE) {
$collection[] = $temp[$x];
}
}
return $collection;
}
...or something like that...once all the teamIDs are in this nice array, you get them out by key or index.
there's a site where you can test this XPath:
http://www.xmlme.com/XpathTool.aspx
paste your XML in the text area and your XPath expression in the text field. See if it works. I'd do it, but my daughter is calling me.
Anyway, look for PHP code snippets that use DOM and XPath and you've got a good solution.