2014-09-02 188 views
-1

我已經編寫了此代碼來生成此XML結構,但出於某種原因,它似乎並沒有工作。 它引發錯誤: - Undefined variable: operationtype in C:\wamp\www\sms-gateway\dev\testxml.php on line 11Call to a member function appendChild() on a non-object in C:\wamp\www\sms-gateway\dev\testxml.php on line 11無法從PHP生成正確的XML

我使用php的DOMDocument庫,我不知道,沒有任何經驗。 這就是我需要生成: - enter image description here

這裏是我的代碼: -

function gen_xml($number,$message,$smssender) 
{ 
    /* create a dom document with encoding utf8 */ 
    $domtree = new DOMDocument('1.0', 'UTF-8'); 

    /* create the root element of the xml tree */ 
    $xmlRoot = $domtree->createElement("sms-teknik"); 
    /* append it to the document created */ 
    $xmlRoot = $domtree->appendChild($xmlRoot); 


    $operationtype->appendChild($domtree->createElement('operationtype','0')); 
    $flash->appendChild($domtree->createElement('flash','0')); 
    $multisms->appendChild($domtree->createElement('multisms','0')); 
    $maxmultisms->appendChild($domtree->createElement('maxmultisms','0')); 
    $compresstext->appendChild($domtree->createElement('compresstext','0')); 
    $smssender->appendChild($domtree->createElement('smssender',$smssender)); 
    $deliverystatustype->appendChild($domtree->createElement('deliverystatustype','0')); 
    $usereplynumber->appendChild($domtree->createElement('usereplynumber','0')); 
    $usereplyforwardtype->appendChild($domtree->createElement('usereplyforwardtype','0')); 
    $usee164->appendChild($domtree->createElement('usee164','0')); 


    $item=$domtree->createElement("items"); 
    $item = $domtree->appendChild($item); 

    $recipient = $domtree->createElement("recipient"); 
    $recipient = $domtree->appendChild($recipient); 

    $orgaddress->appendChild($domtree->createElement('orgaddress',$number)); 
    echo $domtree->saveXML(); 
} 

任何建議/幫助將不勝感激。

+0

那麼錯誤信息很清楚 – Steve 2014-09-02 09:45:08

回答

0

需要更換線11這樣

$xmlRoot-> appendChild($domtree->createElement('operationtype','0')); 

和所有下面的兩行相同,他們將拋出同樣的錯誤。 您應該將孩子添加到root xml元素。

+0

你也可以嘗試[SimpleXMLElement](http://php.net/manual/en/class.simplexmlelement.php)其真正簡單的像它的名字:) – 2014-09-02 09:41:57