2014-11-01 162 views
0

所以我试图使用PHP DOMDocument扩展来编写XML文档。我已经写好了所有的代码,但是出现在新XML文件中的唯一节点是XML标头。PHP DOMDocument不能按预期工作

下面是代码:

//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); 

,也会被保存到文件<?xml version="1.0"?>

我浏览过各种文档,SO问题和谷歌搜索页面,没有找到任何解释或解决问题的方法。

究竟发生了什么,我搞砸了什么导致问题?

由于提前, 〜坎

回答

1

你的save()调用之前缺少这样的:

$xml->appendChild($tag_history); 
+0

这个工作。谢谢! – Homberto 2014-11-01 03:55:40