6

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!

6

JForm really does the same things as your more standard PHP code. If you jump into the definition for JForm at libraries/joomla/form/form.php and look through the functions, you will see simplexml_load_file() and a loop in the function findFields() that matches your query.

And you are correct that you will probably get a performance boost from the first code. There are two reasons for this. The biggest is probably that you break once you found the value that you need. The other is that JForm will add some extra processing to check for paths in the xml to load fields from and such that you don't really need but adds extra work to process.


I can think of only one possible performance boost, which would be to skip the loop entirely and use xpath to get the exact value that you want. I'm not the best at xpath, so you might want to read a bit more about that if you want to try it: http://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx

I tested this a bit with your form data, and this should be an alternative:

$get_xml = simplexml_load_file('templateDetails.xml');
$styles = $get_xml->xpath("//field[@name='get_styles']/@styles");
$style = (string)$styles[0];
$mystyles = explode('|',$style);

The xpath returns an array, which I grab the only instance from and force it to be a string. Then I explode it like you were.

  • excellent example and the explanation. thank you much! – Dan May 7 '14 at 22:02
  • you were very close!! I dont belive how simple this is. $get_xml->xpath('//@styles'); print_r((string)$ztyles[0]); excellent! – Dan May 7 '14 at 22:19
  • 1
    ugh. It would have helped if I copied the right xpath over. I updated the answer with an xpath that actually works. Yours will obviously work, but if you want a more specific version that makes sure it is an attribute of a "field" node with the name "get_styles" that should actually work now. – David Fritsch May 7 '14 at 22:24
  • marvelous! I got nothing else to say here. great scholar. – Dan May 7 '14 at 22:34
3

use simplexml_load_file
JForm - it the class for form building and basically it the layer between you and simplexml, so it will be slower than just simplexml_load_file

but your code can be simpler, look at SimpleXMLElement::xpath for find a field, and at SimpleXMLElement::attributes for get all attributes or just use (string) $field_that_you_found['style']

  • thank you much , yes seems like the code should be refined with xpath, the code i use is old from php 4.4 i think so is time to refine it – Dan May 7 '14 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.