1

I have following output :

<table count="" time="0.010006904602051">
<item>
<id>607</id>
<name>MSPOT6071</name>
<description>Hip Hop / Raps</description>
<type>3</type>
<radio_folder_id/>
<albumart>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_175.jpg
</albumart>
<albumart_300>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_350.jpg
</albumart_300>
<albumart_500>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_500.jpg
</albumart_500>
</item>
<item>
<id>48542614</id>
<name>US Pop - TESTB</name>
<description>Blues</description>
<type>3</type>
<radio_folder_id/>
</item>
</table>

I want to read this output in the form of Key value pair and then want to store each value. for eg i want the value of "name"

Can anyone please help me with the code..I am not getting the clue of how to do.

0

2 Answers 2

2

You can use simplexml extension functions to achieve your goals. With this extension you can load xml from file, from string or from a html node. Follow the links to get more information about each load function. I will explain here how to load using simplexml_load_file. This code:

<?php
echo "<pre>";   
$xml = simplexml_load_file("name.xml");
    print_r($xml);
    echo "</pre>";

?>

Will return this:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [count] => 
            [time] => 0.011766910552979
        )

    [item] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [id] => 607
                    [name] => MSPOT6071
                    [description] => Hip Hop / Raps
                    [type] => 3
                    [radio_folder_id] => SimpleXMLElement Object
                        (
                        )

                    [albumart] => http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_175.jpg
                    [albumart_300] => http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_350.jpg
                    [albumart_500] => http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_500.jpg
                )

            [1] => SimpleXMLElement Object
                (
                    [id] => 48542614
                    [name] => US Pop - TESTB
                    [description] => Blues 
                    [type] => 3
                    [radio_folder_id] => SimpleXMLElement Object
                        (
                        )

                )

        )

)

The information you want, can be accessed like this:

<?php
echo "<pre>";   
$xml = simplexml_load_file("name.xml");
    print_r($xml);
    echo "</pre>";
    echo "<pre>";
    foreach($xml->children() as $item){
        $arr = get_object_vars($item);
        foreach($arr as $key=>$value){
            echo "$key => $value" . PHP_EOL;            
        }
    }
    echo "</pre>";
?>

Note it will be accessed as object first. Then you can get all object vars to dynamically navigate through your object attributes.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried the following way the only issue is that it only returns the last results and not all the results.foreach ($result as $key => $value) { print("Key: ".$key." Value:".$value."\n"); foreach ($value as $subKey => $subValue) { print("SubKey: ".$subKey." SubValue:".$subValue."\n"); $this->id=$value['id']; } }
I do not have input xml file..so how can i do $xml = simplexml_load_file("name.xml");
I want to read all the keys and values from my output. I am getting stuck in the code as I am not able to right the correct foreach conditions. I want to right a generic foreach function that will read all the keys and values depending on item[0], [1]....So i want to use item[i]foreach ($result->children() as $key => $value) { print("Key: ".$key." Value:".$value."\n"); print($result->item[0]->name); foreach ($value as $subKey => $subValue) { print("SubKey: ".$subKey." SubValue:".$subValue."\n"); //$this->id=$value['id']; } }
You are not doing it right. You must work with the object first. See my answer edited to your needs
0

The

<?php

$xmlstr = '<?xml version="1.0" standalone="yes"?>
<table count="" time="0.010006904602051">
<item>
<id>607</id>
<name>MSPOT6071</name>
<description>Hip Hop / Raps</description>
<type>3</type>
<radio_folder_id/>
<albumart>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_175.jpg
</albumart>
<albumart_300>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_350.jpg
</albumart_300>
<albumart_500>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_500.jpg
</albumart_500>
</item>
<item>
<id>48542614</id>
<name>US Pop - TESTB</name>
<description>Blues</description>
<type>3</type>
<radio_folder_id/>
</item>
</table>';

$xml = new SimpleXMLElement($xmlstr);

foreach($xml->item as $item)
{
    echo $item->name."<br>";
}

echo $xml->item[0]->name;

echo '<pre>';
print_r($xml);
echo '</pre>';

?>

Assign your XML string to a variable $xmlstr and so you don't run into incomplete XML document errors make sure you include the following at the top of your XML document.

<?xml version="1.0" standalone="yes"?> 

Then use the built-in SimpleXML class by passing your XML string $xmlstr to SimpleXML:

$xml = new SimpleXMLElement($xmlstr);

Now you can access your XML file as a PHP object using the attributes and methods of the SimpleXML class. In this example we loop over the 'item'(s) in your XML document and print out the 'name' elements:

foreach($xml->item as $item)
{
    echo $item->name."<br>";
}

I have also include code to access the first item element:

echo $xml->item[0]->name;

As well as some debugging code view the XML document in a SimpleXML object:

echo '<pre>';
print_r($xml);
echo '</pre>';

You access the key or in this case the object property by its name. So in your foreach loop you might do:

if($item->name)
{
    echo $item->name;
}

or

if($item->description)
{
    echo $item->description;
}

May the force be with you.

2 Comments

You should provide an explanation about what this is doing.
THank you ..this really helped..Now I want to read the key of the value..So I want to verify whether the "name" key is present in my output..how should I get the key?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.