1

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?

4
  • No source XML document has been provided. How can then this be an "XPath question"? Commented Jul 29, 2012 at 16:58
  • An API is returning the data as an XML document. With the data, I have created the SimpleXMLElement Object and have used XPATH to narrow down my query. All I need now, is to get the actual data value. Commented Jul 29, 2012 at 17:05
  • "with the data" ... With what data? You haven't shown the source XML document. If you only need to get the value of a property, this really isn't an XPath question. Commented Jul 29, 2012 at 17:15
  • Parahat Melayev helped me to fix my xpath query. As such, this was initially an xpath query. I have updated the question as I have made further progress since first asking the question. As such, this is no longer an xpath query - but it was before your comment. It is a matter of converting from object to string. which I am now able to do now. Commented Jul 29, 2012 at 17:27

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

XPath query should be "//query/results/rate[contains(@id, 'XAUGBP')]"

2 Comments

Thanks. But how do I turn that object into an associative array so that I can actually do stuff with it? Alternatively, how do I turn //query/results/rate[contains(@id, 'XAUGBP')]/Ask from an object to a value?
By using SimpleXMLElement::__toString you should be able to get string value for //query/results/rate[contains(@id, 'XAUGBP')]/Ask but it seems quite redundant to me to convert SimpleXML into associative array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.