如何使用domdocument将文本添加到xml元素?

时间:2015-01-28 02:24:02

标签: php variables domdocument

我开始使用Domdocument尝试创建XML文件。我找到了一个教程,让我可以获得创建文件的位置,但我仍然试图获得其中一个元素。

创建第一个元素,它看起来像

<request>
</request>

但我想做的是看起来像:

<request method=".......">
</request>

我似乎无法找到一种方法将文本添加到第一部分,而不会在结束部分显示。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

<?php
     $dom = new DomDocument("1.0", "ISO-8859-1");

     $RequestElem  = $dom->createElement('request');
     $domAttribute = $dom->createAttribute('method');
     $domAttribute->value = 'switchvox.callLogs.search';
     $RequestElem->appendChild($domAttribute);
     $dom->appendChild($RequestElem);

     $ParametersElem = $dom->createElement('parameters');

     $RequestElem->appendChild( $ParametersElem );

     $ParametersElem->appendChild ( $dom->createElement('start_date', '2015-01-19 00:00:00') );
     $ParametersElem->appendChild ( $dom->createElement('end_date', '2015-01-23 00:00:00') );

     $AccountIDElem = $dom->CreateElement('account_ids');
     $ParametersElem->appendChild( $AccountIDElem );

     $AccountIDElem->appendChild ( $dom->createElement('account_id', '1109') );

     $ParametersElem->appendChild ( $dom->createElement('sort_field', 'start_time') );
     $ParametersElem->appendChild ( $dom->createElement('sort_order', 'ASC') );
     $ParametersElem->appendChild ( $dom->createElement('items_per_page', '50') );
     $ParametersElem->appendChild ( $dom->createElement('page_number', '1') );

     $dom->appendChild( $RequestElem );

     $dom->formatOutput = true;

     $dom->save('request.xml');
?>
相关问题