1

I have this xml that works for Flash Encoder:

<?xml version="1.0" encoding="UTF-8"?>
<flashmedialiveencoder_profile>
    <preset>
        <name></name>
        <description></description>
    </preset>
    <capture>
        <video>
            <device></device> 
            <crossbar_input>1</crossbar_input>
            <frame_rate>25.00</frame_rate>
            <size>
                <width></width>
                <height></height>
            </size>
        </video>
        <audio>
            <device></device> 
            <crossbar_input>2</crossbar_input>
            <sample_rate></sample_rate>
            <channels>1</channels>
            <input_volume>60</input_volume>
        </audio>
    </capture>
    <encode>
        <video>
            <format>H.264</format>
            <datarate></datarate>
            <outputsize></outputsize>
            <advanced>
                <profile></profile>
                <level></level>
                <keyframe_frequency>5 Seconds</keyframe_frequency>
            </advanced>
            <autoadjust>
                <enable>false</enable>
                <maxbuffersize>1</maxbuffersize>
                <dropframes>
                <enable>false</enable>
                </dropframes>
                <degradequality>
                <enable>false</enable>
                <minvideobitrate></minvideobitrate>
                <preservepfq>false</preservepfq>
                </degradequality>
            </autoadjust>
        </video>
        <audio>
            <format>MP3</format>
            <datarate></datarate>
        </audio>
    </encode>
    <restartinterval>
        <days></days>
        <hours></hours>
        <minutes></minutes>
    </restartinterval>
    <reconnectinterval>
        <attempts></attempts>
        <interval></interval>
    </reconnectinterval>
    <output>
        <rtmp>
        <url></url>
        <backup_url></backup_url>
        <stream></stream>
        </rtmp>
    </output>
    <metadata>
        <entry>
        <key>author</key>
        <value></value>
        </entry>
        <entry>
        <key>copyright</key>
        <value></value>
        </entry>
        <entry>
        <key>description</key>
        <value></value>
        </entry>
        <entry>
        <key>keywords</key>
        <value></value>
        </entry>
        <entry>
        <key>rating</key>
        <value></value>
        </entry>
        <entry>
        <key>title</key>
        <value></value>
        </entry>
    </metadata>
    <preview>
        <video>
        <input>
            <zoom>50%</zoom>
        </input>
        <output>
            <zoom>50%</zoom>
        </output>
        </video>
        <audio></audio>
    </preview>
    <log>
        <level>100</level>
        <directory></directory>
    </log>
</flashmedialiveencoder_profile>

And I need to update some of the data depending on the type of xml the user requires. So I want to do something like this:

$data = array (
    'preset' => array (
        'name' => 'Low Bandwidth (150 Kbps) - H.264',
    ),
    'capture' => array (
        'audio'  => array (
            'sample_rate' => 44100
        )
    ),
    'encode' => array (
        'video' => array (
            'datarate' => '300;',
            'outputsize' => '480x360;',
            'advanced' => array (
                'profile' => 'Main',
                'level' => 2.1,
            )
         ),
         'audio' => array (
            'datarate' => 48
         )
    ),
);

I know this tree works, cause I can do it manually, but the problem is that I don't want to do it like that, so if in the future I need to change other think in the xml, I have only to add it to the array configuration.

How can I do it? I can write a recursive function to do it, but I don't really want to do it in that way. Is there any other way to do it? I don't know... may be something like:

$xml->updateFromArray($data);

As I repeat, I can write that function and extend SimpleXMLElement, but if there is any other native php method to do it, it will be better.

hakre
  • 193,403
  • 52
  • 435
  • 836
Cito
  • 1,659
  • 3
  • 22
  • 49
  • I don't know of a native PHP function - there are some functions for serializing, but that's probably not as specific as you look for. Also it's not clear to me which problem you have with the recursion. – hakre Feb 12 '13 at 17:43
  • I preffer allways avoid recursiveness, cause it can be harmfull if is not correctly used. Also, there are - the most of the times - other ways to accomplished the same result without recursiveness. However, I think this one is not one of those things... – Cito Feb 12 '13 at 17:53
  • I just updated the stack based approach, it works without recursion and it also shows some syntax fitting for SimpleXML - just see the notes. It now works with numbered elements as well and there is an online demo to easier play with it. You still can extend from SimpleXMLElement and add the method your own. – hakre Feb 12 '13 at 19:05
  • @hakre just to add some more insight, this is why I preffered avoid recursion: http://eval.in/164 vs http://eval.in/179 – Cito Feb 14 '13 at 20:51

2 Answers2

2

Considering $xml is the document element as a SimpleXMLElement and $data is your array (as in the question), if you are concerned about numbering children, e.g. for metadata, I have it numbered in the array this way:

'metadata' => array(
    'entry' => array(
        5 => array(
            'value' => 'Sunny Days',
        ),
    ),
),

The following shows how to solve the problem in a non-recursive manner with the help of a stack:

