I've spent the last two nights searching here for a solution but I haven't been able to figure this out. I have multiple XML files and I'd like to loop through all of them for nodes that contain the same attributes and merge all of their nodes to an output file. Here's an example:
File1.xml
<game rotnum="241">
<gamedate> 123 </gamedate>
<team1> Giants </team1>
<team2> Eagles </team2>
</game>
File2.xml
<game rotnum="241">
<line> 4 </line>
<points> 200 </points>
</game>
Merged.xml
<game rotnum="241">
<gamedate> 123 </gamedate>
<team1> Giants </team1>
<team2> Eagles </team2>
<line> 4 </line>
<points> 200 </points>
</game>
More or less. I need to do this in PHP and I'm thinking that using the DOM will be easier than XSLT (I don't know much about this).
There's a lot of nodes throughout multiple files and I need to match up the data based on similar rotnum attributes.
I don't know if I necessarily need to do this, but it's the easiest way that I can think of to get all of my data into one simpleXML object that I can foreach through and generate separate tables for each game.
These XML files are from several API feeds that I created using via SimpleXML and cached them locally..
function stripAndSaveFile($xml) {
$game = $xml->game;
$output = new SimpleXMLElement("<justbetlinesfeed></justbetlinesfeed>");
for ($i = 0; $i < 16; $i++) {
//Get the date of each game.
$spreadpoints = $game[$i]->line->spread->attributes()->points;
$spreadteam1 = $game[$i]->line->spread->attributes()->team1adj;
$spreadteam2 = $game[$i]->line->spread->attributes()->team2adj;
$rotnum = $game[$i]->attributes()->team1rotnum;
$insert = $output->addChild("game");
$insert->addAttribute("rotnum", "$rotnum");
$insert->addChild("spreadpoints", "$spreadpoints");
$insert->addChild("spreadteam1", "$spreadteam1");
$insert->addChild("spreadteam2", "$spreadteam2");
}
file_put_contents($this->filePath, $output->asXML());
}
}