Hi all,

Sitting here and first time working with SImpelXML.

One of the nodes in the XML feed have name "start-date".

When trying to print it out with:

echo $elment->start-date;

I get in trouble since it recognize the "date" as a function and not "start-date" as a node name. I really need to user "start-date", is there another way?

    TheBeginner,

    Convert the SImpleXML Object to an array and access it.

    I can't take credit for this function, I found it a while ago on the net and neglected to take down the URL since it wasn't going into production, it was merely a proof-of-concept item.

    	function simplexml2array($xml) {
    	   if (get_class($xml) == 'SimpleXMLElement') {
    		   $attributes = $xml->attributes();
    		   foreach($attributes as $k=>$v) {
    			   if ($v) $a[$k] = (string) $v;
    		   }
    		   $x = $xml;
    		   $xml = get_object_vars($xml);
    	   }
    	   if (is_array($xml)) {
    		   if (count($xml) == 0) return (string) $x; // for CDATA
    		   foreach($xml as $key=>$value) {
    			   $r[$key] = simplexml2array($value);
    		   }
    		   if (isset($a)) $r['@'] = $a;    // Attributes
    		   return $r;
    	   }
    	   return (string) $xml;
    	}
    

    To use:

    $XMLArray = simplexml2array($simpleXmlObject);
    echo $XMLArray['start-date'];
    

      Unless I'm mistaken (which I very well could be), can't you do something like:

      $element->{'start-date'}

      ?

        bradgrafelman,

        I am assume in all honesty your way works quite well and is easier.

        This is where my limited SimpleXML usage comes to shine.

        I was using it as an easier means of trolling through the array to look for specific data I needed, and that way worked fine.

        Good information though - so thanks! (even though im not the thread poster, I enjoyed the info)

          big.nerd;10947185 wrote:

          This is where my limited SimpleXML usage comes to shine.

          I actually don't have any knowledge of SimpleXML - I just though that was another way of accessing properties of an object. I know it works with variable names, but I'm not sure if it works the same in OOP, though... :o

            Thank you for all help.

            This helped a lot!

            $element->{'start-date'} solved my problem. I was looking for just that, but couldn't come up with that solution.

              Write a Reply...