Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8516845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:32:32+00:00 2026-06-11T05:32:32+00:00

I’ve loaded an XML file with ‘simplexml_load_file’ in PHP and now I want to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:32:33+00:00Added an answer on June 11, 2026 at 5:32 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm parsing an XML file, the creators of it stuck in a bunch social
i want to parse a xhtml file and display in UITableView. what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.