1
votes

I've loaded an XML file with 'simplexml_load_file' in PHP and now I want to read some elements from it to print them out with PHP. All methods that I've found didn't lead to success because my XML file is maybe just too complex... The best thing I've found is the following:

Just wanted to add a post as to how you can extract the value from a SimpleXMLElement. Its not as straightforward as you think. Because its a complex object you can't just access the element directly. Here is a sample of data that represents a var_dump of a SimpleXmlElement

array(1) { [0]=> object(SimpleXMLElement)#13 (2) { ["@attributes"]=> array(1) { ["name"]=> string(5) "title" } [0]=> string(19) "PHP Tech Book" } }

If you want to extract the title of the book you have to cast the specified element to a string like so. $newTitle = (string) $title[0];

The $title variable is the SimpleXMLElement that you have extracted from the xml document using simplexml_load_string for instance. To initially access the title element from the xml document you can do like so, using xpath. $title = $doc->xpath('str[@name="title"]');

Hope this helps someone out there.

I tried to use "$title = $doc->xpath('str[@name="title"]');" with my own XML file replacing it with my own elements, but then I get a PHP Error:

A PHP Error was encountered Severity: Notice Message: Array to string conversion Filename: views/welcome_message.php Line Number: 79

This is my code:

<?php
    if (file_exists('data.xml')) {
        $xml = simplexml_load_file('data.xml');

    $title = $xml->xpath('str[@name="name"]');
    echo $title;

    } else {
        exit('Failed to open test.xml.');
    }
    ?>

This is a little extract of my very confusing xml file:

SimpleXMLElement Object ( [folder] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => ABC ) [file] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => DEF [size] => 123 [creation-time] => 03.09.2012 16:36:59 [last-write-time] => 06.09.2012 10:46:39 [id3v2] => 1 [v2-tag-PRIV] => XMP ; WM/MediaClassSecondaryID; WM/MediaClassPrimaryID ?}`?#??K??H?*(D; WM/Provider A M G; WM/WMContentID ?????E?@??B????; WM/WMCollectionID ?Yd??B??N5 ?; WM/WMCollectionGroupID ]?????N?????^e; WM/UniqueFileIdentifier A M G a _ i d = R 2 0 5 6 3 8 9 ; A M G p _ i d = P 2 6 4 5 ; A M G t _ i d = T 2 2 1 6 6 0 0 3 [v2-track] => 2 [v2-song-title] => FGH [v2-album] => IJK [v2-genre] => (12) [v2-tag-TPUB] => LMN [v2-tag-TYER] => 1997 [v2-tag-TPE2] => OPQ [v2-composer] => RST [v2-artist] => UVW [id3v1] => 1 [song-title] => XYZ [artist] => ARTIST1 [album-title] => TITLE1 [year] => 1997 [comment] => [track] => 2 [genre] => Other [mpeg-version] => 1 [mpeg-layer] => 3 [bitrate] => 320Kbps [sampling-rate] => 48000Hz [channel-mode] => JointStereo ) )

Why is there a '@' in [@attributes] and how do I have to deal with that? How can I get the infos from the different elements?

2

2 Answers

0
votes

When I have to deal with 'heavy' dom manipulation, I always use simple_html_dom. Give it a try, maybe it helps!

0
votes

The SimpleXML ->xpath() function returns an array of SimpleXML objects, each representing an element or attribute which matches your XPath expression.

So firstly, you need to loop over the results (or take the first one with [0]); and secondly, you need to do something with the SimpleXML object found. If it's an attribute, (string)$sx_attrib will give you its value; otherwise, you'll need to do further manipulation.

Finally, don't use var_dump, print_r, or var_export on SimpleXML objects, as they're not "real" PHP objects but wrappers around non-PHP data. Use simplexml_dump instead.