First post, so please be kind. :) I've been struggling with this for days. I have three XML files with related content and have already conquered the first two. The first two were simple 1)LOTDATA.XML - Contains Data for a specific Dealer by DealerID and 2) vehicles.xml, which contains a list of inventory with child attributes for all Dealers in File#1, again, by DealerID. I iterate through the first file and output an XML for each Dealer with the file name $dealerid.xml. I then iterate through the vehicles.xml file and sequentially place each Vehicle into its corresponding Dealer File by DealerID. Here's where it gets fun; the Third File, LINKS.XML, contains data for each of the Vehicles in File#2 by both DealerID and VIN (Unique ID). What I need to do is get each Links Node appended as a child of the corresponding VIN in its related $dealerid.xml file. I've tried XML Path and all sorts of variations with no joy. The closest I have come is to iterate through all the Links and ECHO its matched VIN in the dealerid.xml file. However, when I try to go through the steps to append a child to the related record, the closest I come is that it adds EVERY Links Node that matches the current DealerID to the first Vehicle Record in the parent file. Following is the closest working code I have come up with:
// Get the DealerID of the Record, and Add Node to the corresponding DealerID.xml file
$linksinfo = 'LINKS.XML';
$links = new xmlReader();
$links->open($linksinfo);
$linksnode = 'LINKS';
while($links->read() && $links->name !== $linksnode);
set_time_limit (180);
// Move to the first vehicle
while($links->name === $linksnode) {
// let's get this Links Node into a SimpleXMLElement
$mylinks = new SimpleXMLElement($links->readOuterXML());
// Get the DealerID for Parent File Name
$localdealerid = $mylinks->DealerID;
// Identify the parent file
$parentfile = "$localdealerid.xml";
//Pull the VIN Number for reference
$localvinnumber = $mylinks->VIN;
$parentdoc = new DOMDocument();
$parentdoc->formatOutput = true;
$parentdoc->preserveWhitespace = false;
$parentdoc->load($parentfile, 0) or die ("Couldnt Open Parent Doc");
echo "Opened Dealer File";
$childdoc = dom_import_simplexml($mylinks) or die ("Error Opening Links File");
$childdoc->formatOutput = true;
$childdoc->preserveWhitespace = false;
echo "Opened Links File <br>";
echo "Local Dealer ID is $mylinks->DealerID <br>";
echo "Searching to match VIN $localvinnumber <br>";
$inventory = $parentdoc->getElementsByTagName('VIN');
foreach($inventory as $vehicle){
$vehicleid = $vehicle->nodeValue or die ("Error Iterating Inventory");
if($vehicleid = $localvinnumber ){
$root = $parentdoc->getElementsByTagName('VEHICLES')->item(0) or die ("Error Getting Matching VIN");
echo "Matched to $vehicleid <br>";
$xmlcontent = $parentdoc->importnode($childdoc,true);
$root->appendChild($xmlcontent) or die ("Error Appending Child Record");
$parentdoc->save($parentfile) or die ("Error Saving Parent File");
echo "Success! <br>";
break; // Moves Iteration to next Links Node
} else {
// do nothing
}
break;
}
// Move to next Linkset
$links->next($linksnode) ;
}
No comments:
Post a Comment