Parsing an XML Feed

I’m trying to parse an xml feed for a database insert which has been proving harder than anticipated. My first roadblock I’m assuming has something to do with not using scope correctly

I’m not getting any results back from the class that parses the xml file:

Parsing class:

<?php 
class jobFeed {

public $insertJobs;


 function __constructor($date, $xmlFile)
 {
		$doc = new DOMDocument();
		$doc->load($xmlFile);
		$jobseekers = $doc->getElementsByTagName( "jobAd" );
		
		foreach( $jobseekers as $jobseeker)
		{
		
		$property_names = $jobseeker->getElementsByTagName( "propertyName" );
		$prop_name = $property_names->item(0)->nodeValue;
		
		$prop_codes = $jobseeker->getElementsByTagName("propertyCode");
		$prop_code = $prop_codes->item(0)->nodeValue;
		
		$job_names= $jobseeker->getElementsByTagName( "requisitionTitle" );
		$job_name= $job_names->item(0)->nodeValue;
		
		$isManagements= $jobseeker->getElementsByTagName( "isManagement" );
		$isManagement= $isManagements->item(0)->nodeValue;
		
		$externalJobAdIds=$jobseeker->getElementsByTagName( "externalJobAdId" );
		$externalJobAdId = $externalJobAdIds->item(0)->nodeValue;
		
			if(isManagement == "true")
			{
				$this->insertJobs = "($prop_name,$prop_code,$job_name,$externalJobAdId),";
			}	
		}
	}
	
	function inserts() {
		return $this->insertJobs;
	}

}
?>

Main script which calls parser class and will eventually run db query:

<?php

$dbc = new mysqli('localhost','root','','jobs');
require_once('jobFeedClass.php');

$july1 = new jobFeed('7/1','jobFeeds/jobs_emp(1).xml');

$jobString = $july1->inserts();

if(!empty($jobString)){
echo $jobString;
}
else {
echo 'no jobs here';
}
?>

Hy
If you just want to read an XML document, and you know its structrure, I think is better to use PHP SimpleXML PHP: SimpleXML - Manual .

Thanks. I’ve read the simplexml class is easier to use, especially if all you want to do is reads. I do plan to use it in future projects. The DOMDoc aspect of this code was easiest for me to use in this project because I just had to copy it over from an already working script that parses the exact same files.

I think the problem here has to do with visibility because I had everything working correctly before I abstracted the parsing functions. I did this because I’ll need to parse a collection of daily files for the month of July and I didn’t want to replicate my the functionality 30 times to accommodate each of the files.

After I get this first bug worked out I’d like to figure out how to put all the files into a single array and loop my DOMDoc object through it to extract the info I need.

Right now the only way I can think to parse each of the files is most certainly a bad design decision which would be to instantiate the DOMDoc object 30 times, passing it each one of the files individually…

I realized I needed to make the $insertJobs var in the parser class an array since it was grabbing other variables inside the foreach loop.

I’m now at least getting an empty array fed back to my calling script, but for some reason the array isn’t collecting any values.

If I compile both scripts into a single script and build the array in an object oriented manner everything works. But as soon as I abstract the parsing functionality the array will no longer populate. Any help would be greatly appreciated…