i am working with an xml, using simplexml and xpath now when i echo a result of xpth query it echoing string but i need to get that string in a array, but when trying to copy in a array it is coming as and simplexml object, like
object(SimpleXMLElement)#237 (1) {
[0]=>
string(69) "Hallituksen esitykset uusiksi Yle-laeiksi eduskunnan käsiteltäviksi"
}
is just want "Hallituksen esitykset uusiksi Yle-laeiksi eduskunnan käsiteltäviksi" here is the code,
$xml = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" . $pageBlocks['news'];
$xmldata = simplexml_load_string($xml);
$result = $xmldata->xpath('/blocks/block/items/item/strong');
$feeddata = array();
while (list(, $node) = each($result)) {
$feeddata [] = $node[0];
}
foreach ($feeddata as $data){
var_dump($data);
}
how to solve that
1 Answer 1
To get it as a string, simply cast it via (string)
, which will internally call SimpleXMLElement's __toString()
to return a string representation. That's what's also happening implicitly when you echo
it, incidentally.
while (list(, $node) = each($result)) {
$feeddata [] = (string)$node[0];
}
-
1I love SimpleXML also for this feature.lorenzo-s– lorenzo-s2012年05月15日 14:03:43 +00:00Commented May 15, 2012 at 14:03
-
@lorenzo-s I "grew up" on DOMDocument, but have mostly switched to SimpleXML because of this ease of use.Michael Berkowski– Michael Berkowski2012年05月15日 14:05:24 +00:00Commented May 15, 2012 at 14:05