Forum Moderators: coopster

Message Too Old, No Replies

str replace error with XML

getting Object id #1

         

mlschutz

2:36 pm on Aug 31, 2009 (gmt 0)

10+ Year Member



Basically, I construct an xml string from an array and then before I send it off I need to append a method parameter to the first xml element such as from <element> to <element method='dothis'> while leaving all of the other xml the same. I've tried creating my xml string and then doing a string replace:

$xml = str_replace("<element>", "<element method='dothis'>", $xml);

This returns Object id #1

Could anybody tell me why and what a possible fix/workaround could be?

-Mike

mlschutz

2:39 pm on Aug 31, 2009 (gmt 0)

10+ Year Member



forgot to mention I'm using a class to create this xml. a message that I also get is:

Object of class array2xml to string conversion

eelixduppy

2:41 pm on Aug 31, 2009 (gmt 0)



$xml is an object not a string. You must pass str_replace the XML string for it to work. In the class "array2xml" figure out how to grab the XML string.

mlschutz

3:01 pm on Aug 31, 2009 (gmt 0)

10+ Year Member



having a hard time. not good with OO or xml. I usually just use straight up functions. Maybe someone could help. Here's the class:

class array2xml extends DomDocument
{

public $nodeName;

private $xpath;

private $root;

private $node_name;

/**
* Constructor, duh
*
* Set up the DOM environment
*
* @param string $root The name of the root node
* @param string $nod_name The name numeric keys are called
*
*/
public function __construct($root='root', $node_name='node')
{
parent::__construct();

/*** set the encoding ***/
$this->encoding = "ISO-8859-1";

/*** format the output ***/
$this->formatOutput = true;

/*** set the node names ***/
$this->node_name = $node_name;

/*** create the root element ***/
$this->root = $this->appendChild($this->createElement( $root ));

$this->xpath = new DomXPath($this);
}

/*
* creates the XML representation of the array
*
* @access public
* @param array $arr The array to convert
* @aparam string $node The name given to child nodes when recursing
*
*/
public function createNode( $arr, $node = null)
{
if (is_null($node))
{
$node = $this->root;
}
foreach($arr as $element => $value)
{
$element = is_numeric( $element ) ? $this->node_name : $element;

$child = $this->createElement($element, (is_array($value) ? null : $value));
$node->appendChild($child);

if (is_array($value))
{
self::createNode($value, $child);
}
}
}
/*
* Return the generated XML as a string
*
* @access public
* @return string
*
*/
public function __toString()
{
return $this->saveXML();
}

/*
* array2xml::query() - perform an XPath query on the XML representation of the array
* @param str $query - query to perform
* @return mixed
*/
public function query($query)
{
return $this->xpath->evaluate($query);
}

} // end of class

mlschutz

3:51 pm on Aug 31, 2009 (gmt 0)

10+ Year Member



I am using PHP version 5.1.6 and the documentation says "...before PHP 5.2.0 the __toString method was only called when it was directly combined with echo() or print()."

Any recommendations on how I could accomplish my object to string conversion using this class with my current setup?