[ create a new paste ] login | about

Link: http://codepad.org/IWygGbiT    [ raw code | output | fork ]

PHP, pasted on Oct 18:
<?php

$xml_content = <<<EOD
<?xml version="1.0" encoding="utf-8" ?>
<transaction dsxml_version="1.08">
<action>action1</action>
<action>action2</action>
</transaction>
EOD;

$xml = simplexml_load_string($xml_content);

echo "Original:\n";
echo $xml->action, "\n";    // Write "action1"
echo $xml->action[0], "\n"; // Write "action1"
echo $xml->action[1], "\n"; // Write "action2"

// create the action array
$action_array = array((string) $xml->action[0], (string) $xml->action[1]);

// remove $xml, otherwise this would be no demo
$xml = new stdclass;

$action = new SimpleXMLArrayMock($action_array);

$xml->action = $action;

echo "\nFake:\n";
echo $xml->action, "\n";    // Write "action1"
echo $xml->action[0], "\n"; // Write "action1"
echo $xml->action[1], "\n"; // Write "action2"


/**
 * Mock SimpleXML array-like behavior
 */
class SimpleXMLArrayMock extends ArrayObject
{
    private $first;
    public function __construct(array $array)
    {
        $this->first = (string) $array[0];
        parent::__construct($array);
    }
    public function __toString()
    {
        return $this->first;
    }
    
}


Output:
1
2
3
4
5
6
7
8
9
Original:
action1
action1
action2

Fake:
action1
action1
action2


Create a new paste based on this one


Comments: