tags:

views:

71

answers:

2

Hello,

I have a XML file, and the layout is such

<author>
   <name></name>
   <iso></iso>
   <price></price>

</author>

I know how it loops. I want to know how I can extract the value of

<name>

Thanks Jean

[edit]

my apologies, if in the

 <author>
          <name>
            <first_name></first_name>
            <last_name></lastname>
          </name>
  </author>

I want to extract first_name

+1  A: 

Use SimpleXML.

Edit: I see. That wasn't showing up before. Try this:

$xml = simple_xml_load_string([your XML string])
echo $xml->name;

Does that work?

Skilldrick
I am using simplexml, but i want to extract the value of <name>
Jean
@Jean: This is perfectly described in the SimpleXML documentation (http://no.php.net/manual/en/simplexml.examples-basic.php)
Johannes Gorset
Why the downvote?
Skilldrick
Sorry, My question was wrongly put up, pls check the question
Jean
+4  A: 

Use simplexml or similar:

<?php
$string = <<<XML
<author>
    <name>
        <first_name>John</first_name>
        <last_name>Smith</last_name>
    </name>
</author>
XML;

$xml = simplexml_load_string($string);

var_dump($xml);
?>

Will output something like this:

object(SimpleXMLElement)#1 (1) {
  ["name"]=>
  object(SimpleXMLElement)#2 (2) {
    ["first_name"]=>
    string(4) "John"
    ["last_name"]=>
    string(5) "Smith"
  }
}

And you can access the name like this:

echo $xml->name->first_name; // outputs 'John'
echo $xml->name->last_name; // outputs 'Smith'
Tatu Ulmanen
Thanks its working.....................
Jean