Ben Hathaway
Ben Hathaway

Reputation: 356

Can't create xml correctly using DOM PHP

I'm trying to create an XML like this from a html form using php. I want to create complex element

<recipe>
<portion_size>
<portion_num>
</portion_num>
</portion_size>

<recipe_description>
<prep_time>
</prep_time
<recipe descrip>
</recipe descrip>
<recipe_description>

Something along the lines of that, however i cant seem to nest it properly using the DOM here is my php code. Does anyone have any advice?

$doc = new DOMDocument(); //open the object xml 
    $r = $doc->createElement("recipe"); 
    $doc->appendChild($r); 

    $name = $doc->createElement('name',$name);
    $r->appendChild($name); 

    $r = $doc->createElement("portion_size");
    $doc->appendChild($r);

    $child = $doc->createElement('portion_num',$portionNum);
    $r->appendchild($child);

     $prepTime = $doc->createElement('prep_time',$prepTime);
    $r->appendChild($prepTime);

     $recipeDescrip = $doc->createElement('descrip',$recipeDescrip);
    $r->appendChild($recipeDescrip);

     $utensilNum = $doc->createElement('utensil_num',$utensilNum);
    $r->appendChild($utensilNum);

     $utensilDescrip = $doc->createElement('utensil_descrip',$utensilDescrip);
    $r->appendChild($utensilDescrip);

     $quantityAmount = $doc->createElement('quantity_amount',$quantityAmount);
    $r->appendChild($quantityAmount); 

     $ingredientName = $doc->createElement('ingredient_name',$ingredientName);
    $r->appendChild($ingredientName); 

     $stepNum = $doc->createElement('step_num',$stepNum);
    $r->appendChild($stepNum); 

     $stepDetail = $doc->createElement('step_detail',$stepDetail);
    $r->appendChild($stepDetail); 

Upvotes: 1

Views: 125

Answers (1)

ThW
ThW

Reputation: 19492

You should not use the second argument of DOMDocument::createElement(), it is not part of the w3c dom api and seems broken. You need to use DOMDocument::createTextNode(). However that gets a little noisy, so I suggest you encapsulate the job into a method.

class MyDOMElement extends DOMElement {

  public function appendElement($name, array $attributes = NULL, $content = '') {
    $node = $this->ownerDocument->createElement($name);
    if (!empty($attributes)) {
      foreach ($attributes as $key => $value) {
        $node->setAttribute($key, $value);
      }
    }
    if (!empty($content)) {
      $node->appendChild($this->ownerDocument->createTextNode($content));
    }
    $this->appendChild($node);
    return $node;
  }
}

class MyDOMDocument extends DOMDocument {

  public function __construct($version = '1.0', $encoding= 'utf-8') {
    parent::__construct($version, $encoding);
    $this->registerNodeClass('DOMElement', 'MyDOMElement');
  }
}

$dom = new MyDOMDocument();
$dom->appendChild($root = $dom->createElement('recipe'));

$root->appendElement('name', NULL, 'Reciepe Example');
$root
  ->appendElement('portion_size')
  ->appendElement('portion_num', NULL, '4');
$description = $root->appendElement('recipe_description');
$description->appendElement('prep_time', array('unit' => 'minutes'), '10');
//...

echo $dom->saveXml();

Output:

<?xml version="1.0" encoding="utf-8"?>
<recipe>
  <name>Reciepe Example</name>
  <portion_size>
    <portion_num>4</portion_num>
  </portion_size>
  <recipe_description>
    <prep_time unit="minutes">10</prep_time>
  </recipe_description>
</recipe>

Upvotes: 2

Related Questions