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 8293299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:46:36+00:00 2026-06-08T13:46:36+00:00

I have the following SimpleXMLElement Object http://pastebin.com/qEP0UUPJ I can query it using xpath $xaugbp

  • 0

I have the following SimpleXMLElement Object http://pastebin.com/qEP0UUPJ

I can query it using xpath $xaugbp = $xml->xpath("/query/results/rate"); but this just narrows down the search:

http://pastebin.com/Xwezx4bZ

I wish to access the following object:

[0] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [id] => XAUGBP
            )

        [Name] => XAU to GBP
        [Rate] => 1030.5784
        [Date] => 7/27/2012
        [Time] => 5:55pm
        [Ask] => 1027.5662
        [Bid] => 1033.5896

I can get that particular object using

$ob = $xmlObject->xpath("//query/results/rate[contains(@id, 'XAUGBP')]");

I can get ‘Ask’ by using

$ob = $xmlObject->xpath("//query/results/rate[contains(@id, 'XAUGBP')]/Ask");

But how do I turn that object into a variable. e.g. so that I can do calculations on it?

Is xpath the only way of doing this? Is there a better / more efficient / quicker way? Is it possible to convert this to an associative array for easy access?

  • 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-08T13:46:39+00:00Added an answer on June 8, 2026 at 1:46 pm

    Preface

    Before you do anything, (re)familiarise yourself with SimpleXML’s peculiar way of doing things. The best place to start is the SimpleXML basic usage manual page.


    The example below shows you:

    • Getting a single <rate> element object based on an attribute’s value, and how to display it.
    • Looping over all of the <rate> elements and displaying their values.

    Basic example code

    $query = simplexml_load_file('…');
    
    // Get a <rate> element by its id attribute
    $rates = $query->xpath('results/rate[@id = "XAUGBP"]');
    $rate  = $rates[0];
    echo "$rate->Name was at $rate->Rate at $rate->Time on $rate->Date\n";
    
    echo "-----\n";
    
    // or, loop over all of the <rate> elements
    foreach ($query->results->rate as $rate) {
        echo "$rate->Name was at $rate->Rate at $rate->Time on $rate->Date\n";
    }
    

    The above example outputs:

    XAU to GBP was at 1030.5784 at 5:55pm on 7/27/2012
    -----
    XAU to GBP was at 1030.5784 at 5:55pm on 7/27/2012
    XAG to GBPZ 999 N was at 17.4502 at 5:55pm on 7/27/2012
    XPT to GBPZ 999 was at 893.3414 at 5:55pm on 7/27/2012
    XPD to GBP1 UZ was at 362.652 at 5:55pm on 7/27/2012
    

    (See this example running online)


    XML to array

    I see this being asked time and time and time again. Rather than turning a tree of SimpleXMLElement objects into some “friendly” array, please instead take the time to learn how to use SimpleXML: once you have done that, there is no need to take the intermediate step of turning the objects into arrays to work with. The “basic usage” page referred to above should get you going, but key points to know are:

    1. Child nodes use property syntax: $parent->child
    2. Attributes use array-style syntax: $element["attr_name"]
    3. Cast objects into friendly types when desired: (string) $element->child
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XML array: [link]=> array(2) { [0]=> object(SimpleXMLElement)#311 (1) { [@attributes]=>
I have following plist: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE plist PUBLIC -//Apple//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
I have this SimpleXMLElement object with a XML setup similar to the following... $xml
I have the following simple xml object file: [AuthorList] => SimpleXMLElement Object ( [@attributes]
Hi I have following simple xml object: [ad] => Array ( [0] => SimpleXMLElement
I have the following XML (XBRL) file <xbrli:context id=I2010_ForwardContractsMember> <xbrli:entity> <xbrli:identifier scheme=http://www.sec.gov/CIK>0000027419</xbrli:identifier> <xbrli:segment> <xbrldi:explicitMember
I have the following XML: <?xml version=1.0 encoding=UTF-8?> <gnm:Workbook xmlns:gnm=http://www.gnumeric.org/v10.dtd xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://www.gnumeric.org/v9.xsd> <office:document-meta xmlns:office=urn:oasis:names:tc:opendocument:xmlns:office:1.0
I have the following object: object(SimpleXMLElement)#337 (1) { [0]=> string(4) 1001 } But I
Consider the following XML: <s:Envelope xmlns:s=http://schemas.xmlsoap.org/soap/envelope/> <s:Body> <myResponse xmlns=https://example.com/foo> <myResult xmlns:a=https://example.com/bar xmlns:i=http://www.w3.org/2001/XMLSchema-instance> <a:accountNumber>AAA</a:accountNumber> <a:accountName>BBB</a:accountName>
I have following div in a page (I can not modify). <div id=:0.control>Click me</div>

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.