Forcing a SimpleXML Object to a string, regardless of context

PhpXmlSimplexml

Php Problem Overview


Let's say I have some XML like this

<channel>
  <item>
    <title>This is title 1</title>
  </item>
</channel>

The code below does what I want in that it outputs the title as a string

$xml = simplexml_load_string($xmlstring);
echo $xml->channel->item->title;

Here's my problem. The code below doesn't treat the title as a string in that context so I end up with a SimpleXML object in the array instead of a string.

$foo = array( $xml->channel->item->title );

I've been working around it like this

$foo = array( sprintf("%s",$xml->channel->item->title) );

but that seems ugly.

What's the best way to force a SimpleXML object to a string, regardless of context?

Php Solutions


Solution 1 - Php

Typecast the SimpleXMLObject to a string:

$foo = array( (string) $xml->channel->item->title );

The above code internally calls __toString() on the SimpleXMLObject. This method is not publicly available, as it interferes with the mapping scheme of the SimpleXMLObject, but it can still be invoked in the above manner.

Solution 2 - Php

You can use the PHP function

strval();

This function returns the string values of the parameter passed to it.

Solution 3 - Php

There is native SimpleXML method SimpleXMLElement::asXML Depending on parameter it writes SimpleXMLElement to xml 1.0 file or just to a string:

$xml = new SimpleXMLElement($string);
$validfilename = '/temp/mylist.xml';
$xml->asXML($validfilename);    // to a file
echo $xml->asXML();             // to a string

Solution 4 - Php

Another ugly way to do it:

$foo = array( $xml->channel->item->title."" );

It works, but it's not pretty.

Solution 5 - Php

The accepted answer actually returns an array containing a string, which isn't exactly what OP requested (a string). To expand on that answer, use:

$foo = [ (string) $xml->channel->item->title ][0];

Which returns the single element of the array, a string.

Solution 6 - Php

To get XML data into a php array you do this:

// this gets all the outer levels into an associative php array
$header = array();
foreach($xml->children() as $child)
{
  $header[$child->getName()] = sprintf("%s", $child); 
}
echo "<pre>\n";
print_r($header);
echo "</pre>";

To get a childs child then just do this:

$data = array();
foreach($xml->data->children() as $child)
{
  $header[$child->getName()] = sprintf("%s", $child); 
}
echo "<pre>\n";
print_r($data);
echo "</pre>";

You can expand $xml-> through each level until you get what you want You can also put all the nodes into one array without the levels or just about any other way you want it.

Solution 7 - Php

Not sure if they changed the visibility of the __toString() method since the accepted answer was written but at this time it works fine for me:

var_dump($xml->channel->item->title->__toString());

OUTPUT:

string(15) "This is title 1"

Solution 8 - Php

Try strval($xml->channel->item->title)

Solution 9 - Php

There is native SimpleXML method SimpleXMLElement::asXML Depending on parameter it writes SimpleXMLElement to xml 1.0 file, Yes

$get_file= read file from path;
$itrate1=$get_file->node;
$html  = $itrate1->richcontent->html;


echo  $itrate1->richcontent->html->body->asXML();
 print_r((string) $itrate1->richcontent->html->body->asXML());

Solution 10 - Php

Just put the ''. before any variable, it will convert into string.

$foo = array( ''. $xml->channel->item->title );

Solution 11 - Php

The following is a recursive function that will typecast all single-child elements to a String:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FUNCTION - CLEAN SIMPLE XML OBJECT
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function cleanSimpleXML($xmlObject = ''){

	// LOOP CHILDREN
	foreach ($xmlObject->children() as $child) {

		// IF CONTAINS MULTIPLE CHILDREN
		if(count($child->children()) > 1 ){

			// RECURSE
			$child = cleanSimpleXML($child);

		}else{
			
			// CAST
			$child = (string)$child;

		}
	
	}

	// RETURN CLEAN OBJECT
	return $xmlObject;

} // END FUNCTION

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark BiekView Question on Stackoverflow
Solution 1 - PhpAron RotteveelView Answer on Stackoverflow
Solution 2 - PhpZajeView Answer on Stackoverflow
Solution 3 - PhpMixed CaseView Answer on Stackoverflow
Solution 4 - PhpMikepoteView Answer on Stackoverflow
Solution 5 - PhpDJ FarView Answer on Stackoverflow
Solution 6 - PhpPeteView Answer on Stackoverflow
Solution 7 - PhpEaten by a GrueView Answer on Stackoverflow
Solution 8 - PhpAriel RuizView Answer on Stackoverflow
Solution 9 - PhpBAVA SHIEK BAREETHView Answer on Stackoverflow
Solution 10 - PhpKrishan KumarView Answer on Stackoverflow
Solution 11 - PhpTonyView Answer on Stackoverflow