Search code examples
phpxmlxmlwriter

php to xml info inside xml tag and trasform data


I need some help with 2 subjects

First:

I need to have some info from a database into a a xml.

$xml->startElement('job');

  $xml->startElement("jobs");
  $xml->writeRaw(utf8_encode($row['post_name']));
  $xml->endElement();

  $xml->startElement("id");
  $xml->writeRaw(utf8_encode($row['ID']));
  $xml->endElement();

and this give me the result like

<job>
<jobs>consultoresformadores-para-zona-de-lisboa</jobs>
<id>2320</id>
</job>

but i need it to be shown like

<job id="2320">
<jobs>consultoresformadores-para-zona-de-lisboa</jobs>
</job>

The 2 subject is that i need to transform a date data to a specific format

i have my code like:

$xml->startElement("expire");
  $strval = unserialize(utf8_encode($row['meta_value']) );
  $xml->writeRaw($strval[validTo]);
  $xml->endElement();

And it shows results like

<expire>Mon, 30 Jun 2014</expire>

but i need it to be presented like

<expire>30.06.2014</expire>

Thanks in advanced


Solution

  • For the first question, you need to add your attribute using the writeAttribute function (http://www.php.net/manual/en/function.xmlwriter-write-attribute.php) :

    $xml->startElement('job');
    
    $xml->startElement("jobs");
    $xml->writeAttribute("id", utf8_encode($row['ID'])); 
    $xml->writeRaw(utf8_encode($row['post_name']));
    $xml->endElement();
    

    For the second question, you need to use the date and strtotime functions to format your date :

    $xml->startElement("expire");
    $strval = unserialize(utf8_encode($row['meta_value']) );
    $xml->writeRaw(date("d.m.Y", strtotime($strval[validTo])));
    $xml->endElement();