while (list($set, $node) = array_pop($stack)) {
    if (!is_array($set)) {
        $node[0] = $set;
        continue;
    }

    foreach ($set as $element => $value) {
        $parent = $node->$element;
        if ($parent[0] == NULL) {
            throw new Exception(sprintf("Child-Element '%s' not found.", $element));
        }
        $stack[] = array($value, $parent);
    }
}

Some notes:

  • I changed the concrete exception type to remove the dependency.
  • $parent[0] == NULL tests the element $parent is not empty (compare/see SimpleXML Type Cheatsheet).
  • As the element node is put into the stack to be retrieved later, $node[0] needs to be used to set it after it got fetched from the stack (the numbered element is already in $node (the first one by default), to change it later, the number 0 needs to be used as offset).

And the Online Demo.


The example so far does not allow to create new elements if they do not exist so far. To add adding of new elements the exception thrown for nonexisting children:

        if ($parent[0] == NULL) {
            throw new Exception(sprintf("Child-Element '%s' not found.", $element));
        }

needs to be replaced with some code that is adding new children including the first one:

        if ($parent[0] == NULL) {
            if (is_int($element)) {
                if ($element != $node->count()) {
                    throw new Exception(sprintf("Element Number out of order: %d unfitting for %d elements so far.", $element, $node->count()));
                }
                $node[] = '';
                $parent = $node->$element;
            } else {
                $parent[0] = $node->addChild($element);
            }
        }

The exception still in is for the case when a new element is added but it's number is larger than the existing number of elements plus one. e.g. you have got 4 elements and then you "add" the element with the number 6, this won't work. The value is zero-based and this normally should not be any problem.

Demo

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • This looks awsome! One question: what is the difference between the code I wrote? I'm willing to use this, but I wish to know the difference between this and the code above... – Cito Feb 12 '13 at 20:23
  • Thanks man! I've choosed this one as the correct answer since it works as I wish, without recurssion. To honest, I don't clearly understand how it works, so I'll study it :) Anyways, thanks a lot :) – Cito Feb 12 '13 at 20:31
  • I have one question. What if I want to add information to the array? For example, if I want to add another data into entry... See this example: http://eval.in/11804 – Cito Mar 05 '13 at 16:23
  • A standard array-push operation should do it: `$entry['metadata']['entry'][] = array('value' => 'New');` - PHP numbers automagically here. If in doubt, see http://php.net/arrays and inspect with `print_r`/ `var_dump`. – hakre Mar 05 '13 at 16:27
  • The problem is not that. See this example: http://eval.in/11807 I can do it as you suggest me, but I don't know how many elements will be new neither which ones, so I need to do it programmatically. If I can edit the function you gave me before will be great! (I already tried, but I was not able to do it...) – Cito Mar 05 '13 at 16:39
  • Ah true, now I see. You want to add here (not edit as in the answer). But we exploit the number for the edit. That needs some modifications, maybe this can be done quick, let me try something. – hakre Mar 05 '13 at 16:46
0

I have made this object extending SimpleXMLElement, but if there is any better way to do this, I'll really appreciate it :)

/**
 * Object to manipulate and add information to an xml object
 *
 * @package Stream
 * @subpackage SimpleXMLElement
 * @author abraham.sustaita@gmail.com
 * @author Abraham Cruz
 * @version 2 Added functionality to update data with array 
 * @example $this->rows[0] = array (); now can be edited
 */
class Stream_SimpleXMLElement extends SimpleXMLElement 
{
    /** 
     * Method to update the information of the xml
     * from an array
     * 
     * @throws Core_Exception
     * @param array $array
     * @return Stream_SimpleXMLElement
     */
    public function updateFromArray(array $array) {
        foreach ($array as $key => $data) {
            if ($this->$key->count() == 0) {
                throw new Core_Exception("The key {$key} does not exists within the XML object.");
            } else if ($this->$key->count() > 1) {
                foreach ($data as $i => $value) {
                    $tmpData = &$this->$key;
                    $tmpkey = &$tmpData [$i];
                    if (is_array($value)) {
                        foreach ($value as $key2 => $value2) {
                            if ($tmpkey->$key2 instanceof Stream_SimpleXMLElement && is_array($value2)) {
                                $tmpkey->$key2->updateFromArray($value2);
                            } else {
                                $tmpkey->$key2 = $value2;
                            }
                        }
                    } else {
                        $tmpkey = $value;
                    }
                    unset ($tmpData, $tmpkey);
                }
            } else {
                if (is_array($data)) {
                    $this->$key->updateFromArray($data);
                } else {
                    $this->$key = $data;
                }
            }
        }
        return $this;
    }
}

As the object Stream_SimpleXMLElement extends SimpleXMLElement, then every child of the object will be an instance of Stream_SimpleXMLElement, so we can use the same method...

Cito
  • 1,659
  • 3
  • 22
  • 49
  • the `updateFromArray(array $array)` method has one parameter, however, you call it with two. You might want to review that. – hakre Feb 12 '13 at 17:42
  • Yes, I deleted the seconde parameter, but not in the calls. I've updated the method – Cito Feb 12 '13 at 17:47