1

How can I get the value of the below xml on attribute name.

    <list version="1.0">
<meta>
<type>resource-list</type>
</meta>
<resources start="0" count="165">
<resource classname="Quote">
<field name="name">USD/KRW</field>
<field name="price">1131.319946</field>
<field name="symbol">KRW=X</field>
<field name="ts">1371547710</field>
<field name="type">currency</field>
<field name="volume">0</field>
</resource>
<resource classname="Quote">
<field name="name">SILVER 1 OZ 999 NY</field>
<field name="price">0.045962</field>
<field name="symbol">XAG=X</field>
<field name="ts">1371505422</field>
<field name="type">currency</field>
<field name="volume">7</field>
</resource>....

There are 165 such structures of

Want to fetch SILVER 1 OZ 999 NY 0.045962 XAG=X 1371505422 etc

My code so far goes like

$xml = simplexml_load_string($data);

foreach($xml->children() as $resources)
{
    foreach($resources->children() as $resource => $data)
    {
        echo $data->field['name'];
        echo "<br>";
    }


}
hakre
  • 193,403
  • 52
  • 435
  • 836

4 Answers4

1

Based on the XML in the comment, it should be:

$xml = simplexml_load_string($data);

foreach($xml->resources->resource as $resource)
{
    foreach($resource->field as $field)
    {
        echo $field->attributes()->name; // e.g. name, price, symbol
        echo (string)$field; // this is the content, e.g. SILVER 1 OZ 999 NY
    }
}

Note that $xml always contains the root element which in this case is <list>.

Demo

MrCode
  • 63,975
  • 10
  • 90
  • 112
  • resource-list USD/KRW 1131.319946 KRW=X 1371547710 currency 0 SILVER 1 OZ 999 NY 0.045962 XAG=X 1371505422 currency ..... –  Jun 18 '13 at 10:55
  • It gives me a blank output. Nothing displayed on screen! According to your modified code. –  Jun 18 '13 at 11:06
  • Yes its done. Thanks a ton. I was going crazy on it...thanks. –  Jun 18 '13 at 11:15
0

here you go.

foreach($xml->children() as $resources)
{
foreach($resources->children() as $resource => $data)
{
    echo $data->field['name'];

    echo "<br>";
    echo $resource->attributes()->name;

}


}
  • Fatal error: Call to a member function attributes() on a non-object –  Jun 18 '13 at 11:01
0

corrected code:

<quote>
<name>test</name><price>567</price><symbol>xyz</symbol><ts>1371505422</ts>
<type>currency</type>
<volume>7</volume>
</quote>
$xmlObject = new SimpleXMLElement($xmlstring);
foreach ($xmlObject->children() as $node){
echo $node->Company;
echo $node->price;
echo $node->symbol;
echo $node->ts;
echo $node->type;
echo $node->volume;
}
Jibu K
  • 496
  • 5
  • 3
0

You were not that far away. Query the elements by their name instead of using children(), you only need the children() method for children not within the elements namespace. In your code you just queried the wrong data, see this example (Demo):

$xml = simplexml_load_string($data);

foreach($xml->resources->resource as $resource)
{
    echo "---------------\n";
    foreach($resource->field as $field)
    {
        echo $field['name'], ': ',  // e.g. name, price, symbol
             $field, "\n";          // this is the content, e.g. SILVER 1 OZ 999 NY
    }
}

Outout:

---------------
name: USD/KRW
price: 1131.319946
symbol: KRW=X
ts: 1371547710
type: currency
volume: 0
---------------
name: SILVER 1 OZ 999 NY
price: 0.045962
symbol: XAG=X
ts: 1371505422
type: currency
volume: 7

This is outlined and explained in the basic simplexml usage examples, see: http://php.net/simplexml.examples-basic

hakre
  • 193,403
  • 52
  • 435
  • 836
  • What if I just want to extract the name and price? How should I go about. –  Jun 19 '13 at 04:30
  • see the linked examples, and also there is xpath to do that: [Xpath query with PHP (take two values)](http://stackoverflow.com/questions/9293463/xpath-query-with-php-take-two-values/9293590#9293590) – hakre Jun 19 '13 at 05:09