简体   繁体   中英

Generate XML from dynamic PHP with a foreach loop

I'm trying to create an XML feed, essentially of a bunch of job listings. I have 39 job listings in the database right now, and I'm creating the XML using SimpleXML and it's working just fine, except it's only outputting the very last record from the database in the xml. I'm sure there's an easy solution.

Looking at the code, I want each job to be inside the <job> element, and I want a new <job> element to be created for each job. All of these are enclosed inside one <source> element. Here is my PHP code, and below that is the result I'm getting - you'll see there's only one row returning instead of all 39.

<?php
Header('Content-type: text/xml');
class SimpleXMLExtended extends SimpleXMLElement {
  public function addCData($cdata_text) {
    $node = dom_import_simplexml($this);
    $no   = $node->ownerDocument;
    $node->appendChild($no->createCDATASection($cdata_text));
  }
}


$jobs = $dbjobs->find(array('job_title' => array('$exists' => true), 'job_title' => array('$nin'=> array('',' ', null))));
$jobs = iterator_to_array($jobs);
$xml = new SimpleXMLExtended('<source/>');
$i = 0;
foreach ($jobs as $job) {
  $i++;
  $xml->job = NULL;
  $j = $xml->job;
  $j->referencenumber = NULL;
  $j->referencenumber->addCData($job['id']);
  $j->title = NULL;
  $j->title->addCData($job['job_title']);
  $j->url = NULL;
  $j->url->addCData('http://www.site.com/joblisting.php?jl=' . $job['id']);
  $j->description = NULL;
  $j->description->addCData($job['job_description']);
  $j->company = NULL;
  $j->company->addCData($job['company']);
  $j->city = NULL;
  $j->city->addCData($job['city']);
  $j->state = NULL;
  $j->state->addCData($job['state']);
  $j->postalcode = NULL;
  $j->postalcode->addCData('');
  $j->country = NULL;
  $j->country->addCData('US');
  $j->date = NULL;
  $j->date->addCData(date("Y-m-d", $job['added']->sec));
  $j->site = NULL;
  $j->site->addCData('site.com');
  $j->count = NULL;
  $j->count->addCData($i);
}

print($xml->asXML());
?>

And here is an example response I get:

<source>
  <job>
    <referencenumber>230257</referencenumber>
    <title>Home Phone Representative</title>
    <url>http://www.site.com/joblisting.php?jl=230257</url>
    <description></description>
    <company>Media LLC</company>
    <city>San Jose</city>
    <state>CA</state>
    <postalcode></postalcode>
    <country>US</country>
    <date>2013-09-16</date>
    <site>site.com</site>
    <count>39</count>
  </job>
</source>

As you can see it populates just fine but I need all the listings instead of just the last one in the loop. Thanks for your help in advance.

You should add Childs to the root:

foreach ($jobs as $job) {
  $i++;
  $j=$xml->addChild('job')
  ...
$xml->job = NULL;

is your wong line because you are cancelling last record.

and all $j->xxxxxx = NULL; are useless.

then code as

foreach ($jobs as $job) {
  $i++;
  $j = $xml->addChild('job');
  $j->referencenumber->addCData($job['id']);
  $j->title->addCData($job['job_title']);

 (...)

is better.

to know more about check the SimpleXMLElement::addChild doc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM