I need to get field attribute value from templateDetails xml . This is the field and I need the styles attribute
<field name="get_styles" type="yjsglist" default="blue|007ebd" styles="blue==007ebd|red==121212">
<option value="blue|007ebd">Blue</option>
<option value="red|ff0000">Mesh</option>
</field>
currently I am doing this
$get_xml = simplexml_load_file('templateDetails.xml');
$get_styles = $get_xml->config->fields->fieldset[1];
$load_styles ='';
foreach ($get_styles as $attribute){
if($attribute['name'] == 'get_styles'){
$load_styles = get_object_vars($attribute);
break;
}
}
$mystyles = explode('|',$attribute['styles']);
this would be JForm::getInstance way
$get_xml = JForm::getInstance('template-settings','templateDetails.xml');
$getStyles = $get_xml->getFieldAttribute('get_styles','styles' ,'default', 'params');
$mystyles = explode('|',$getStyles);
By just looking at the code JForm::getInstance seems faster but for some reason looks like performance is slower and I think is because I was breaking the loop as soon as I found the attribute name and Jform::getInstance might be looping trough the end.
Please note that I cannot get it from db or anywhere else since in db or from params->get I get the assigned value and I need the node attribute with all available styles.
What do you think? Is there a better/faster way to get the attribute from the above mentioned?
Any hep is appreciated. Thank you!
Solution: Great example by David got me to following code correction as advised here
http://www.dimuthu.org/blog/2008/09/30/xpath-in-simplexml/
$get_xml = simplexml_load_file('templateDetails.xml');
$styles = $get_xml->xpath('//@styles');
$style = (string)$styles[0];
$mystyles = explode('|',$style);
hope this helps you!