2

I have an xml file that has attributes/tags for mutliple levels in the feed however simplexml isn't showing them in the print_r dump.

Example:

<types tag1="1287368759" tag2="1287368759">
    <locations>
        <segment prefix="http" lastchecked="0">www.google.com</segment>
        <segment prefix="http" lastchecked="0">www.google.com</segment>
        <segment prefix="http" lastchecked="0">www.google.com</segment>
        <segment prefix="http" lastchecked="0">www.google.com</segment>
        <segment prefix="http" lastchecked="0">www.google.com</segment>
    </locations>
</types>

Problem is the tags in the <types> works fine and shows up in the xml dump, however the tags in each of the segments are not there. Any help?

deceze
  • 510,633
  • 85
  • 743
  • 889
Joe
  • 3,043
  • 9
  • 43
  • 56
  • also, anyone know how to get values from the "@attributes" array inside the simplexml? – Joe Oct 18 '10 at 05:49
  • simplexml drove me nuts, NUTS... I really loathe the mixture of arrays and objects. I am so much happier using Python and Beautiful Soup. I don't know if a library like Soup is available for PHP or not, but simplexml is anything but simple. I truly hate it. – JAL Oct 18 '10 at 06:09
  • Don't use print_r(). Seriously, don't. @Alex JL: SimpleXML doesn't use arrays or objects, so yeah, no wonder you lost your mind on it. SimpleXML supports nodes to be accessed as object properties, attributes as associative array entries, collections (nodelists) as 0-based numerically indexed arrays. All those are magic properties, print_r() will tell you squat about it. – Josh Davis Oct 18 '10 at 11:10
  • @Josh Davis So, it appears you're saying that 'nodes to be access as object properties' are not objects, and 'associative arrays' and '0-based numerically indexed arrays' are not arrays? I think I'll stand by my statement. – JAL Oct 18 '10 at 15:14
  • SimpleXML is an extension that lets you access XML documents using the object notation for nodes and the array notation for properties. At the very core, however, they are neither. They're a completely different data structure, wrapped in a familiar syntax. If you don't get that, stick with what you know. – Josh Davis Oct 19 '10 at 19:09

2 Answers2

2

SimpleXML is more like a resource, so var_dumping / print_ring will not yield any usable results.

A simple foreach($xml->types->segment->locations as $location) should work to loop through your locations, and use getAttributes() to get attributes of a node.

I'd suggest taking a closer look at the examples & functions in the manual (look at the comments too), as working with SimpleXML may be simple after you know how, you do need some background on how to use it as the usual introspection is not possible.

Josh Davis
  • 28,400
  • 5
  • 52
  • 67
Wrikken
  • 69,272
  • 8
  • 97
  • 136
0

SimpleXML will not show the attributes if the element has normal data in it, you need to use as the example below:

<?php
$xml = '<types tag1="1287368759" tag2="1287368759">
    <locations>
        <segment prefix="http" lastchecked="0"><![CDATA[www.google.com]]></segment>
        <segment prefix="http" lastchecked="0"><![CDATA[www.google.com]]></segment>
        <segment prefix="http" lastchecked="0"><![CDATA[www.google.com]]></segment>
        <segment prefix="http" lastchecked="0"><![CDATA[www.google.com]]></segment>
        <segment prefix="http" lastchecked="0"><![CDATA[www.google.com]]></segment>
    </locations>
</types>';

$xml_data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);

print_r($xml_data);

foreach ($xml_data->locations->segment as $segment) {
    print $segment['prefix'] . ' - ' . ((string) $segment) . "\n";
}

I'm not sure why this is, but I found that that works,

Hope it helps.

NiGhTHawK
  • 838
  • 1
  • 9
  • 14
  • that works however the problem is I need to get the prefix/lastchecked tag values as well, these don't show up in the print_r dump – Joe Oct 18 '10 at 05:59
  • I updated the example to include the displaying of an attribute, but as Wrikken stated in his comment there are other ways to do this... – NiGhTHawK Oct 18 '10 at 06:06
  • With regards to the attributes you can also look here: http://stackoverflow.com/questions/1652128/accessing-attribute-from-simplexml – NiGhTHawK Oct 18 '10 at 06:20
  • many thanks, strange how a print_r didn't reveal all the array levels, however they were all there! – Joe Oct 18 '10 at 06:22