downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Introduction> <SDO_DAS_XML::saveString
Last updated: Fri, 30 Oct 2009

view this page in

SimpleXML



Introduction> <SDO_DAS_XML::saveString
Last updated: Fri, 30 Oct 2009
 
add a note add a note User Contributed Notes
SimpleXML
antoine dot rabanes at gmail dot com
16-Oct-2009 02:22
if for some reasons you need the string value instead of the simpleXML Object you can cast the return value as a string.

exemple:

<?php
$all_api_call
= simplexml_load_file($url);
$all_api = array();
$all_api = $all_api_call->result;
$list_all_api_name = array();
$i = 0;
foreach (
$all_api->children() as $funcky_function)
{
   
$string_tmp = (string )$funcky_function->function;
   
$list_all_api_name[$i++] = $putain;
}
?>

...
spcmky at gmail dot com
14-Oct-2009 05:49
There are issues with most examples of converting a simplexml object into an array recursively.  The situation that fails is when you have a value for an element and attributes.  For example:

<results found="yes">
  <warehouse>
    <name>WH3</name>
    <phone>2-1121</phone>
  </warehouse>
  <items count="3">
    <item id="E1120" bin="AA21">Desk</item>
    <item id="E1121" bin="FG03">Chair</item>
    <item id="E1122" bin="CD00">Table</item>
  </items>
</results>

If you :

<?php
$obj
= simplexml_load_string($xml);
print_r($obj);
?>

You will get:

(
    [@attributes] => Array
        (
            [found] => yes
        )

    [warehouse] => SimpleXMLElement Object
        (
            [name] => WH3
            [phone] => 2-1121
        )

    [items] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [count] => 3
                )

            [item] => Array
                (
                    [0] => Desk
                    [1] => Chair
                    [2] => Table
                )

        )

)

Where is the rest of the data for the items?  If you were to traverse recursively through the object the item information is in fact there.  The problem is how to represent an element as an array that has a value and attributes.  Most of the solution's just ignore the attributes. 

print_r, var_dump, and get_object_vars ignore the attributes in this special case from what I've found.

Here is a simple function to convert the object into an array but it still fails to deal with the attributes.
<?php
function ToArray ( $data )
{
  if (
is_object($data)) $data = get_object_vars($data);
  return (
is_array($data)) ? array_map(__FUNCTION__,$data) : $data;
}
?>

So why even bother trying to convert the object to an array?  Simply because of serialization for caching the object.

One solution would be to create an index called "value" and assign that the element's value.  This way the attributes could coexist.
walter
12-Jun-2009 02:58
Working fix for the infamous SimpleXML + memcache bug.

Improves code at http://tinyurl.com/bmoon-simplexml to actually work for arbitrary-depth structures.  (Because neither the function nor the json_decode(json_encode($obj)) hack listed there worked for me.)

There's probably some superfluous code in here that could be improved on -- but it works!

<?php
 
# convert a structure that may include objects to a pure
 # array-based structure (that can be stored in memcache)
 #  ... includes support for simplexml!
 # (nb: may have problems with infinite recursive structs)
 
function enforce_array($obj) {
 
$array = (array)$obj;
  if(empty(
$array)) {
  
$array = '';
  }
  else {
   foreach(
$array as $key=>$value) {
    if(!
is_scalar($value)) {
     if(
is_a($value,'SimpleXMLElement')) {
     
$tmp = memcache_objects_to_array($value);
      if(!
is_array($tmp)) {
      
$tmp = ''.$value;
      }
     
$array[$key] = $tmp;
     }
     else {
     
$array[$key] = enforce_array($value);
     }
    }
    else {
    
$array[$key] = $value;
    }
   }
  }
  return
$array;
 }
?>
mail at kleineedv dot de
28-Apr-2009 07:37
I had a problem with simplexml reading nodes from an xml file. It always return an SimpleXML-Object but not the text inside the node.

