Thursday, April 16, 2015

php SimpleXMLElement tutorial

<?php
$xmlString = '<?xml version="1.0" encoding="utf-8"?>
<Foo>
  <bar>
    <type>System.String</type>
    <value>Stackoverflow</value>
  </bar>
  <bar>
    <type>System.Boolean</type>
    <value>True</value>
  </bar>
  <bar>
    <type>System.Char</type>
    <value>x</value>
  </bar>
  <bar>
    <type>System.Int32</type>
    <value>42</value>
  </bar>
</Foo>';
//header('Content-Type: application/xml');
//echo $xmlString;exit;

$xml = new SimpleXMLElement($xmlString);
foreach ($xml->bar as $element) {
echo $element->type."<br>";
echo $element->value."<br>";
// or...........
//foreach($element as $key => $val) {
//echo "{$key}: {$val}<br>";
//}
}
?>

No comments: