Eg:
$xmlstr ='<Address><to>Sriniva</to><from>Chennai</from><country>India</county></Address>';
ReadXml($xmlstr)
Output ->
Address
to: Srinivas
from: Chennai
country : India
Eg:
$xmlstr ='<Address><to>Sriniva</to><from>Chennai</from><country>India</county></Address>';
ReadXml($xmlstr)
Output ->
Address
to: Srinivas
from: Chennai
country : India
You could use the XML functions, but using XSLT would be another decent option.
You might also want to have a look to the PHP xpath.
They use almost your same code in the example :)
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.
Your Xml is not Valid so, its not Possible to do it with the xml tools php provides.
<?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