Example:
<?xml version="1.0" encoding="UTF-8"?>
<Test>
    <Id>123</Id>
</Test>

Reading this xml into a variable called $xml and then doing the following
<?php
$myId
= $xml->Id;
?>
Did not return 123 in $myId, but instead I got a SimpleXMLElement Object.

The solution is simple, when you know it. Use explicit string conversion.
<?php
$myId
= (string)$xml->Id;
?>
oscargodson at gmail dot com
25-Jan-2009 11:05
To add to what others have said, you can't directly put a $_GET or $_POST value into a variable then into an attribute using SimpleXML. You must first convert it to an integer.

This will NOT work

<?php
      $page_id
= $_GET['id'];
      echo
$xml->page[$page_id]
?>

You will get something like:
Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/mysite/index.php on line 10

However, this WILL work and is much simpler then using (string) or other methods.
<?php
   $page_id
= intval($_GET['id']);
   echo
$xml->page[$page_id]
?>
bxt at die-optimisten dot net
26-Nov-2008 03:47
Addition to QLeap's post:
SimpleXML will return a reference to an object containing the node value and you can't use references in session variables as there is no feasible way to restore a reference to another variable.

This won't work too:
$val=$this->xml->node->attributes()->name;
echo $array[$val]; // will cause a warning because of the wrong index type.

You have to convert/cast to a String first:
echo $array[(string)$val];

This will work as expected, because converting will call the __toString() method. Therefor echo works too:
echo $val; // will display the name
QLeap
18-Nov-2008 10:27
Storing SimpleXMLElement values in $_SESSION does not work. Saving the results as an object or individual elements of the object will result in the dreaded "Warning: session_start() [function.session-start]: Node no longer exists" error.

For example, this does not work:

    $xml  = new SimpleXMLElement($page);
    $country  = $xml->Response->Placemark->AddressDetails->Country->CountryNameCode;
    $_SESSION['country'] = $country;

This will work:

    $_SESSION['country'] = (string) $country;
gwhitescarver at yahoo dot com
11-Sep-2008 08:02
Moving some code from a PHP 5.2.6 / Windows environment to a 5.2.0 / Linux environment, I somehow lost access to a plain text node within a SimpleXML Object.  On a var_dump of $xml_node, a [0] element was shown as the string '12'.  However, $xml_node[0] was evaluating NULL in 5.2.0.  You can see below the code change I made, pulling my data out of the raw XML with a regular expression.  Hope this is useful to someone.

//In some versions of PHP it seems we cannot access the [0] element of a SimpleXML Object.  Doesn't work in 5.2.0:
//$count = $xml_node[0];
//grab the raw XML:
$count = ($xml_node->asXML());
//pull out the number between the closing and opening brace of the xml:
$count = preg_replace('/.*>(\d*)<.*/', '$1', $count);
charlie at blankcanvasstudios dot com
14-Aug-2008 06:21
Optimizing aalaap at gmail dot com's php

<?php
function is_rss($feedxml) {
    @
$feed = simplexml_load_string($feedxml);

    return (
$feed->channel->item)?true:false;
}

function
is_atom($feedxml) {
    @
$feed = new SimpleXMLElement($feedxml);
    (
$feed->entry):true:false;
}
?>
aalaap at gmail dot com
30-Jun-2008 01:58
Here are two quick and dirty functions that use SimpleXML to detect if a feed xml is RSS or ATOM:

<?php
function is_rss($feedxml) {
    @
$feed = new SimpleXMLElement($feedxml);

    if (
$feed->channel->item) {
        return
true;
    } else {
        return
false;
    }
}

function
is_atom($feedxml) {
    @
$feed = new SimpleXMLElement($feedxml);

    if (
$feed->entry) {
        return
true;
    } else {
        return
false;
    }
}
?>

The functions take in the full text feed (retrieved via cURL, for example) and return a true or a false based on the result.

Introduction> <SDO_DAS_XML::saveString
Last updated: Fri, 30 Oct 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites