Homberto
Homberto

Reputation: 165

PHP DOMDocument Not Working As Expected

So I'm trying to use the PHP DOMDocument extension to write a XML document. I have all my code written up, but the only node that appears in the new XML file is the XML header.

Here is the code:

//vars
$history_location="w1/history_files/" . md5($time) . md5(rand(0,1000)) . rand(0,1000) . '.xml';
$id=2;
$airline="Aero Test Ltd.";
$aircraft="Boeing 747-400";
$engine="Rolls-Royce RB211-524H2-T";
$f=5;
$c=25;
$yp=40;
$y=560;
//xml history file
$xml=new DOMDocument();
$xml->formatOutput=true;
//tags
$tag_history=$xml->createElement("history");
$tag_preface=$xml->createElement("preface");
$tag_l1=$xml->createElement("l1", "This is the history file for aircraft " . $id . ".");
$tag_l2=$xml->createElement("l2", "Generated: " . date("F d Y H:i:s", $time) . ".");
$tag_l3=$xml->createElement("l3", "Last Updated: " . date("F d Y H:i:s", $time) . ".");
$tag_body=$xml->createElement("body");
$tag_item=$xml->createElement("item");
$tag_airline=$xml->createElement("airline", $airline);
$tag_aircraft=$xml->createElement("aircraft", $aircraft);
$tag_engine=$xml->createElement("engine", $engine2);
$tag_config=$xml->createElement("config", $f . $c . $yp . $y);
//attr
$attr_name=$xml->createAttribute("name");
$attr_name->value="purchase";
$attr_date=$xml->createAttribute("date");
$attr_date->value=date("F d Y H:i:s", $time);
//sort
$tag_history->appendChild($tag_preface);
$tag_preface->appendChild($tag_l1);
$tag_preface->appendChild($tag_l2);            
$tag_preface->appendChild($tag_l3);
$tag_history->appendChild($tag_body);
$tag_body->appendChild($tag_item);
$tag_item->appendChild($attr_name);
$tag_item->appendChild($attr_date);
$tag_item->appendChild($tag_airline);
$tag_item->appendChild($tag_aircraft);
$tag_item->appendChild($tag_engine);
$tag_item->appendChild($tag_config);
//save
$xml->save($history_location);

And all that is saved to the file is <?xml version="1.0"?>.

I've looked through all sorts of documentation, SO questions, and pages of Google searches and have found nothing that explains or solves the problem.

What exactly is going on and what did I screw up to cause the problem?

Thanks in advance, ~Hom

Upvotes: 1

Views: 431

Answers (1)

Vinicius Braz Pinto
Vinicius Braz Pinto

Reputation: 8289

You're missing this before the save() call:

$xml->appendChild($tag_history);

Upvotes: 2

Related Questions