//READ IN XML AND VALIDATE AGAINST DTD
$path_xml = "country.xml"; //puts the file country.xml into variable $path_xml
$xml_obj = new DomDocument; //creates new DomDocument, places in variable
if (!$xml_obj->load($path_xml)) //if not loaded, then print error
{print " XML $path_xml not loaded into DOM Document
";}
if($xml_obj->validate()) //conditional test on validational
{print "DOCUMENT IS VALIDATED
";} //print this if document validates
else {print "document dtd does not validate";} //otherwise, print this
print "";
print "
";
print "";
print "";
print "";
//LOOPS THROUGH FILE AND PRINTS DATA TO SCREEN
$sxe = simplexml_load_file($path_xml); //load as simpleXML using the same variable $path_xml as first initialised above
print $sxe->acountry->countryname . "
"; //source the countryname using the simplexml hierarchical strucutre
foreach($sxe->acountry->destination as $oneDest) //put destination into a variable so we can loop and access easily
{
$aDest = $oneDest->adestination; //use the variable $oneDest to access adestination, put value into variable $aDest
$aAirp = $oneDest->airport; //use the variable $oneDest to access airport, put value into $aAirp
print $aDest . "
";
print $aAirp . "
";
foreach($oneDest->activities->activity as $oneAct) //INNER LOOP , creating oneAct from $oneDest 'pointer'
{
$act = $oneAct; //loop thru the activities for that particular destination
print $act . "
"; //and print them
}
}
//WRITES XML PLUS DATA TO FILE USING APPEND TO OUTTEXT USING THE CUT DOWN XML
//ACTUALL CONVERTS PREVIOUS TAG NAMES TO NEW TAG NAMES BEING CUT DOWN XML
$outfilepointer = fopen("activityWrite.xml","w"); //initialise filepointer, and open file for write
$outtext =" \n"; //send first tag to file
$myCountry = $sxe->acountry->countryname; //place country into variable $myCountry
foreach($sxe->acountry->destination as $oneDest) //loop through destinations
{
$aDest = $oneDest->adestination; //put destination into variable $aDest
$outtext .= " \n"; //print to file the tag
$outtext .= "" . $myCountry . " \n";//print to file the open tag, the data and the close tag
$outtext .= "" . $aDest . " \n"; //print to file the open tag, data, close tag
foreach($oneDest->activities->activity as $oneAct) //loop through activities, each activity
{
$act = $oneAct; //put each activity into a variable
$outtext .= "" . $act . " \n"; //put tag, then data, then close tag to file
}
$outtext .= " \n"; //print close (wrapper) tag to file
}
$outtext .= " \n"; //print final close tag
fwrite($outfilepointer, $outtext); //write to file the outtext as appended at each line of code
fclose($outfilepointer); //close the outfilepointer
?>