Skip to content

Instantly share code, notes, and snippets.

@Shaz3e
Last active August 29, 2015 13:57
Show Gist options
  • Save Shaz3e/9396156 to your computer and use it in GitHub Desktop.
Save Shaz3e/9396156 to your computer and use it in GitHub Desktop.
Get XML Elements into PHP File
<?xml version="1.0" encoding="UTF-8"?>
<extension type="template" client="site" version="1.0">
<name>S3-CMS</name>
<files>
<filename>index.html</filename>
<filename>index.php</filename>
<filename>component.php</filename>
<filename>module.php</filename>
<folder>tmpl</folder>
<folder>css</folder>
</files>
<config>
<fields name="basic">
<fieldset name="test">
<field name="first" label="this is the first label" description="this is the first description for testing purpose only."></field>
</fieldset>
</fields>
<fields name="advance">
<fieldset name="test1">
<field name="second" label="this is the second label" description="this is the second description for testing purpose only."></field>
</fieldset>
</fields>
<fields name="expert">
<fieldset name="modules">
<field name="modules positions" label="Module Positions Location" description="this is the module position node."></field>
</fieldset>
</fields>
<fields name="basic">
<fieldset name="test">
<field name="first" label="this is the first label" description="this is the first description for testing purpose only."></field>
</fieldset>
</fields>
<fields name="advance">
<fieldset name="test1">
<field name="second" label="this is the second label" description="this is the second description for testing purpose only."></field>
</fieldset>
</fields>
<fields name="expert">
<fieldset name="modules">
<field name="modules positions" label="Module Positions Location" description="this is the module position node."></field>
</fieldset>
</fields>
</config>
</extension>
<?php
$xml = simplexml_load_file("file.xml");
//print_r($xml);
//echo '<br>';
//echo $xml;
//echo '<br>';
//$xml = simplexml_load_file("xml_file.xml");
//$details = $xml->getName();
//$type = $xml->show->attributes()->type;
$type = $xml->attributes()->{'type'};
$client = $xml->attributes()->{'client'};
$version = $xml->attributes()->{'version'};
echo 'Type: '.$type . ' Client: ' .$client . ' Version: ' . $version ;
echo '<br>'.$xml->getName().'<br>';
echo '<hr>';
echo 'Fields Name: '.$xml->config->fields->attributes()->{"name"} . '<br>';
echo '<hr>';
echo 'Fieldset Name: '.$xml->config->fields->fieldset->attributes()->{"name"} . '<br>';
echo '<hr>';
echo 'Field Name: '.$xml->config->fields->fieldset->field->attributes()->{"name"} . '<br>';
echo 'Field Label: '.$xml->config->fields->fieldset->field->attributes()->{"label"} . '<br>';
echo 'Field Description: '.$xml->config->fields->fieldset->field->attributes()->{"description"} . '<br>';
echo '<br><br><br>';
echo '<h2>All "name" Attributes</h2>';
//$xml = simplexml_load_string($xml);
foreach($xml->xpath('//fields') as $child){
$attributes = $child->attributes();
echo $attributes['name'] . "<br>";
}
echo '<h2>All "name" of fieldset Attributes</h2>';
foreach($xml->xpath('//fieldset') as $child){
$attributes = $child->attributes();
echo $attributes['name'] . "<br>";
}
echo '<h2>All "name" of field Attributes</h2>';
foreach($xml->xpath('//field') as $child){
$attributes = $child->attributes();
echo '<strong>Name:</strong> '. $attributes['name'] . '<br> <strong>Label</strong>: '.$attributes['label'] . ' <br><strong>Description:</strong> '.$attributes['description'].'<br>';
}
/*
echo $xml->name;
echo '<br>';
foreach($xml->files->filename as $file){
echo $file.'<br>';
}
foreach($xml->files->folder as $folder){
echo '/'.$folder.'/'.'<br>';
}
foreach($xml->config->fields as $field){
echo $field;
}
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment