无法从PHP生成正确的XML

时间:2014-09-02 09:31:14

标签: php xml domdocument

我已经编写了这段代码来生成这个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的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();
}

任何建议/帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

你需要像这样替换第11行

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

并且所有以下行都相同,它们会抛出相同的错误。 您应该将childs添加到root xml元素。