Based on what I was reading it seems like the best way to parse XML in PHP is to use the simplexml_load_string()
method. Every example that I found consisted of XML that have different child node names. In my case I have XML that is in a row/value format. I want to be able to parse each one of the rows into a custom object called 'Job'. So each row node in the XML represents a 'Job' and each data node represents a Job property. What is the best way to parse this type of XML in PHP?
Here is an example of the XML that I am trying to parse:
<?xml version="1.0" encoding="utf-8"?>
<dataset xmlns="http://developer.cognos.com/schemas/xmldata/1/" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
<!--
<dataset
xmlns="http://developer.cognos.com/schemas/xmldata/1/"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://developer.cognos.com/schemas/xmldata/1/ xmldata.xsd"
>
-->
<metadata>
<item name="RequisitionNumber" type="xs:string" length="52"/>
<item name="Title" type="xs:string" length="52"/>
<item name="City" type="xs:string" length="52"/>
<item name="State" type="xs:string" length="52"/>
<item name="PostalCode" type="xs:string" length="52"/>
<item name="Description" type="xs:string" length="52"/>
<item name="Requirements" type="xs:string" length="52"/>
<item name="ID" type="xs:string" length="52"/>
</metadata>
<data>
<row>
<value>16-1279</value>
<value>Manager</value>
<value>Portland</value>
<value>OR</value>
<value>98660</value>
<value>Manager Description goes here</value>
<value>Requirements go here</value>
<value>45</value>
</row>
<row>
<value>16-1279</value>
<value>Exec</value>
<value>Portland</value>
<value>OR</value>
<value>98660</value>
<value>Exec Description goes here</value>
<value>Exec go here</value>
<value>45</value>
</row>
</data>
</dataset>
This the path I have started down:
class Job
{
function __construct($requisitionnumber, $title, $city, $state, $postalcode, $description, $requirements)
{
$this->RequisitionNumber = $requisitionnumber;
$this->Title = $title;
$this->City = $city;
$this->State = $state;
$this->PostalCode = $postalcode;
$this->Description = $description;
$this->Requirements = $requirements;
}
}
function ParseReportResult($xml)
{
$jobs = array();
foreach($xml->row)
{
//How do I parse each data node into the below object?
$job = new Job($requisitionnumber, $title, $city, $state, $postalcode, $description, $requirements);
array_push($jobs, $job);
}
return $jobs;
}