[development] trouble destructing xml

Jamie Holly hovercrafter at earthlink.net
Fri Mar 25 01:34:27 UTC 2011


I use SimpleXML pretty much exclusively for this stuff:

$data='<this>
<that parm1="a" parm2="b" parm3="c">red</that>
<that parm1="e" parm2="f" parm3="d">blue</that>
</this>';

$xml=simplexml_load_string($data);
foreach ($xml->that as $item){
  print_r($item);
  echo "<hr>";
}

Returns:

SimpleXMLElement Object ( [@attributes] => Array ( [parm1] => a [parm2]
=> b [parm3] => c ) [0] => red )
------------------------------------------------------------------------
SimpleXMLElement Object ( [@attributes] => Array ( [parm1] => e [parm2]
=> f [parm3] => d ) [0] => blue )

Which is what it should.

You can also use XPath to get at what you want:

$xpath = $xml->xpath('/this/that');

Which would return an array of each <that> item along with the attributes.

Jamie Holly
http://www.intoxination.net 
http://www.hollyit.net


On 3/24/2011 9:09 PM, jeff at ayendesigns.com wrote:
> When all else fails, and #xml, ##php and #drupal-support come up
> empty...ask the folks who actually code :)
>
> I have an xml structure that has
>
> <this>
> <that parm1="a" parm2="b" parm3="c">red</that>
> <that parm1="e" parm2="f" parm3="d">blue</that>
> </this>
>
> I've tried simplexml and a few third-party scripts, one that loads the
> xml into an associative array rather than an object, and nothing lets
> me get at 'red' or 'blue'.  When I use dsm() on the results, for
> example, in the case of the object with simplexml I get only one
> 'that' and do not get its contents. When I use the script that creates
> the array, I get both that's, and all their parms as elements, but not
> the colors.
>
> I'm curious what else people are using.
>
> Jeff
> -- 
> /I am a non sequitur. Beware, the contents were packaged where peanuts
> are processed./
>
> Ayen Designs
> 388 Bullsboro Drive #105 · Newnan, Georgia 30263
> 404-271-9734
> Web:ayendesigns.com <http://ayendesigns.com/>
> Blog: theAccidentalCoder.com <http://theaccidentalcoder.com/>
> Drupal: j. ayen green <http://drupal.org/user/367108> (367108)
> IRQ: j_ayen_green
> IM (Yahoo) baalwww    (MSN) baalwww at yahoo.com
> Skype: ayendesigns | Facebook: ayendesigns | Twitter: @ayendesigns
>
>
>
> Ayen Designs is the computer services division of
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20110324/575badf5/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ayenlogo
Type: image/jpeg
Size: 8316 bytes
Desc: not available
Url : http://lists.drupal.org/pipermail/development/attachments/20110324/575badf5/attachment-0002.jpe 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: acmelogo
Type: image/jpeg
Size: 1462 bytes
Desc: not available
Url : http://lists.drupal.org/pipermail/development/attachments/20110324/575badf5/attachment-0003.jpe 


More information about the development mailing list