2014-09-02 128 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