Wilmer
Wilmer

Reputation: 11

How can I add text to an xml element with domdocument?

I started working with Domdocument in trying to create an XML file. I found a tutorial that got me to where I can get the file created, but I'm stuck in trying to get one of the elements created.

The first element is created and it looks like

<request>
</request>

But what I'd like to do is have look like:

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

I can't seem to find a way to add the text to the first part without it showing up in the closing part. Any help would be appreciated.

Upvotes: 0

Views: 145

Answers (1)

Wilmer
Wilmer

Reputation: 11

<?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');
?>

Upvotes: 1

Related Questions