tags:

views:

215

answers:

5

Eg:

$xmlstr ='<Address><to>Sriniva</to><from>Chennai</from><country>India</county></Address>';

ReadXml($xmlstr) 

Output ->

Address

to: Srinivas

from: Chennai

country : India

A: 

You could use the XML functions, but using XSLT would be another decent option.

Ignacio Vazquez-Abrams
A: 

You might also want to have a look to the PHP xpath.

They use almost your same code in the example :)

Roberto Aloi
+6  A: 

With SimpleXML

$address = new SimpleXMLElement($xmlstr);
printf("%s\nto: %s\nfrom: %s\ncountry: %s",
       $address->getName(),
       $address->to, 
       $address->from, 
       $address->country);

or

$address = new SimpleXMLElement($xmlstr);
echo $address->getName(), PHP_EOL;
foreach($address as $name => $part) {
    echo "$name: $part", PHP_EOL;
}

Both output:

Address
to: Sriniva
from: Chennai
country: India

Just fix the closing country tag. It has a typo and is missing the r.

Gordon
A: 

Your Xml is not Valid so, its not Possible to do it with the xml tools php provides.

streetparade
A: 
<?php
$xml = simplexml_load_string("<Address>;
<to>Srinivas</to>
<from>Chennai</from>
<country>India</country>
</Address> ");



echo $xml->getName() . "<br />";

foreach($xml->children() as $child)
  {
  echo $child->getName() . ": " . $child . "<br />";
  }
?>

Output:

Address

to: Srinivas

from: Chennai

country : India

Srinivas Tamada
If this is an addition to your question, please edit your original question instead of giving it as an answer.
Gordon