Let's say I have some XML like this
This is title 1
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?
Vincent
21.7k17 gold badges57 silver badges61 bronze badges
asked Jan 6, 2009 at 13:42
Mark BiekMark Biek
143k54 gold badges155 silver badges200 bronze badges
1
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.
answered Jan 6, 2009 at 14:01
Aron RotteveelAron Rotteveel
79k17 gold badges104 silver badges128 bronze badges
6
You can use the PHP function
strval[];
This function returns the string values of the parameter passed to it.
answered Feb 4, 2009 at 20:06
1
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
answered Feb 1, 2013 at 6:03
Mixed CaseMixed Case
911 silver badge3 bronze badges
3
Another ugly way to do it:
$foo = array[ $xml->channel->item->title."" ];
It works, but it's not pretty.
answered Aug 13, 2012 at 8:25
MikepoteMikepote
5,7752 gold badges32 silver badges36 bronze badges
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.
answered Sep 7, 2016 at 16:24
DJ FarDJ Far
4985 silver badges12 bronze badges
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 "\n";
print_r[$header];
echo "
";
To get a childs child then just do this:
$data = array[];
foreach[$xml->data->children[] as $child]
{
$header[$child->getName[]] = sprintf["%s", $child];
}
echo "\n";
print_r[$data];
echo "
";
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.
answered Jul 11, 2011 at 21:27
PetePete
313 bronze badges
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"
answered Sep 29, 2017 at 15:40
Eaten by a GrueEaten by a Grue
19.9k10 gold badges79 silver badges99 bronze badges
Try strval[$xml->channel->item->title]
answered Jul 4, 2017 at 15:04
Ariel RuizAriel Ruiz
1152 silver badges4 bronze badges
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[]];
answered Nov 23, 2018 at 9:54
Just put the ''. before any variable, it will convert into string.
$foo = array[ ''. $xml->channel->item->title ];
answered May 3, 2020 at 15:36
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
answered Jul 17, 2017 at 19:26
TonyTony
2,7761 gold badge23 silver badges33 bronze badges